-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcircle.h
More file actions
287 lines (221 loc) · 8.03 KB
/
Copy pathcircle.h
File metadata and controls
287 lines (221 loc) · 8.03 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* ------------------------------
Copyright <2018> [Tim Hu]
email: timtingwei@hotamail.com
------------------------------
File:circle.h
Date:Sat Jun 2 14:12:22 CST 2018
compile: g++ -std=c++11 spaceOrder.cpp -lGLEW -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lXcursor
-----------------------------
*/
#include <iostream>
#include <vector>
const double PI = 3.1415926f;
struct Circle {
double x, y, r;
int resourceCount = 20; // 圆的资源被分成几份
double unitRadian = 2*PI/resourceCount; // 每份的弧度
void moveCircle(const double angle, const double dist);
// 得到c2,c所占的弧度, is_main决定谁为主体
double getRadian(const Circle& c2, const bool& is_main);
// 得到c2与该c的圆心距离
double getDistance(const Circle& c2);
// c2相对于c的角度, is_main决定c是否为主体
double getDirection(const Circle& c2, const bool& is_main);
};
void Circle::moveCircle(const double angle, const double dist) {
// 通过已经确定的空间, 与x轴逆时针角度, 距离, 确定下一个空间的位置
x += cos(angle) * dist;
y += sin(angle) * dist;
}
double Circle::getRadian(const Circle& c2, const bool& is_main) {
// 得到c2,c所占的弧度, is_main决定谁为主体
double dist = getDistance(c2);
// is_main, true, c2占c1; false, c1占c2
double area = (is_main) ? (pow(c2.r, 2)*PI): pow(r, 2)*PI;
return 2 * atan(sqrt(area/PI)/dist);
}
double Circle::getDistance(const Circle& c2) {
// 得到c2与该c的圆心距离
double dist = sqrt(pow((x - c2.x), 2) +
(pow((y - c2.y), 2)));
return dist;
}
double Circle::getDirection(const Circle& c2, const bool& is_main) {
// c2相对于c的角度, is_main决定c是否为主体
double delta_x, delta_y;
if (is_main) {
delta_x = c2.x - this->x;
delta_y = c2.y - this->y;
} else {
delta_x = this->x - c2.x;
delta_y = this->y - c2.y;
}
double alpha = atan(delta_y/delta_x);
if ((delta_x > 0) && (delta_y > 0)) { return alpha; // 第一象限
} else if ((delta_x < 0) && (delta_y > 0)) { return PI+alpha; // 2
} else if ((delta_x < 0) && (delta_y < 0)) { return alpha + PI; // 3
} else if ((delta_x > 0) && (delta_y < 0)) {return 2*PI+alpha;
}
}
struct Circle_lst {
Circle circles[50];
int n = 0;
Circle coverCircle;
void appendCircle(Circle);
void appendCircles(Circle_lst);
void getCoverCircle();
void moveCircles(double angle, double dist);
Circle getFarthestCircle(const Circle P);
void printCircles();
void drawCircles(int childrenColor);
};
struct Circle_tree {
Circle_lst clsts_arr[50];
int n = 0;
void appendClst(Circle_lst);
void printTreeCircles();
};
class ChildrenCircle_lst : public Circle_lst {
public:
// ...
std::vector<std::vector<int> > rsrcIndex_2vec = {};
// 每个子空间到父空间的距离
std::vector<double> dist_vec = {};
std::vector<Circle> childCircles = {};
private:
// Circle_lst circles
};
class ParentCircle : public Circle {
public:
// 构造函数
ParentCircle() = default;
ParentCircle(double p_x, double p_y, double p_r) {
x = p_x, y = p_y, r = p_r;
}
Circle_lst getChildrenClst() {return childrenClst;}
// 获取父空间到每个子空间的距离
std::vector<double> getDist_vec();
// 从子空间获取索引列表
std::vector<std::vector<int> > getResourceOccupy_2vec();
// 每个资源对应圆的索引列表
std::vector<std::vector<int> > resourceOccupy_2vec = {};
ChildrenCircle_lst childrenClst; // 对应的所有的子空间
// 父空间到每个子空间的距离
std::vector<double> dist_vec = {};
private:
};
// 获取父空间到每个子空间的距离
std::vector<double> ParentCircle::getDist_vec() {
std::vector<double> vec = {};
for (int i = 0; i < this->childrenClst.n; i++) {
double dist = this->getDistance(this->childrenClst.circles[i]);
vec.push_back(dist);
}
this->dist_vec = vec;
return this->dist_vec;
}
/*
// 从子空间获取索引列表
std::vector<std::vector<int> > ParentCircle::getResourceOccupy_2vec() {
std::vector<std::vector<int> > vec = {};
for (int i = 0; i < this->childrenClst.n; i++) {
vec.push_back(this->childrenClst.childCircles[i].rsrcIndex_vec);
}
this->resourceOccupy_2vec = vec;
return this->resourceOccupy_2vec;
}
*/
class ChildCircle : public Circle {
public:
// ParentCircle parentCircle; // 该子空间的父空间
// std::vector<int> rsrcIndex_vec = {}; // 子空间占父空间的资源序列
// 构造函数
ChildCircle() = default;
ChildCircle(ParentCircle P, double p_x, double p_y, double p_r) :
parentCircle(P) {
// parentCircle = P;
initResourceIndex();
x = p_x, y = p_y, r = p_r;
}
// 通过parentCircle, rsrcIndex_vec, area, dist生成childCircle
ChildCircle(ParentCircle P, std::vector<int> r_i_v, double area, double d)
: parentCircle(P), rsrcIndex_vec(r_i_v) {
this->r = (sqrt(area/PI));
this->dist = d;
// 得到坐标
reviseCircleFromRsrcIndex(r_i_v);
}
// 根据当前数据, 更新子空间
void updateChildCircle();
void initResourceIndex() {
// 当前c对P所占的u弧度
double radian = getRadian(parentCircle, false);
// 当前圆所占资源数量
int resourceOccupyCount = radian/unitRadian + 1;
std::cout << "resourceOccupyCount = " << resourceOccupyCount << std::endl;
}
ParentCircle getParentCircle() {return parentCircle;}
// maybe error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
std::vector<int> getResourceIndex() {return rsrcIndex_vec;}
int getResourceOccupyCount() {
// 当前c对P所占的u弧度
double radian = getRadian(parentCircle, false);
// 当前圆所占资源数量
this->resourceOccupyCount = radian/unitRadian + 1;
return this->resourceOccupyCount;
}
// 得到圆心准确角度
double getPointAngle();
// 根据ParentCircle, rsrcIndex_vec, dist, 得到当前circle
void reviseCircleFromRsrcIndex(std::vector<int>&);
// 根据当前Circle位置, parentCircle 得到当前rsrcIndex_vec, dist
void getCircleDataFromParent(ParentCircle new_p);
ParentCircle parentCircle; // 该子空间的父空间
// 每个子空间到父空间的距离
double dist;
// 子空间所占父空间资源数量
int resourceOccupyCount;
// 子空间所占父空间资源编号
std::vector<int> rsrcIndex_vec = {};
// 圆心对应角度
double pointAngle;
private:
};
// 得到圆心准确角度
double ChildCircle::getPointAngle() {
// std::cout << "---- into getPointAngle ---" << std::endl;
pointAngle = rsrcIndex_vec[0] * unitRadian
+ getRadian(parentCircle, false) / 2;
return pointAngle;
}
// 根据ParentCircle, rsrcIndex_vec, dist, 得到当前circle
void ChildCircle::reviseCircleFromRsrcIndex(std::vector<int>& vec) {
// vec = {0, 1, 2};
this->rsrcIndex_vec = vec;
double angle = getPointAngle();
// std::cout << "angle = " << angle << std::endl;
// std::cout << this->x << " " << this->y << " " << this->r << std::endl;
this->x = this->parentCircle.x + cos(angle) * dist;
this->y = this->parentCircle.y + sin(angle) * dist;
// std::cout << this->x << " " << this->y << " " << this->r << std::endl;
}
// 根据当前Circle位置, parentCircle 得到当前rsrcIndex_vec, dist
void ChildCircle::getCircleDataFromParent(ParentCircle new_P) {
// std::cout << "---- into getCircleDataFromParent() ----" << std::endl;
// 当前P修改为新父空间
this->parentCircle = new_P;
std::vector<int> new_rsrcVec = {};
double angle = getDirection(this->parentCircle, false);
int first_i = angle / this->unitRadian;
for (int i = 0; i < this->resourceOccupyCount; i++) {
// std::cout << first_i << " ";
new_rsrcVec.push_back(first_i++);
}
// std::cout << std::endl;
// 更新资源列表
this->rsrcIndex_vec = new_rsrcVec;
// 修改距离
this->dist = getDistance(parentCircle);
// std::cout << "dist = " << this->dist << std::endl;
// std::cout << "---- end getCircleDataFromParent() ----" << std::endl;
}