-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenderWidget.cpp
530 lines (423 loc) · 13.6 KB
/
RenderWidget.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//
// copyright 2018 Palestine Polytechnic Univeristy
//
// This software can be used and/or modified for academich use as long as
// this commented part is listed
//
// Last modified by: Zein Salah, on 24.02.2021
//
#include "RenderWidget.h"
#include <QPainter>
#include <cmath>
RenderWidget::RenderWidget(QWidget *parent) : QWidget(parent){
}
QSize RenderWidget::minimumSizeHint() const{
return QSize(100, 100);
}
QSize RenderWidget::sizeHint() const{
return QSize(600, 600);
}
void RenderWidget::paintEvent(QPaintEvent *){
//drawTableLamp();
//drawCar();
//CohenSutherland(0, 0, 500, 500, 200, 400, 200, 400);
int verticesNumber = 6;
// Give the vertices coordinates as a pair {x, y}
std::pair<float, float> polygonVertices[verticesNumber]={{50,100}, {200, 50}, {300, 300}, {150, 400}, {0, 300}, {100, 200}};
convexPolygonFilling(polygonVertices, verticesNumber, 600, 0, 600, 0);
}
void RenderWidget::drawTableLamp(){
drawLine(250,599,0,599);
drawEllipse(125, 599, 125, 50);
drawLine(150,549,150,407);
drawLine(115,548,115,407);
drawCircle(135,373,38);
drawCircle(135,373,12);
drawLine(131,335,190,235);
drawLine(163,350,220,255);
drawLine(189,235,235,265);
drawArc(220,202,50, 230, 45);
drawLine(249,168,282,191);
drawArc(345,280,50, 221, 110);
drawLine(413,195,262,351);
drawArc(340, 270, 228, 405, 40);
}
void RenderWidget::drawCar(){
drawCircle(200,500,25);
drawCircle(450,500,25);
drawCircle(200,500,60);
drawCircle(450,500,60);
drawLine(257, 520, 392, 520);
drawLine(257, 480, 392, 480);
drawLine(97, 520, 142, 520);
drawLine(97, 480, 142, 480);
drawLine(507, 520, 562, 520);
drawLine(507, 480, 562, 480);
drawLine(97, 480, 97, 520);
drawLine(562, 480, 562, 520);
drawArc(240, 480, 100, 180, 120);
drawArc(380, 423, -5, 159, 170);
drawLine(549, 425, 549, 480);
drawArc(125, 456, 82, 258, 20);
drawArc(542, 457, 290, 425, 20);
drawLine(394, 365, 514, 365);
drawArc(380, 430, 26, 85, 150);
drawLine(394, 365, 394, 281);
drawLine(244, 365, 364, 365);
drawArc(380, 430, 96, 155, 151);
drawLine(364, 365, 364, 281);
drawLine(339, 410, 364, 410);
drawLine(339, 420, 364, 420);
drawLine(364, 420, 364, 410);
drawLine(339, 410, 339, 420);
}
void RenderWidget::mapPoint(float &x, float &y){
x = x + 300;
y = 300 - y;
}
void RenderWidget::drawLine(float x1, float y1, float x2, float y2){
QPainter painter(this);
QColor color(0, 0, 0);
painter.setPen(color);
if (x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0){
mapPoint(x1, y1);
mapPoint(x2, y2);
}
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int xStep = (x1 < x2) ? 1 : -1;
int yStep = (y1 < y2) ? 1 : -1;
int pk = dx - dy; // decision parameter
while (true){
painter.drawPoint(x1, y1);
if (x1 == x2 && y1 == y2){
break;
}
int pk2 = 2 * pk;
// If pk2 is positive, the algorithm increments the y-coordinate
if (pk2 > -dy){
pk -= dy;
x1 += xStep;
}
if (pk2 < dx){
pk += dx;
y1 += yStep;
}
}
}
void RenderWidget::drawLineMyDerivation(float x1, float y1, float x2, float y2){
QPainter painter(this);
QColor color(0, 0, 0);
painter.setPen(color);
if (x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0){
mapPoint(x1, y1);
mapPoint(x2, y2);
}
painter.drawPoint(x1, y1);
// Calculate constants outside the loop
int dx = x2 - x1, dy = y2 - y1;
int twoDy = 2 * dy, twoDx = 2 * dx;
int twoDyMinusDx = twoDy - twoDx;
int twoDyPlusDx = twoDy + twoDx;
int twoDxMinusDy = twoDx - twoDy;
int twoDxPlusDy = twoDx + twoDy;
int pk;
if (abs(dx) >= abs(dy)){ // Slope between 0 and 1 (Case 1)
if (dx >= 0){
pk = twoDy - dx;
while (x1 < x2){
x1++;
if (pk < 0){
pk += twoDy;
}else{
y1 += (dy >= 0) ? 1 : -1;
pk += twoDyMinusDx;
}
painter.drawPoint(x1, y1);
}
}else{ // Slope between -1 and 0 (Case 3)
pk = twoDy + dx;
while (x1 > x2){
x1--;
if (pk < 0){
pk += twoDyPlusDx;
}else{
y1 += (dy >= 0) ? 1 : -1;
pk += twoDyMinusDx;
}
painter.drawPoint(x1, y1);
}
}
}else{ // Slope between 1 and infinity (Case 2 )
if (dy >= 0){
pk = twoDx - dy;
while (y1 < y2){
y1++;
if (pk < 0)
{
pk += twoDx;
}
else
{
x1 += (dx >= 0) ? 1 : -1;
pk += twoDxMinusDy;
}
painter.drawPoint(x1, y1);
}
}else{ // Slope between -infinity and -1 (Case 4)
pk = twoDx + dy;
while (y1 > y2)
{
y1--;
if (pk < 0)
{
pk += twoDxPlusDy;
}
else
{
x1 += (dx >= 0) ? 1 : -1;
pk += twoDxMinusDy;
}
painter.drawPoint(x1, y1);
}
}
}
}
void RenderWidget::drawCircle(float xc, float yc, float r){
QPainter painter(this);
QColor color(0, 0, 0);
painter.setPen(color);
if (yc < 0 || xc < 0 ){
mapPoint(xc, yc);
}
float x = r, y = 0; // The first point in the circle
float theta = 1 / r;
float sinTheta = sin(theta), cosTheta = cos(theta);
while (y <= x){ // Using the symmetric property for circle
// Octant 1
painter.drawPoint(xc + x, yc - y);
painter.drawPoint(xc + y, yc - x);
// Octant 2
painter.drawPoint(xc - x, yc - y);
painter.drawPoint(xc - y, yc - x);
// Octant 3
painter.drawPoint(xc - x, yc + y);
painter.drawPoint(xc - y, yc + x);
// Octant 4
painter.drawPoint(xc + x, yc + y);
painter.drawPoint(xc + y, yc + x);
x = x * cosTheta - y * sinTheta;
y = x * sinTheta + y * cosTheta;
}
}
void RenderWidget::drawArc(float xc, float yc, float t1, float t2, float r) {
// Ensure t1 is less than t2
if (t1 > t2) {
std::swap(t1, t2);
}
QPainter painter(this);
QColor color(0, 0, 0);
painter.setPen(color);
float theta = 1 / r;
float sinTheta = sin(theta), cosTheta = cos(theta);
t1 = t1 * M_PI / 180.0;
t2 = t2 * M_PI / 180.0;
float x = r * cos(t1);
float y = r * sin(t1);
while (t1 <= t2) {
// Map the point if needed
if (yc < 0 || xc < 0) {
mapPoint(xc, yc);
}
painter.drawPoint(xc + x, yc - y);
t1 += theta;
x = r * cos(t1);
y = r * sin(t1);
}
}
void RenderWidget::drawEllipse(float xc, float yc, float a, float b) {
QPainter painter(this);
QColor color(0, 0, 0);
painter.setPen(color);
if (yc < 0 || xc < 0 ){
mapPoint(xc, yc);
}
int numPoints = 1000;
float theta = M_PI / numPoints; // * 2 if you want the whole ellipse
for (int i = 0; i <= numPoints; ++i) {
float currentTheta = theta * i;
float r = a * b / sqrt(pow(b * cos(currentTheta), 2) + pow(a * sin(currentTheta), 2));
float xCoordinate = xc + r * cos(currentTheta);
float yCoordinate = yc - r * sin(currentTheta);
painter.drawPoint(xCoordinate, yCoordinate);
}
}
void RenderWidget::CohenSutherland(float x1, float y1, float x2, float y2, float wLeft, float wRight, float wTop, float wBottom) {
// Clipping line according to Cohen-Sutherland Algorithm
// Send the 2 vertices for line that you want to clip, and the dimensions for the window to clip against it
QPainter painter(this);
QColor color(0, 0, 0);
painter.setPen(color);
// Iterative clipping until the line is completely inside or outside the window
while (true){
// Giving regions for the points
std::string regionPoint1 = "", regionPoint2 = "";
regionPoint1 += ( y1 < wTop ) ? "T" : ( y1 > wBottom) ? "B" : "";
regionPoint1 += ( x1 < wLeft ) ? "L" : ( x1 > wRight) ? "R" : "";
regionPoint2 += ( y2 < wTop ) ? "T" : ( y2 > wBottom) ? "B" : "";
regionPoint2 += ( x2 < wLeft ) ? "L" : ( x2 > wRight) ? "R" : "";
// Case 1 : The whole line is in the window (visible) -> draw it
if (regionPoint1 == "" && regionPoint2 == ""){
drawLine(x1, y1, x2, y2);
return;
}
// Case 2 : The whole line is outside the window (invisible) -> do not draw anything
// if the intersection between the tow points region is {}
bool isOutside = false;
for (char c : regionPoint1) {
if (regionPoint2.find(c) != std::string::npos) {
// If there's a common character between the two region strings,
// it means there's a part of the line inside the window.
isOutside = true;
break;
}
}
// If no common character found, the line is completely outside the window.
if (isOutside) {
return;
}
// Case 3: Otherwise
// Clip both points
float m = (y2 - y1) / (x2 - x1);
// Array of pointers, each one represents the value of a point
// When we change the value in the array the change affect the variable it self since er store the reference to it
// This as an optimization instead of call a function twice
float* xArr[] = {&x1, &x2};
float* yArr[] = {&y1, &y2};
std::string regionPointArr[] = {regionPoint1, regionPoint2};
for (int i = 0; i < 2; ++i) {
// In the first loop we clip the first point against the 4 directions, and in the second loop we do fo the second one
// I did this to avoid creating new function
if (regionPointArr[i].find("L") != std::string::npos) {
*yArr[i] += m * (wLeft - *xArr[i]);
*xArr[i] = wLeft;
} else if (regionPointArr[i].find("R") != std::string::npos) {
*yArr[i] += m * (wRight - *xArr[i]);
*xArr[i] = wRight;
}
if (regionPointArr[i].find("T") != std::string::npos) {
*xArr[i] += (wTop - *yArr[i]) / m;
*yArr[i] = wTop;
} else if (regionPointArr[i].find("B") != std::string::npos) {
*xArr[i] += (wBottom - *yArr[i]) / m;
*yArr[i] = wBottom;
}
}
/*
Other 2 alterative ways for Case 3:
1. repeat this code for the 2 points
float m = (y2 - y1) / (x2 - x1);
if (regionPoint1.find("L") != string::npos ){
y1 += m * (wLeft - x1);
x1 = wLeft;
}else if (regionPoint1.find("R") != string::npos ){
y1 += m * (wRight - x1 );
x1 = wRight;
}
if (regionPoint1.find("T") != string::npos ){
x1 += (wTop - y1) / m;
y1 = wTop;
}else if (regionPoint1.find("B") != string::npos ){
x1 += (wBottom - y1) / m;
}
2. Write a function to clip a point, and call it twice
*/
}
}
void RenderWidget::convexPolygonFilling(std::pair<float, float> polygonVertices[], int verticesNumber, float xMax, float xMin, float yMax, float yMin){
// The scanline table consists of 2 columns: the first is xMin and the second is xMax
// The number of rows depends on the hight of the frame buffer
float scanlineNumber = yMax - yMin;
float scanlineTable [int(scanlineNumber)][2];
// Initialize the array and but the maximum value in the xMin and vice versa, to determine weather there is a span in this scan line or no
// If the xMin < xMax -> there is a span in this scanline
for (int i = 0; i < scanlineNumber; i++){
for (int j = 0; j < 2; j++){
scanlineTable[i][j] = ( j == 0 ) ? xMax : xMin;
}
}
float mm; // 1/m
float x1, y1, x2, y2;
// Now we update te scanline table according to the polygon vertices
for (int i = 0; i < verticesNumber; i++){
// polygonVertices[i].first -> the x value for point i & polygonVertices[i].second -> the y value for point i
x1 = polygonVertices[i].first, y1 = polygonVertices[i].second;
// I added the percentage to handle the case (the edge) between the first and last vertices
x2 = polygonVertices[(i + 1) % verticesNumber].first, y2 = polygonVertices[(i + 1) % verticesNumber].second;
mm = (x2 - x1) / (y2 - y1 );
// To draw the polygon border (optionally)
myDrawLine(x1,y1,x2,y2);
if (y2 < y1){
std::swap(y2, y1);
std::swap(x2, x1);
}
float x = x1, y = y1;
// Loop to the whole scanlines between each two vertices
while(y <= y2){
// Insert (x,y) into SLT (update the table)
scanlineTable[int(y)][0] = std::min(scanlineTable[int(y)][0], x);
scanlineTable[int(y)][1] = std::max(scanlineTable[int(y)][1], x);
x += mm;
y++;
}
}
// Or you can use for each loop
// for (auto point : polygonVertices ){
//
// }
QPainter painter(this);
QColor color(0, 0, 0);
painter.setPen(color);
// Now the scanline table is ready, we will use it to fill the polygon
for (int y = yMin ; y < yMax; y++){
// Check if there is a span in this scanline
if (scanlineTable[y][0] < scanlineTable[y][1]){
// draw a line in this scanline
for (int x = scanlineTable[y][0]; x < scanlineTable[y][1]; x++){
painter.drawPoint(x, y);
}
}
}
}
void RenderWidget::myDrawLine(float x1, float y1, float x2, float y2){
QPainter painter(this);
// painter.setRenderHint(QPainter::Antialiasing, false);
QColor color(0, 0, 0);
painter.setPen(color);
float m = (y2 - y1) / (x2 - x1);
// SetPixel(x1, y1, color); // first point
painter.drawPoint(x1, y1);
if (m < 1)
{
float y = y1;
for (int i = x1 + 1; i < x2; ++i)
{
y = y + m;
// SetPixel(i, round(y), color);
painter.drawPoint(i, round(y));
}
}
else // i.e., m > 1
{
float mm = 1 / m;
float x = x1;
for (int i = y1 + 1; i < y2; ++i)
{
x = x + mm;
// SetPixel(round(x), i, color);
painter.drawPoint(round(x), i);
}
}
// SetPixel(x2, y2, color); // last point
painter.drawPoint(x2, y2);
}