-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare.cpp
61 lines (60 loc) · 1.91 KB
/
square.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
// Киреев Александр Константинович 206
#include "square.hpp"
#include "cmath"
#define _USE_MATH_DEFINES
Square::Square(const Dot& center, const Dot& d) :
_center(center), _d(d) {
_type = "Square";
}
Square::Square(const Square& s) {
this->_center = s._center;
this->_d = s._d;
this->_type = s._type;
}
Square::Square(std::istream& is) {
is >> this->_center >> this->_d;
_type = "Square";
}
Dot Square::getCenter() const {
return this->_center;
}
void Square::print(std::ostream& os) const {
std::vector<Dot> dots;
this->getCoords(dots);
os << "[ " << dots[0];
for (int i = 1; i < dots.size(); ++i) {
os << ", " << dots[i];
}
os << " ]";
}
// будем считать площадь через описанную окружность, тк порядок точек фигуры не влияет на ее результат
double Square::getArea() const {
double r = sqrt((this->_d.X - this->_center.X) * (this->_d.X - this->_center.X) +
(this->_d.Y - this->_center.Y) * (this->_d.Y - this->_center.Y));
return 2.0 * r * r;
}
std::string Square::getType() const {
return _type;
}
void Square::getCoords(std::vector<Dot>& dots) const {
double r = sqrt((this->_d.X - this->_center.X) * (this->_d.X - this->_center.X) +
(this->_d.Y - this->_center.Y) * (this->_d.Y - this->_center.Y));
double lenX = this->_d.X - this->_center.X;
double lenY = this->_d.Y - this->_center.Y;
double phi = acos(lenX / r);
if (lenX < 0) {
if (lenY < 0)
phi = -M_PI + phi;
} else {
if (lenY < 0)
phi = -phi;
}
for (int i = 0; i < 4; ++i) {
double angle = 2.0 * M_PI * i / 4.0;
dots.push_back(Dot(r * cos(angle + phi) + this->_center.X, r * sin(angle + phi) + this->_center.Y));
}
}
std::ostream& operator<<(std::ostream& os, const Square& s) {
s.print(os);
return os;
}