-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoval.cpp
More file actions
183 lines (169 loc) · 5.76 KB
/
oval.cpp
File metadata and controls
183 lines (169 loc) · 5.76 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
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
#include "oval.h"
#include <cmath>
Oval::Oval() {
type = OVAL;
rotateDegrees = 0;
}
Oval::Oval(QPoint point1, QPoint point2) {
type = OVAL;
center = QPoint((point1.x() + point2.x()) / 2, (point1.y() + point2.y()) / 2);
transformCenter = center;
int delta_x = abs(point1.x() - point2.x());
int delta_y = abs(point1.y() - point2.y());
a = delta_x / 2;
b = delta_y / 2;
rotateDegrees = 0;
set_LTRB();
}
Oval::Oval(QPoint center, int a, int b) {
type = OVAL;
this->center = center;
transformCenter = center;
this->a = a;
this->b = b;
rotateDegrees = 0;
set_LTRB();
}
void Oval::set_LTRB() {
if(rotateDegrees == 0) {
LTPoint.setX(center.x() - a - 1);
LTPoint.setY(center.y() - b - 1);
RBPoint.setX(center.x() + a + 1);
RBPoint.setY(center.y() + b + 1);
}
}
void Oval::draw(QPen& pen, QPixmap& pix) {
if(a == 0 && b == 0)
return;
int x = 0;
int y = b;
/*
为了保证计算过程中只有整数出现,
所以将参数p和增量都乘了4
*/
int p_area1 = 4 * b * b + a * a - 4 * a * a * b;
int delta1 = 8 * a * a + 12 * b * b - 8 * a * a * b;
int delta2 = 12 * b * b;
//area1
set_Pixel(x, y, pen, pix);
while(b * b * x <= a * a * y) {
if(p_area1 >= 0) {
x += 1;
y -= 1;
p_area1 += delta1;
delta1 = delta1 + 8 * b * b + 8 * a * a;
delta2 = delta2 + 8 * b * b;
}
else {
x++;
p_area1 += delta2;
delta1 += 8 * b * b;
delta2 += 8 * b * b;
}
set_Pixel(x, y, pen, pix);
}
//area2
int p_area2 = 4 * b * b * x * x + 4 * b * b * x + b * b + 4 * a * a * y * y
- 8 * a * a * y + 4 * a * a - 4 * a * a * b * b;
delta1 = 8 * b * b * x - 8 * a * a * y
+ 8 * b * b + 12 * a * a;
delta2 = -8 * a * a * y + 12 * a * a;
while(y >= 0) {
if(p_area2 <= 0) {
x += 1;
y -= 1;
p_area2 += delta1;
delta1 = delta1 + 8 * b * b + 8 * a * a;
delta2 = delta2 + 8 * a * a;
}
else {
y -= 1;
p_area2 += delta2;
delta1 += 8 * a * a;
delta2 += 8 * a * a;
}
set_Pixel(x, y, pen, pix);
}
}
void Oval::set_Pixel(int x, int y, QPen &pen, QPixmap &pix) {
QPainter painter(&pix);
painter.setPen(pen);
double rad = ((double)rotateDegrees * 3.14159265358) / 180;
double COS = cos(rad);
double SIN = sin(rad);
int oldX = x;
int oldY = y;
double x_double, y_double;
x_double = oldX * COS - oldY * SIN;
y_double = oldX * SIN + oldY * COS;
painter.drawPoint(QPointF(x_double + center.x(), y_double + center.y()));
painter.drawPoint(QPointF(-x_double + center.x(), -y_double + center.y()));
if(rotateDegrees != 0) {
if((int)x_double + center.x() < LTPoint.x())
LTPoint.setX((int)x_double + center.x());
if((int)x_double + center.x() > RBPoint.x())
RBPoint.setX((int)x_double + center.x());
if((int)y_double + center.y() < LTPoint.y())
LTPoint.setY((int)y_double + center.y());
if((int)y_double + center.y() > RBPoint.y())
RBPoint.setY((int)y_double + center.y());
if((int)-x_double + center.x() < LTPoint.x())
LTPoint.setX((int)-x_double + center.x());
if((int)-x_double + center.x() > RBPoint.x())
RBPoint.setX((int)-x_double + center.x());
if((int)-y_double + center.y() < LTPoint.y())
LTPoint.setY((int)-y_double + center.y());
if((int)-y_double + center.y() > RBPoint.y())
RBPoint.setY((int)-y_double + center.y());
}
oldX = -oldX;
x_double = oldX * COS - oldY * SIN;
y_double = oldX * SIN + oldY * COS;
painter.drawPoint(QPointF(x_double + center.x(), y_double + center.y()));
painter.drawPoint(QPointF(-x_double + center.x(), -y_double + center.y()));
if(rotateDegrees != 0) {
if((int)x_double + center.x() < LTPoint.x())
LTPoint.setX((int)x_double + center.x());
if((int)x_double + center.x() > RBPoint.x())
RBPoint.setX((int)x_double + center.x());
if((int)y_double + center.y() < LTPoint.y())
LTPoint.setY((int)y_double + center.y());
if((int)y_double + center.y() > RBPoint.y())
RBPoint.setY((int)y_double + center.y());
if((int)-x_double + center.x() < LTPoint.x())
LTPoint.setX((int)-x_double + center.x());
if((int)-x_double + center.x() > RBPoint.x())
RBPoint.setX((int)-x_double + center.x());
if((int)-y_double + center.y() < LTPoint.y())
LTPoint.setY((int)-y_double + center.y());
if((int)-y_double + center.y() > RBPoint.y())
RBPoint.setY((int)-y_double + center.y());
}
}
Oval::~Oval() {
center.setX(0);
center.setY(0);
a = 0;
b = 0;
}
void Oval::translate(int dx, int dy) {
center = translate_Point(dx, dy, center);
transformCenter = center;
LTPoint = translate_Point(dx, dy, LTPoint);
RBPoint = translate_Point(dx, dy, RBPoint);
}
void Oval::rotate(int x, int y, int r) {
rotateDegrees = (rotateDegrees + r) % 360;
center = rotate_Point(x, y, r, center);
transformCenter = center;
LTPoint = RBPoint = transformCenter;
set_LTRB();
}
void Oval::scale(int x, int y, float s) {
center = scale_Point(x, y, s, center);
a = (int)(a * s);
b = (int)(b * s);
transformCenter = center;
LTPoint = RBPoint = transformCenter;
set_LTRB();
}