-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderarea.cpp
295 lines (236 loc) · 5.83 KB
/
renderarea.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "renderarea.h"
#include <QPaintEvent>
#include <QPainter>
#include <math.h>
RenderArea::RenderArea(QWidget *parent) :
QWidget(parent),
mBackgroundColor (0, 0, 255),
mPen (Qt::white),
mShape (Astroid)
{
mPen.setWidth(2);
on_shape_changed ();
}
QSize RenderArea::minimumSizeHint() const
{
return QSize(400, 400);
}
QSize RenderArea::sizeHint() const
{
return QSize(400, 400);
}
void RenderArea::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setBrush(mBackgroundColor);
painter.setPen(mPen);
// drawing area
painter.drawRect(this->rect());
QPoint center = this->rect().center();
QPointF prevPoint = compute (0);
QPoint prevPixel;
prevPixel.setX(prevPoint.x() * mScale + center.x());
prevPixel.setY(prevPoint.y() * mScale + center.y());
float step = mIntervalLength / mStepCount;
for (float t = 0; t < mIntervalLength; t += step) {
QPointF point = compute (t);
QPoint pixel;
pixel.setX(point.x() * mScale + center.x());
pixel.setY(point.y() * mScale + center.y());
painter.drawLine(pixel, prevPixel);
prevPixel = pixel;
}
QPointF point = compute (mIntervalLength);
QPoint pixel;
pixel.setX(point.x() * mScale + center.x());
pixel.setY(point.y() * mScale + center.y());
painter.drawLine(pixel, prevPixel);
}
void RenderArea::on_shape_changed()
{
switch (mShape) {
case Astroid:
mScale = 90;
mIntervalLength = 2 * M_PI;
mStepCount = 256;
break;
case Cycloid:
mScale = 10;
mIntervalLength = 4 * M_PI;
mStepCount = 128;
break;
case HuygensCycloid:
mScale = 12;
mIntervalLength = 4 * M_PI;
mStepCount = 256;
break;
case HypoCycloid:
mScale = 40;
mIntervalLength = 2 * M_PI;
mStepCount = 256;
break;
case Line:
mIntervalLength = 2;
mScale = 100;
mStepCount = 128;
break;
case Circle:
mScale = 165;
mIntervalLength = 2 * M_PI;
mStepCount = 128;
break;
case Ellipse:
mScale = 75;
mIntervalLength = 2 * M_PI;
mStepCount = 128;
break;
case Fancy:
mScale = 10;
mIntervalLength = 12 * M_PI;
mStepCount = 512;
break;
case Starfish:
mScale = 25;
mIntervalLength = 6 * M_PI;
mStepCount = 256;
break;
case Cloud1:
mScale = 10;
mIntervalLength = 28 * M_PI;
mStepCount = 128;
break;
case Cloud2:
mScale = 10;
mIntervalLength = 28 * M_PI;
mStepCount = 128;
break;
default:
break;
}
}
QPointF RenderArea::compute(float t)
{
switch (mShape) {
case Astroid:
return compute_astroid(t);
//break;
case Cycloid:
return compute_cycloid(t);
//break;
case HuygensCycloid:
return compute_huygens(t);
// break;
case HypoCycloid:
return compute_hypo(t);
// break;
case Line:
return compute_line(t);
// break;
case Circle:
return compute_circle(t);
// break;
case Ellipse:
return compute_ellipse(t);
//break;
case Fancy:
return compute_fancy(t);
//break;
case Starfish:
return compute_starfish(t);
//break;
case Cloud1:
return compute_cloud1 (t);
//break;
case Cloud2:
return compute_cloud2 (t);
// break;
default:
break;
}
return QPointF (0, 0);
}
QPointF RenderArea::compute_astroid(float t)
{
// we will compute the astroid function here
float cos_t = cos (t);
float sin_t = sin (t);
float x = 2 * cos_t * cos_t * cos_t; // pow (cos_t, 3);
float y = 2 * sin_t * sin_t * sin_t; // pow (sin_t, 3);
return QPointF (x, y);
}
QPointF RenderArea::compute_cycloid(float t)
{
return QPointF (
1.5 * (1 - cos (t)), //X
1.5 * (t - sin (t)) //Y
);
}
QPointF RenderArea::compute_huygens(float t)
{
return QPointF (
4 * (3 * cos (t) - cos (3 * t)), // X
4 * (3 * sin (t) - sin (3 * t)) // Y
);
}
QPointF RenderArea::compute_hypo(float t)
{
return QPointF (
1.5 * (2 * cos(t) + cos (2 * t)), //X
1.5 * (2 * sin(t) - sin(2 * t)) //Y
);
}
QPointF RenderArea::compute_line(float t)
{
return QPointF (1 - t, 1 - t);
}
QPointF RenderArea::compute_circle(float t)
{
return QPointF (
cos (t),
sin (t)
);
}
QPointF RenderArea::compute_ellipse(float t)
{
float a = 2;
float b = 1;
return QPointF (
a * cos (t),
b * sin (t)
);
}
QPointF RenderArea::compute_fancy(float t)
{
float v1 = 15;
float v2 = 4;
float x = v1 * cos (t) - v2 * cos (v1 / v2 * t);
float y = v1 * sin (t) - v2 * sin (v1 / v2 * t);
return QPointF (x, y);
}
QPointF RenderArea::compute_starfish(float t)
{
float R = 5;
float r = 3;
float d = 5;
float x = (R - r) * cos (t) + d * cos (t * ((R - r) / r));
float y = (R - r) * sin (t) - d * sin (t * ((R - r) / r));
return QPointF (x, y);
}
QPointF RenderArea::compute_cloud1(float t)
{
return compute_cloud_with_sign(t, -1);
}
QPointF RenderArea::compute_cloud2(float t)
{
return compute_cloud_with_sign(t, 1);
}
QPointF RenderArea::compute_cloud_with_sign(float t, float sign)
{
float a = 14;
float b = 1;
float x = (a + b) * cos (t * b / a) + sign * b * cos (t * (a + b) / a);
float y = (a + b) * sin (t * b / a) - b * sin (t * (a + b) / a);
return QPointF (x, y);
}