-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry.h
166 lines (122 loc) · 3.64 KB
/
geometry.h
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
#include <cstdlib>
#include <iostream>
#include <cassert>
#ifndef LAB1_GEOMETRY_H
#define LAB1_GEOMETRY_H
using namespace std;
const double EPS = 1e-5;
class Point {
public:
// default constructor
explicit Point(int x = 0, int y = 0) : x(x), y(y) {}
// copy constructor
Point(const Point &other) = default;
// destructor
~Point() = default;
// copy assign operator
Point &operator=(const Point &other) = default;
// perimeter or area can't be computed
int getX() const;
int getY() const;
friend ostream &operator<<(ostream &stream, const Point &point);
private:
int x, y;
};
ostream &operator<<(ostream &stream, const Point &point);
double getDistanceBetweenPoints(const Point &p1, const Point &p2);
int productSign(const Point &a, const Point &b, const Point &c);
int productSign(const Point &a, const Point &b, const Point &c, const Point &d);
double angle(const Point &a, const Point &b, const Point &c);
class PolygonalChain {
public:
// default constructor
explicit PolygonalChain(size_t numberOfPoints, const Point * points)
: numberOfPoints(numberOfPoints), points(new Point[numberOfPoints])
{
for (size_t i = 0; i < numberOfPoints; ++i) {
this->points[i] = points[i];
}
}
// copy constructor
PolygonalChain(const PolygonalChain &other)
: numberOfPoints(other.numberOfPoints), points(new Point[numberOfPoints])
{
for (size_t i = 0; i < numberOfPoints; ++i) {
points[i] = other.points[i];
}
}
// copy assign operator
PolygonalChain &operator=(const PolygonalChain &other)
{
if (this != &other) {
delete [] points;
numberOfPoints = other.numberOfPoints;
points = new Point[numberOfPoints];
for (size_t i = 0; i < numberOfPoints; ++i) {
points[i] = other.points[i];
}
}
return *this;
}
// destructor
~PolygonalChain() { delete [] points; }
// other methods
size_t getNumberOfPoints() const;
virtual double getPerimeter() const;
const Point &getPoint(size_t point) const;
friend ostream &operator<<(ostream &stream, const PolygonalChain &polygonalChain);
private:
size_t numberOfPoints;
Point * points;
};
ostream &operator<<(ostream &stream, const PolygonalChain &polygonalChain);
class ClosedPolygonalChain : public PolygonalChain {
public:
explicit ClosedPolygonalChain(size_t numberOfPoints, const Point * points)
: PolygonalChain(numberOfPoints, points)
{}
double getPerimeter() const override;
};
class Polygon : public ClosedPolygonalChain {
public:
explicit Polygon(size_t numberOfPoints, const Point * points)
: ClosedPolygonalChain(numberOfPoints, points)
{
assert(isConvex());
}
virtual double getArea() const;
private:
bool isConvex() const;
};
class Triangle : public Polygon {
public:
explicit Triangle(const Point * points)
: Polygon(3, points)
{}
bool hasRightAngle() const;
double getArea() const override;
};
class Trapezoid : public Polygon {
public:
explicit Trapezoid(const Point * points)
: Polygon(4, points)
{
assert(isTrapezoid());
}
double getHeight() const;
double getArea() const override;
private:
bool isTrapezoid() const;
};
class RegularPolygon : public Polygon {
public:
explicit RegularPolygon(const Point * points)
: Polygon(4, points)
{
assert(isRegularPolygon());
}
double getArea() const override;
private:
bool isRegularPolygon() const;
};
#endif