-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.cpp
103 lines (88 loc) · 2.3 KB
/
system.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "system.h"
void System::AddObject(std::unique_ptr<Star>&& star)
{
std::string name = star->GetName();
objects_[name] = std::move(star);
object_list_.push_back(objects_.at(name).get());
is_sorted_ = false;
}
void System::AddObject(std::unique_ptr<Planet>&& planet)
{
std::string name = planet->GetName();
objects_[name] = std::move(planet);
children_[objects_.at(name)->GetParent()->GetName()].push_back(objects_.at(name).get());
object_list_.push_back(objects_.at(name).get());
is_sorted_ = false;
}
Object* System::GetParent(const std::string& name)
{
auto it = objects_.find(name);
if (it != objects_.end())
{
return it->second.get();
}
return nullptr;
}
void System::Print(const Date& date)
{
if (!is_sorted_)
{
SortByParent();
}
for (const auto& object : object_list_)
{
std::cout << object->GetName()
<< std::setprecision(6) << "\t{x,y} = " << object->GetPosition(date)
<< std::endl;
}
}
void System::SortByParent()
{
std::vector<Object*> objects;
for (auto it = objects_.begin(); it != objects_.end(); ++it)
{
Object* parent = (*it).second->GetParent();
if (parent == nullptr)
{
objects.push_back((*it).second.get());
std::vector<Object*> children = std::move(GetAllChildren((*it).first));
objects.insert(objects.end(), children.begin(), children.end());
}
}
object_list_ = std::move(objects);
is_sorted_ = true;
}
void System::SortByDistance(std::vector<Object*>& objects)
{
auto comporator = [&](Object* lhs, Object* rhs)
{
Distance lhs_distance = lhs->GetDistance(), rhs_distance = rhs->GetDistance();
return lhs_distance.AU() < rhs_distance.AU();
};
std::sort(objects.begin(), objects.end(), comporator);
}
std::vector<Object*> System::GetChildren(const std::string& name)
{
auto it = children_.find(name);
if (it != children_.end())
{
SortByDistance((*it).second);
return (*it).second;
}
return {};
}
std::vector<Object*> System::GetAllChildren(const std::string& name)
{
std::vector<Object*> all_children;
std::vector<Object*> children = GetChildren(name);
for (auto child : children)
{
std::vector<Object*> children_children = GetAllChildren(child->GetName());
all_children.push_back(std::move(child));
for (auto child_child : children_children)
{
all_children.push_back(std::move(child_child));
}
}
return all_children;
}