-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle.cpp
91 lines (81 loc) · 2.36 KB
/
circle.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
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
#include "circle.h"
#include <cmath>
Circle::Circle() {
type = CIRCLE;
}
Circle::Circle(QPoint center, int radius) {
type = CIRCLE;
this->center = center;
this->radius = radius;
transformCenter = center;
set_LTRB();
}
Circle::Circle(QPoint point1, QPoint point2) {
type = CIRCLE;
center = QPoint((point1.x() + point2.x()) / 2, (point1.y() + point2.y()) / 2);
int delta_x = point1.x() - point2.x(), delta_y = point1.y() - point2.y();
radius = (int)(pow(delta_x * delta_x + delta_y * delta_y, 0.5)) / 2;
set_LTRB();
}
void Circle::set_LTRB() {
LTPoint.setX(center.x() - radius - 1);
LTPoint.setY(center.y() - radius - 1);
RBPoint.setX(center.x() + radius + 1);
RBPoint.setY(center.y() + radius + 1);
}
void Circle::draw(QPen& pen, QPixmap& pix) {
int x = 0;
int y = radius;
int e = 1 - radius;
int delta1 = 5 - 2 * radius;
int delta2 = 3;
while(x <= y) {
set_Pixel(x,y,pen,pix);
if(e > 0) {
x += 1;
y -= 1;
e += delta1;
delta1 += 4;
delta2 += 2;
}
else {
x += 1;
e += delta2;
delta1 += 2;
delta2 += 2;
}
}
}
void Circle::set_Pixel(int x, int y, QPen &pen, QPixmap &pix) {
QPainter painter(&pix);
painter.setPen(pen);
painter.drawPoint(x + center.x(), y + center.y());
painter.drawPoint(x + center.x(), -y + center.y());
painter.drawPoint(-x + center.x(), y + center.y());
painter.drawPoint(-x + center.x(), -y + center.y());
painter.drawPoint(y + center.x(), x + center.y());
painter.drawPoint(y + center.x(), -x + center.y());
painter.drawPoint(-y + center.x(), x + center.y());
painter.drawPoint(-y + center.x(), -x + center.y());
}
Circle::~Circle() {
center.setX(0);
center.setY(0);
radius = 0;
}
void Circle::translate(int dx, int dy) {
center = translate_Point(dx, dy, center);
transformCenter = center;
set_LTRB();
}
void Circle::rotate(int x, int y, int r) {
center = rotate_Point(x, y, r, center);
transformCenter = center;
set_LTRB();
}
void Circle::scale(int x, int y, float s) {
center = scale_Point(x, y, s, center);
radius = radius * s;
transformCenter = center;
set_LTRB();
}