-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolygons.cpp
More file actions
174 lines (112 loc) · 3.07 KB
/
Polygons.cpp
File metadata and controls
174 lines (112 loc) · 3.07 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
// Polygons.cpp
#include "Polygons.h"
Polygons::Polygons( int n, double A )
{
this->A = A;
setSides( n );
plot = Plot::CreateInstance();
polygons.resize(1);
polygons[0].resize(1);
char command[128];
polygons[0][0] = new Polygon(0,0,n,A,0,1, vector<double>(2,0) );
sprintf(command, "set title \"%d Polygons (%d sides, %d tiers, %.2f (%.2fx%.2f) area, %.2f%% covered)\"", this->getNumber(), n, 1, plot->getArea(), plot->getXMax()-plot->getXMin(), plot->getYMax()-plot->getYMin(), 100*this->getArea()/plot->getArea());
plot->write( command );
plot->refresh();
int i = 1;
int index = 1;
int added = 1;
while( added )
{
polygons.resize(polygons.size()+1);
added = 0;
int maxsize = polygons[i-1].size();
for( int j = 0; j < maxsize; j++ )
{
Polygon* parent = getPolygon(i-1,j);
for( int k = 0; k < n; k++ )
{
if( parent == NULL ) continue;
if( parent->isHidden() ) continue;
index++;
vector<double> center = parent->getCenter();
vector<double> normal = parent->getNormal(k);
vector<double> vertex = parent->getVertex(k);
Polygon* child = new Polygon( center[0] + normal[0], center[1] + normal[1], n, A, i, index, normal );
bool collides = false;
for( int u = 0; u < polygons.size(); u++ )
{
for( int v = 0; v < polygons[u].size(); v++ )
{
Polygon* collide = getPolygon(u,v);
if( collide == NULL ) continue;
if( child->collidesWith(collide) ) collides = true;
if( collides ) break;
}
if( collides ) break;
}
if( collides )
{
index--;
delete child;
}
else
{
polygons[i].push_back( child );
added++;
}
}
}
sprintf(command, "set title \"%d Polygons (%d sides, %d tiers, %.2f (%.2fx%.2f) area, %.2f%% covered)\"", this->getNumber(), n, (added?i+1:i), plot->getArea(), plot->getXMax()-plot->getXMin(), plot->getYMax()-plot->getYMin(), 100*this->getArea()/plot->getArea());
plot->write( command );
plot->refresh();
i++;
}
}
Polygons::~Polygons()
{
plot->write("unset arrow");
plot->write("unset label");
plot->write("unset object");
plot->refresh();
for( int i = 0; i < polygons.size(); i++ )
for( int j = 0; j < polygons[i].size(); i++ )
delete getPolygon(i,j);
}
int Polygons::getNumber()
{
int count = 0;
for( int i = 0; i < polygons.size(); i++ )
for( int j = 0; j < polygons[i].size(); j++ )
{
Polygon* polygon = getPolygon(i,j);
if( polygon == NULL ) continue;
if( !polygon->isHidden() ) count++;
}
return count;
}
double Polygons::getArea()
{
double area = 0;
for( int i = 0; i < polygons.size(); i++ )
for( int j = 0; j < polygons[i].size(); j++ )
{
Polygon* polygon = getPolygon(i,j);
if( polygon == NULL ) continue;
if( !polygon->isHidden() )
{
double R = polygon->getOuterRadius();
area += 0.5*n*R*R*sin(2*pi/n);
}
}
return area;
}
Polygon* Polygons::getPolygon( int x, int y )
{
if( x > polygons.size() - 1 || y > polygons[x].size() - 1 )
return NULL;
return polygons[x][y];
}
void Polygons::setSides( int n )
{
this->n = n;
}