-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShapes.h
220 lines (219 loc) · 4.9 KB
/
Shapes.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
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
#pragma once
#include"Container.h"
#include"iostream"
#include"algorithm"
#include"math.h"
#include"Base.h"
const double Pi = 3.14159;
class Shape : public Printable
{
public:
static double Counter;
Shape()
{
Counter++;
}
Shape(const Shape& shape)
{
Counter++;
}
static double GetCount()
{
return Counter;
}
std::string Print() const { return "Shape"; }
~Shape()
{
Counter--;
}
};
double Shape::Counter = 0;
class Point : public Shape, public Named
{
public:
double ox, oy;
Point(): Named("Point")
{
ox = 0;
oy = 0;
}
Point(const double &x, const double &y):Named("Point")
{
ox = x;
oy = y;
}
std::string Print() const
{
return "\n" + this->GetName() + "\nox:" + std::to_string(this->ox) + "\noy:" + std::to_string(this->oy) + "\n";
}
friend std::ostream& operator<<(std::ostream& os, Point& point)
{
return os << point.Print();
}
};
class Circle : public Shape, public Named
{
public:
double ox, oy, radius;
Circle():Named("Circle")
{
ox = 0;
oy = 0;
radius = 0;
}
Circle(const double &x, const double &y, const double &r) :Named("Circle")
{
ox = x;
oy = y;
radius = r;
}
std::string Print() const
{
return "\n" + this->GetName() + "\nCenter Ox:" + std::to_string(this->ox) + "\nCenter Oy:" + std::to_string(this->oy)
+ "\nRadius:" + std::to_string(this->radius) + "\nArea of Circle:" + std::to_string(this->radius * this->radius * Pi) + "\n";;
}
friend std::ostream& operator<<(std::ostream& Os, Circle& circle)
{
return Os << circle.Print();
}
};
class Rect : public Shape, public Named
{
public:
Point Point1, Point2;
Rect(): Named("Rectangle")
{
Point1 = Point();
Point2 = Point();
}
Rect(const double &LeftX, const double &LeftY, const double &RightX,
const double &RightY): Named("Rectangle")
{
Point1 = Point(LeftX, LeftY);
Point2 = Point(RightX, RightY);
}
std::string Print() const
{
return "\n" + this->GetName()
+ "\nbig side length:" + std::to_string(std::max(fabs(this->Point1.ox - this->Point2.ox), fabs(this->Point1.oy - this->Point2.oy)))
+ "\nsmall side length:" + std::to_string(std::min(fabs(this->Point1.ox - this->Point2.ox), fabs(this->Point1.oy - this->Point2.oy)))
+ "\narea:" + std::to_string(fabs(this->Point1.ox - this->Point2.ox)*fabs(this->Point1.oy - this->Point2.oy)) + "\n";
}
friend std::ostream& operator<<(std::ostream& Os, Rect& Rectangle)
{
return Os << Rectangle.Print();
}
};
class Square : public Shape, public Named
{
public:
Point LeftPoint;
double Length;
Square(): Named("Square")
{
LeftPoint = Point();
Length = 0;
}
Square(const double &LeftX, const double &LeftY, const double & length): Named("Square")
{
LeftPoint = Point(LeftX, LeftY);
Length = length;
}
std::string Print() const
{
return "\n" + this->GetName() + "\nside length:" + std::to_string(this->Length) + "\narea:" + std::to_string(pow(this->Length, 2)) + "\n";
}
friend std::ostream& operator<<(std::ostream& Os, Square& square)
{
return Os <<square.Print();
}
};
class Polyline : public Shape, public Named
{
public:
Container<Point> Points;
double Length;
Polyline(): Named("Polyline")
{
Points.AddFirst(Point());
Length = 0;
}
Polyline(Container<Point>& dots, const int& size): Named("Polyline")
{
Length = 0;
dots.SetToFirst();
AddPoint(dots.GetFirst());
for (int i = 1; i < size; i ++)
{
AddPoint(*dots.GetAndMove());
}
}
void AddPoint(const Point& point)
{
if (this->Points.IsEmpty())
{
this->Points.AddLast(point);
return;
}
else
{
double dotX = this->Points.GetLast().ox;
double dotY = this->Points.GetLast().oy;
this->Points.AddLast(point);
this->Length += sqrt(pow(dotX - point.ox, 2) + pow(dotY - point.oy, 2));
}
}
std::string Print() const
{
return "\n" + this->GetName() + "\nlength:" + std::to_string(this->Length) + "\n";
}
friend std::ostream& operator<<(std::ostream& Os, Polyline& Line)
{
return Os <<Line.Print();
}
};
class Polygone : public Shape, public Named
{
public:
Container<Point> Points;
double Length;
Polygone(): Named("Polygone")
{
Points.AddFirst(Point());
Length = 0;
}
Polygone(Container<Point>& dots, const int& size): Named("Polygone")
{
Length = sqrt(pow(dots.GetFirst().ox - dots.GetLast().ox, 2) +
pow(dots.GetFirst().oy - dots.GetLast().oy, 2));
dots.SetToFirst();
AddPoint(dots.GetFirst());
for (int i = 1; i < size ; i ++)
{
AddPoint(*dots.GetAndMove());
}
}
void AddPoint(const Point& point)
{
if (this->Points.IsEmpty())
{
this->Points.AddLast(point);
return;
}
else
{
double dotX = this->Points.GetLast().ox;
double dotY = this->Points.GetLast().oy;
this->Points.AddLast(point);
this->Length += sqrt(pow(dotX - point.ox, 2) + pow(dotY - point.oy, 2));
}
}
std::string Print() const
{
return "\n" + this->GetName() + "\nperimeter:" + std::to_string(this->Length) + "\n";
}
friend std::ostream& operator<<(std::ostream& Os, Polygone& Ngon)
{
return Os << Ngon.Print();
}
};