This repository was archived by the owner on Jan 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCCircle.cpp
More file actions
94 lines (76 loc) · 2.56 KB
/
CCircle.cpp
File metadata and controls
94 lines (76 loc) · 2.56 KB
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
#include "CCircle.h"
#include <cmath>
CCircle::CCircle(float x, float y, float radius, const CColor &col, int lt, bool isFilled) :
CShape2D(col, lt, isFilled), center(x, y), radius(radius) {}
const CPoint2D &CCircle::getCenter() const {
return center;
}
void CCircle::setCenter(const CPoint2D ¢er) {
CCircle::center = center;
}
float CCircle::getRadius() const {
return radius;
}
void CCircle::setRadius(float radius) {
CCircle::radius = radius;
}
std::ostream &CCircle::print(std::ostream &os) const {
CShape2D::print(os);
os << "center: [" << center << "]; radius: " << radius << "; ";
return os;
}
void CCircle::scale(float factor) {
radius *= factor;
}
void CCircle::move(const CPoint2D &loc) {
center.add(loc);
}
float CCircle::area() const {
return pow(radius, 2) * M_PI;
}
void CCircle::render(CCanvas &ref) const {
int r = radius;
int cX = center.getX();
int cY = center.getY();
double ratio = 6/3;
std::string stroke = colorString + CCanvas::circleOut + COLOR_RESET;
std::string fill = colorString + CCanvas::fill + COLOR_RESET;
for (int x = 0; x < r * ratio / M_SQRT2; x++) {
int y = (int)round(sqrt(pow(r, 2) - pow(x / ratio, 2)));
ref.writeChecked(cX - x, cY - y, stroke);
ref.writeChecked(cX - x, cY + y, stroke);
ref.writeChecked(cX + x, cY - y, stroke);
ref.writeChecked(cX + x, cY + y, stroke);
if (!isFilled) continue;
while (y-- > 0) {
ref.writeChecked(cX - x, cY - y, fill);
ref.writeChecked(cX - x, cY + y, fill);
ref.writeChecked(cX + x, cY - y, fill);
ref.writeChecked(cX + x, cY + y, fill);
}
}
for (int y = 0; y < r / M_SQRT2; y++) {
int x = ratio * (int)round(sqrt(pow(r, 2) - pow(y, 2)));
ref.writeChecked(cX - x, cY - y, stroke);
ref.writeChecked(cX - x, cY + y, stroke);
ref.writeChecked(cX + x, cY - y, stroke);
ref.writeChecked(cX + x, cY + y, stroke);
if (!isFilled) continue;
while (x-- > 0) {
ref.writeChecked(cX - x, cY - y, fill);
ref.writeChecked(cX - x, cY + y, fill);
ref.writeChecked(cX + x, cY - y, fill);
ref.writeChecked(cX + x, cY + y, fill);
}
}
//center
ref.writeChecked(cX, cY, colorString + CCanvas::circleCenter + COLOR_RESET);
}
std::istream &CCircle::read(std::istream &is) {
CShape2D::read(is);
std::cout << "Radius: ";
std::cin >> radius;
std::cout << "Center point:\n";
std::cin >> center;
return is;
}