-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
352 lines (340 loc) · 8.93 KB
/
main.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
class Point {
private:
double x;
double y;
public:
explicit Point (double x_ = 0, double y_ = 0) : x(x_), y(y_) {}
Point (const Point &other) {
x = other.x;
y = other.y;
}
Point& operator = (const Point &other) {
if (this == &other) {
return *this;
}
x = other.x;
y = other.y;
return *this;
}
void show () const {
cout << x << " " << y << endl;
}
[[nodiscard]] double getX () const {
return x;
}
[[nodiscard]] double getY () const {
return y;
}
};
void build (vector<Point> &tmp, int n) {
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
tmp.emplace_back(Point(x, y));
}
cout << endl;
}
class Line {
protected:
vector<Point> points;
public:
explicit Line (const vector<Point> &points_) : points(points_) {}
Line () = default;
Line (const Line &other) {
this->points = other.points;
}
Line& operator = (const Line &other) {
if (this == &other) {
return *this;
}
this->points = other.points;
return *this;
}
void show () const {
cout << "\nHave " << points.size() << " points:\n";
for (const auto &point : points) {
point.show();
}
cout << '\n';
}
};
class LockedLine : public Line {
private:
double P = -1;
double countP() {
int n = (int) points.size();
double p = 0;
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
p += dist(i, j);
}
return p;
}
protected:
double dist (int i, int j) {
double x1 = points[i].getX(), y1 = points[i].getY();
double x2 = points[j].getX(), y2 = points[j].getY();
return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
public:
explicit LockedLine (vector<Point> &points_) : Line(points_), P(countP()) {}
LockedLine () = default;
LockedLine (const LockedLine &other) : Line(other) {
this->P = other.P;
}
LockedLine& operator = (const LockedLine &other) {
if (this == &other) {
return *this;
}
Line::operator = (other);
this->P = other.P;
return *this;
}
void itsP () const {
cout << "P: " << P << endl;
}
virtual void itsAll () const {
show();
itsP();
}
};
class Polygon : public LockedLine {
private:
double S = -1;
double countS () {
double s = 0;
int n = (int) points.size();
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
double x1 = points[i].getX(), y1 = points[i].getY();
double x2 = points[j].getX(), y2 = points[j].getY();
s += x1 * y2;
s -= x2 * y1;
}
return abs(s / 2);
}
bool itsConvex () {
int n = (int) points.size();
if (n < 3) {
return false;
}
bool sign1, sign;
for (int i = 0; i < n; i++) {
int j = (n + i - 1) % n, k = (n + i + 1) % n;
double x1 = points[j].getX(), y1 = points[j].getY();
double x2 = points[i].getX(), y2 = points[i].getY();
double x3 = points[k].getX(), y3 = points[k].getY();
Point ab (x2 - x1, y2 - y1);
Point bc (x3 - x2, y3 - y2);
double cnt = ab.getX() * bc.getY() - ab.getY() * bc.getX();
if (cnt == 0) {
continue;
}
sign = (cnt > 0);
if (i == 0) {
sign1 = (cnt >= 0);
}
if (sign != sign1) {
return false;
}
}
return true;
}
bool itsOneLine () {
int n = (int) points.size();
if (n < 3) {
return true;
}
double x1 = points[0].getX(), y1 = points[0].getY();
double x2 = points[1].getX(), y2 = points[1].getY();
for (int i = 2; i < n; i++) {
double x = points[i].getX(), y = points[i].getY();
if ((y1 - y2) * x + (x2 - x1) * y + x1 * y2 - x2 * y1 != 0) {
return false;
}
}
return true;
}
public:
explicit Polygon (vector<Point> &points_) : LockedLine(points_) {
if (!itsConvex()) {
cout << "Incorrect data: polygon isn't convex" << endl;
}
else if (itsOneLine()) {
cout << "Incorrect data: all points on the same line" << endl;
}
else {
S = countS();
}
}
Polygon () = default;
Polygon (const Polygon &other) : LockedLine(other) {
this->S = other.S;
}
Polygon& operator = (const Polygon &other) {
if (this == &other) {
return *this;
}
LockedLine::operator=(other);
this->S = other.S;
return *this;
}
void itsS () const {
cout << "S: " << S << endl;
}
void itsAll () const override {
LockedLine::itsAll();
itsS();
}
};
class Triangle : public Polygon {
private:
bool itsTriangle() {
if (points.size() != 3) {
return false;
}
return true;
}
public:
explicit Triangle (vector<Point> &points_) : Polygon(points_) {
if (!itsTriangle()) {
cout << "Incorrect data: not 3 vertices" << endl;
}
}
Triangle () = default;
};
class Trapeze : public Polygon {
private:
bool itsTrapeze () {
int n = (int) points.size();
if (n != 4) {
return false;
}
vector<double> k(n);
for (int i = 0; i < n; i++) {
double x1 = points[i].getX(), y1 = points[i].getY();
double x2 = points[(i + 1) % n].getX(), y2 = points[(i + 1) % n].getY();
k[i] = (y2 - y1) / (x2 - x1);
}
if (!((k[0] == k[2] && k[1] != k[3]) || (k[0] != k[2] && k[1] == k[3]))) {
return false;
}
return true;
}
public:
explicit Trapeze (vector<Point> &points_) : Polygon(points_) {
if (!itsTrapeze()) {
cout << "Incorrect data: not 4 vertices / not trapeze" << endl;
}
}
Trapeze () = default;
};
class RegularPolygon : public Polygon {
private:
bool itsRegularSide () {
int n = (int) points.size();
double last, now;
last = dist(0, 1);
for (int i = 1; i < n; i++) {
int j = (i + 1) % n;
now = dist(i, j);
if (last != now) {
return false;
}
else {
last = now;
}
}
return true;
}
static double deg (double a, double b, double c) {
return acos((pow(a, 2) + pow(b, 2) - pow(c, 2)) / (2 * a * b)) * 180 / M_PI;
}
bool itsRegularDeg() {
int n = (int) points.size();
double deg1 = deg(dist(0, 1), dist(1, 2), dist(0, 2));
for (int i = 1; i < n; i++) {
int j = (i + 1) % n;
int k = (i + 2) % n;
double deg2 = deg(dist(i, j), dist(j, k), dist(i, k));
if (deg1 != deg2) {
return false;
}
}
return true;
}
public:
explicit RegularPolygon (vector<Point> &points_) : Polygon(points_) {
if (!itsRegularSide()) {
cout << "Incorrect data: different sides" << endl;
}
if (!itsRegularDeg()) {
cout << "Incorrect data: different angles" << endl;
}
}
RegularPolygon () = default;
};
int main() {
while (true) {
cout << '\n';
for (int i = 0; i < 20; i++) {
cout << "#";
}
cout << "\nChoose:\n"
"1. Point\n"
"2. Line\n"
"3. Locked line\n"
"4. Polygon\n"
"5. Triangle\n"
"6. Trapeze\n"
"7. Regular polygon\n"
"0. Out\n\n";
int press;
cin >> press;
int n;
vector<Point> tmp;
int x, y;
if (press == 1) {
cin >> x >> y;
Point my(x, y);
my.show();
}
else if (press >= 2 && press <= 7) {
cout << "Number of points:\n";
cin >> n;
build(tmp, n);
}
if (press == 2) {
Line my (tmp);
my.show();
}
else if (press == 3) {
LockedLine my (tmp);
my.itsAll();
}
else if (press == 4) {
Polygon my (tmp);
my.itsAll();
}
else if (press == 5) {
Triangle my (tmp);
my.itsAll();
}
else if (press == 6) {
Trapeze my (tmp);
my.itsAll();
}
else if (press == 7) {
RegularPolygon my (tmp);
my.itsAll();
}
else if (press != 1) {
break;
}
}
}