-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShape.h
261 lines (230 loc) · 7.39 KB
/
Shape.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
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
#pragma once
#include <string>
#include <iostream>
#include <sstream>
#include <cmath>
#include "Printable.h"
#include "Named.h"
#include "My_forward_list.h"
#include "error_types.h"
class Shape: public Printable
{
public:
virtual ~Shape()
{
sm_counter--;
}
virtual std::string get_info() const = 0;
static int get_counter()
{
return sm_counter;
}
protected:
Shape()
{
sm_counter++;
}
private:
static int sm_counter;//=0
};
class Point: public Shape, public Named
{
public:
Point(double _x, double _y): Named("Point"), m_x(_x), m_y(_y)
{
}
Point(Point const& orig): Named("Point"), m_x(orig.m_x), m_y(orig.m_y)
{
}
virtual std::string get_info() const
{
std::stringstream ss;
ss << get_name() << "\nCoordinates: (" << m_x << ", " << m_y << ")\n";
return ss.str();
}
double get_x() const
{
return m_x;
}
double get_y() const
{
return m_y;
}
friend double get_dist(Point const& a, Point const& b)
{
return sqrt((a.m_x-b.m_x)*(a.m_x-b.m_x)+(a.m_y-b.m_y)*(a.m_y-b.m_y));
}
friend std::ostream& operator << (std::ostream &s, Point const &a)
{
return s << a.get_info();
}
private:
double m_x, m_y;
};
class Circle: public Shape, public Named
{
public:
Circle(Point const& c, double _r) throw(Shape_errors): Named("Circle"), m_cen(c), m_r(_r)
{
if(m_r<=0)
throw WRONG_PARAMETERS_FOR_CIRCLE;
}
virtual std::string get_info() const
{
std::stringstream ss;
ss << get_name() << "\nCenter coordinates: (" << m_cen.get_x() << ", " << m_cen.get_y() << ")\nR=" << m_r << "\nL=" << 2*M_PI*m_r << "\nS=" << M_PI*m_r*m_r << "\n";
return ss.str();
}
friend std::ostream& operator << (std::ostream &s, Circle const &a)
{
return s << a.get_info();
}
private:
Point m_cen;
double m_r;
};
class Rect: public Shape, public Named
{
public:
Rect(Point const& _a, Point const& _b) throw(Shape_errors): Named("Rect"), m_a(_a), m_b(_b)
{
if(m_a.get_x()==m_b.get_x() || m_a.get_y()==m_b.get_y())
throw WRONG_PARAMETERS_FOR_RECT;
}
virtual std::string get_info() const//вывести b, d
{
std::stringstream ss;
ss << get_name() << "\nCoordinates s: a=(" << m_a.get_x() << ", " << m_a.get_y() << "), c=(" << m_b.get_x() << ", " << m_b.get_y() << ")\n";
ss << "L=" << 2*fabs(m_a.get_x()-m_b.get_x())+2*fabs(m_a.get_y()-m_b.get_y()) << "\n";
ss << "S=" << fabs((m_a.get_x()-m_b.get_x())*(m_a.get_y()-m_b.get_y())) << "\n";
return ss.str();
}
friend std::ostream& operator << (std::ostream &s, Rect const &a)
{
return s << a.get_info();
}
private:
Point m_a, m_b;
};
class Square: public Shape, public Named
{
public:
Square(Point const& _a, Point const& _b) throw(Shape_errors): Named("Square"), m_a(_a), m_b(_b)
{
if(m_a.get_x()==m_b.get_x() && m_a.get_y()==m_b.get_y())
throw WRONG_PARAMETERS_FOR_SQUARE;
}
virtual std::string get_info() const
{
std::stringstream ss;
/*
* чисто аналит. геометрия
* Да, формулы скучные, не отличные от формул в Rect
*/
ss << get_name() << "\nCoordinates s: a=(" << m_a.get_x() << ", " << m_a.get_y() <<
"), b=(" << (m_a.get_x()+m_b.get_x())/2 + (m_a.get_y()-m_b.get_y())/2 << ", " << (m_a.get_y()+m_b.get_y())/2 - (m_a.get_x()-m_b.get_x())/2 <<
"), c=(" << m_b.get_x() << ", " << m_b.get_y() <<
"), d=(" << (m_a.get_x()+m_b.get_x())/2 - (m_a.get_y()-m_b.get_y())/2 << ", " << (m_a.get_y()+m_b.get_y())/2 + (m_a.get_x()-m_b.get_x())/2 << ")\n";//восстанавливаем еще 2 точки
ss << "L=" << 2.0/sqrt(0.5)*sqrt(fabs((m_a.get_x()-m_b.get_x())*(m_a.get_x()-m_b.get_x()) + (m_a.get_y()-m_b.get_y())*(m_a.get_y()-m_b.get_y()))) << "\n";
ss << "S=" << 0.5*((m_a.get_x()-m_b.get_x())*(m_a.get_x()-m_b.get_x()) + (m_a.get_y()-m_b.get_y())*(m_a.get_y()-m_b.get_y())) << "\n";
return ss.str();
}
friend std::ostream& operator << (std::ostream &s, Square const &a)
{
return s << a.get_info();
}
private:
Point m_a, m_b;
};
class Polyline : public Shape, public Named
{
public:
Polyline(): Named("Polyline"), m_points()
{
}
Polyline(My_forward_list<Point> const &l): Named("Polyline"), m_points(l)
{
}
virtual std::string get_info() const
{
std::stringstream ss;
ss << get_name() << '\n';
if(m_points.is_empty())
{
ss << "Vershin net\nL=0\n";
}
else if(m_points.get_element_number()==1)
{
ss << "Vershini:\n(" << (*m_points.get_beginning()).get_x() << ',' << (*m_points.get_beginning()).get_y()<< ')' << "\nL=0\n";
}
else
{
ss << "Vershini:\n";
MyForwardIterator<Point> i=m_points.get_beginning(), j=m_points.get_beginning()++;
double len=0;
while(j!=m_points.get_ending())
{
ss << '(' << (*i).get_x() << ',' << (*i).get_y()<< ')' << '\n';
i++;
j++;
len+=get_dist(*i, *j);
}
ss << '(' << (*i).get_x() << ',' << (*i).get_y()<< ')' << '\n';
ss << "L=" << len << '\n';
}
return ss.str();
}
friend std::ostream& operator << (std::ostream &s, Polyline const &a)
{
return s << a.get_info();
}
void add_point(Point const& p)
{
m_points.add_in_ending(p);
}
private:
My_forward_list<Point> m_points;
};
class Polygon: public Shape, public Named
{
public:
Polygon(My_forward_list<Point> const &l): Named("Polygon"), m_points(l)
{
}
virtual std::string get_info() const
{
std::stringstream ss;
ss << get_name() << '\n';
if(m_points.is_empty())
{
ss << "Vershin net\nL=0\n";
}
else if(m_points.get_element_number()==1)
{
ss << "Vershini:\n(" << (*m_points.get_beginning()).get_x() << ',' << (*m_points.get_beginning()).get_y()<< ')' << "\nL=0\n";
}
else
{
ss << "Vershini:\n";
MyForwardIterator<Point> i=m_points.get_beginning(), j=m_points.get_beginning()++;
double len=0;
while(j!=m_points.get_ending())
{
ss << '(' << (*i).get_x() << ',' << (*i).get_y()<< ')' << '\n';
i++;
j++;
len+=get_dist(*i, *j);
}
len+=get_dist(*m_points.get_beginning(), *m_points.get_ending());
ss << '(' << (*i).get_x() << ',' << (*i).get_y()<< ')' << '\n';
ss << "L=" << len << '\n';
}
return ss.str();
}
friend std::ostream& operator << (std::ostream &s, Polygon const &a)
{
return s << a.get_info();
}
private:
My_forward_list<Point> m_points;
};