-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
741 lines (690 loc) · 16.3 KB
/
main.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#include <bits/stdc++.h>
#include <time.h>
#include "Dot.h"
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#endif
using namespace std;
float red = 1.0, green = 0.0, blue = 0.0;
int tmpx, tmpy; // store the first point when shape is line, rectangle or circle
int brushSize = 4;
int eraserSize = 1;
bool isSecond = false;
bool isRandom = false;
bool isEraser = false;
bool isRadial = false;
float window_w = 1365;
float window_h = 768;
int shape = 1; // 1:point, 2:line, 3:rectangle, 4:circle, 5:brush
std::vector<Dot> dots; // store all the points until clear
std::list<int> undoHistory; // record for undo, maximum 20 shapes in history
std::list<int> redoHistory; // record for redo, maximum 20 shapes in history
std::vector<Dot> redoDots; // store the dots after undo temporaly
void pallet();
void display(void)
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
pallet();
glPointSize(2);
glBegin(GL_POINTS);
for (unsigned int i = 0; i < dots.size(); i++)
{
glColor3f(dots[i].getR(), dots[i].getG(), dots[i].getB());
glVertex2i(dots[i].getX(), dots[i].getY());
}
glEnd();
glutSwapBuffers();
}
void clear()
{
dots.clear();
undoHistory.clear();
redoDots.clear();
redoHistory.clear();
glClear(GL_COLOR_BUFFER_BIT);
pallet();
glutSwapBuffers();
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
std::cout << asctime(timeinfo)
<< "[Info] The window and the history are cleared successfully.\n";
}
void quit()
{
std::cout << "Thank you for using this Paint tool! Goodbye!" << std::endl;
exit(0);
}
void undo()
{
if (undoHistory.size() > 0)
{
if (undoHistory.back() != dots.size() && redoHistory.back() != dots.size())
{
redoHistory.push_back(dots.size());
}
int numRemove = dots.size() - undoHistory.back();
for (int i = 0; i < numRemove; i++)
{
redoDots.push_back(dots.back());
dots.pop_back();
}
redoHistory.push_back(undoHistory.back());
undoHistory.pop_back();
}
else
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
std::cout << asctime(timeinfo)
<< "[Warning] Cannot undo. This is the first record in the history.\n";
}
}
void redo()
{
if (redoHistory.size() > 1)
{
undoHistory.push_back(redoHistory.back());
redoHistory.pop_back();
int numRemove = redoHistory.back() - dots.size();
for (int i = 0; i < numRemove; i++)
{
dots.push_back(redoDots.back());
redoDots.pop_back();
}
}
else
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
std::cout << asctime(timeinfo)
<< "[Warning] Cannot redo. This is the last record in the history.\n";
}
}
void drawDot(int mousex, int mousey)
{
Dot newDot(mousex, window_h - mousey, isEraser ? 1.0 : red, isEraser ? 1.0 : green, isEraser ? 1.0 : blue);
dots.push_back(newDot);
}
void drawBrush(int x, int y)
{
for (int i = 0; i < brushSize; i++)
{
int randX = rand() % (brushSize + 1) - brushSize / 2 + x;
int randY = rand() % (brushSize + 1) - brushSize / 2 + y;
drawDot(randX, randY);
}
}
void drawLine(int x1, int y1, int x2, int y2)
{
bool changed = false;
// Bresenham's line algorithm is only good when abs(dx) >= abs(dy)
// So when abs(dx) < abs(dy), change axis x and y
if (abs(x2 - x1) < abs(y2 - y1))
{
int tmp1 = x1;
x1 = y1;
y1 = tmp1;
int tmp2 = x2;
x2 = y2;
y2 = tmp2;
changed = true;
}
int dx = x2 - x1;
int dy = y2 - y1;
int yi = 1;
int xi = 1;
if (dy < 0)
{
yi = -1;
dy = -dy;
}
if (dx < 0)
{
xi = -1;
dx = -dx;
}
int d = 2 * dy - dx;
int incrE = dy * 2;
int incrNE = 2 * dy - 2 * dx;
int x = x1, y = y1;
if (changed)
drawDot(y, x);
else
drawDot(x, y);
while (x != x2)
{
if (d <= 0)
d += incrE;
else
{
d += incrNE;
y += yi;
}
x += xi;
if (changed)
drawDot(y, x);
else
drawDot(x, y);
}
}
/**
* We can use drawLine function to draw the rectangle
*
* Top-left corner specified by the first click,
* and the bottom-right corner specified by a second click
*/
void drawRectangle(int x1, int y1, int x2, int y2)
{
if (x1 < x2 && y1 < y2)
{
drawLine(x1, y1, x2, y1);
drawLine(x2, y1, x2, y2);
drawLine(x2, y2, x1, y2);
drawLine(x1, y2, x1, y1);
}
else
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
std::cout << asctime(timeinfo)
<< "[Warning] The first click should be the top-left corner, the second click should be bottom-right corner.\n";
}
}
/**
* Midpoint circle algorithm
*/
void drawCircle(int x1, int y1, int x2, int y2)
{
int r = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
double d;
int x, y;
x = 0;
y = r;
d = 1.25 - r;
while (x <= y)
{
drawDot(x1 + x, y1 + y);
drawDot(x1 - x, y1 + y);
drawDot(x1 + x, y1 - y);
drawDot(x1 - x, y1 - y);
drawDot(x1 + y, y1 + x);
drawDot(x1 - y, y1 + x);
drawDot(x1 + y, y1 - x);
drawDot(x1 - y, y1 - x);
x++;
if (d < 0)
{
d += 2 * x + 3;
}
else
{
y--;
d += 2 * (x - y) + 5;
}
}
}
void drawRadialBrush(int x, int y)
{
int xc = glutGet(GLUT_WINDOW_WIDTH) / 2;
int yc = glutGet(GLUT_WINDOW_HEIGHT) / 2;
int dx, dy;
dx = xc - x;
dy = yc - y;
drawDot(xc + dx, yc + dy);
drawDot(xc - dx, yc + dy);
drawDot(xc + dx, yc - dy);
drawDot(xc - dx, yc - dy);
drawDot(xc + dy, yc + dx);
drawDot(xc - dy, yc + dx);
drawDot(xc + dy, yc - dx);
drawDot(xc - dy, yc - dx);
}
void pallet(){
glBegin(GL_POLYGON);
glColor3f(1, 0, 0);
glVertex2f(50, 0);
glColor3f(0, 1, 0);
glVertex2f(250,0);
glColor3f(0, 0, 1);
glVertex2f(150, 150);
glEnd();
}
void erase(int x, int y)
{
for (int i = -eraserSize; i <= eraserSize; i++)
{
for (int j = -eraserSize; j <= eraserSize; j++)
{
drawDot(x + i, y + j);
}
}
}
void keyboard(unsigned char key, int xIn, int yIn)
{
isSecond = false;
switch (key)
{
case 'q':
case 27: // 27 is the esc key
quit();
break;
case 'c':
clear();
break;
case '+':
if (shape == 5 && !isEraser)
{
if (brushSize < 16)
brushSize += 4;
else
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
std::cout << asctime(timeinfo)
<< "[Warning] Airbrush's size cannot be larger. It is already the largest.\n";
}
}
else if (isEraser)
{
if (eraserSize < 10)
eraserSize += 4;
else
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
std::cout << asctime(timeinfo)
<< "[Warning] Eraser's size cannot be larger. It is already the largest.\n";
}
}
break;
case '-':
if (shape == 5 && !isEraser)
{
if (brushSize > 4)
brushSize -= 4;
else
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
std::cout << asctime(timeinfo)
<< "[Warning] Airbrush's size cannot be smaller. It is already the smallest.\n";
}
}
else if (isEraser)
{
if (eraserSize > 2)
eraserSize -= 4;
else
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
std::cout << asctime(timeinfo)
<< "[Warning] Eraser's size cannot be smaller. It is already the smallest.\n";
}
}
break;
case 'u':
undo();
break;
case 'r':
redo();
break;
}
}
void getColor(int x,int y)
{
unsigned char p[3];
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &p[0]);
red = p[0]/(float)255;
green = p[1]/(float)255;
blue = p[2]/(float)255;
}
float triangleArea(int x1, int y1, int x2, int y2, int x3, int y3){
return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
}
bool isInTriangle(int x, int y){
int x1 = 50, x2 = 250, x3 = 150;
int y1 = 0, y2 = 0, y3 = 150;
float A = triangleArea(x1, y1, x2, y2, x3, y3);
float A1 = triangleArea(x, y, x2, y2, x3, y3);
float A2 = triangleArea(x1, y1, x, y, x3, y3);
float A3 = triangleArea(x1, y1, x2, y2, x, y);
return (A == A1 + A2 + A3);
}
void mouse(int bin, int state, int x, int y)
{
if (bin == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
cout<<x<<' '<<y;
if(isInTriangle(x, window_h - y)){
getColor(x, window_h - y);
return;
}
if (isRandom)
{
srand(time(NULL));
red = float(rand()) / float(RAND_MAX);
green = float(rand()) / float(RAND_MAX);
blue = float(rand()) / float(RAND_MAX);
}
if (isEraser)
{
undoHistory.push_back(dots.size());
erase(x, y);
}
else
{
if (shape == 1)
{
undoHistory.push_back(dots.size());
if (isRadial)
drawRadialBrush(x, y);
else
drawDot(x, y);
}
else if (shape == 5)
{
undoHistory.push_back(dots.size());
drawBrush(x, y);
}
else
{
if (!isSecond)
{
tmpx = x;
tmpy = y;
isSecond = true;
}
else
{
if (undoHistory.back() != dots.size())
undoHistory.push_back(dots.size());
if (shape == 2)
drawLine(tmpx, tmpy, x, y);
else if (shape == 3)
drawRectangle(tmpx, tmpy, x, y);
else if (shape == 4)
drawCircle(tmpx, tmpy, x, y);
isSecond = false;
}
}
}
if (undoHistory.size() > 20)
{
undoHistory.pop_front();
}
}
}
void motion(int x, int y)
{
if (isEraser)
erase(x, y);
else
{
if (shape == 1)
{
if (isRadial)
drawRadialBrush(x, y);
else
drawDot(x, y);
}
if (shape == 5)
drawBrush(x, y);
}
}
void reshape(int w, int h)
{
window_w = w;
window_h = h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, w, h);
}
void processMainMenu(int value)
{
switch (value)
{
case 0:
quit();
break;
case 1:
clear();
break;
case 2:
undo();
break;
case 3:
redo();
break;
}
}
void processBrushSizeMenu(int value)
{
shape = 5;
isEraser = false;
brushSize = value;
glutSetCursor(GLUT_CURSOR_CROSSHAIR);
}
void processColourMenu(int value)
{
isSecond = false;
isEraser = false;
isRandom = false;
switch (value)
{
case 1: // red
red = 1.0;
green = 0.0;
blue = 0.0;
break;
case 2: // green
red = 0.0;
green = 1.0;
blue = 0.0;
break;
case 3: // blue
red = 0.0;
green = 0.0;
blue = 1.0;
break;
case 4: // purple
red = 0.5;
green = 0.0;
blue = 0.5;
break;
case 5: // yellow
red = 1.0;
green = 1.0;
blue = 0.0;
break;
case 6: // random
isRandom = true;
break;
}
}
void processShapeMenu(int value)
{
shape = value;
isEraser = false;
isSecond = false;
isRadial = false;
switch (shape)
{
case 1:
glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
break;
case 2:
case 3:
case 4:
glutSetCursor(GLUT_CURSOR_CROSSHAIR);
break;
}
}
void processEraserSizeMenu(int value)
{
glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
eraserSize = value;
isEraser = true;
}
void processRadicalBrushMenu(int value)
{
isRadial = value == 1 ? true : false;
}
void createOurMenu()
{
int colourMenu = glutCreateMenu(processColourMenu);
glutAddMenuEntry("Red", 1);
glutAddMenuEntry("Green", 2);
glutAddMenuEntry("Blue", 3);
glutAddMenuEntry("Purple", 4);
glutAddMenuEntry("Yellow", 5);
glutAddMenuEntry("Random", 6);
int sizeMenu = glutCreateMenu(processBrushSizeMenu);
glutAddMenuEntry("4px", 4);
glutAddMenuEntry("8px", 8);
glutAddMenuEntry("12px", 12);
glutAddMenuEntry("16px", 16);
int shapeMenu = glutCreateMenu(processShapeMenu);
glutAddMenuEntry("Point", 1);
glutAddMenuEntry("Line", 2);
glutAddMenuEntry("Rectangle", 3);
glutAddMenuEntry("Circle", 4);
glutAddSubMenu("Airbrush", sizeMenu);
int eraserSizeMenu = glutCreateMenu(processEraserSizeMenu);
glutAddMenuEntry("Small", 2);
glutAddMenuEntry("Medium", 6);
glutAddMenuEntry("Large", 10);
int radicalBrushMenu = glutCreateMenu(processRadicalBrushMenu);
glutAddMenuEntry("True", 1);
glutAddMenuEntry("False", 2);
int main_id = glutCreateMenu(processMainMenu);
glutAddSubMenu("Colour", colourMenu);
glutAddSubMenu("Shapes", shapeMenu);
glutAddSubMenu("Radical Paint Brush", radicalBrushMenu);
glutAddSubMenu("Eraser", eraserSizeMenu);
glutAddMenuEntry("Undo", 2);
glutAddMenuEntry("Redo", 3);
glutAddMenuEntry("Clear", 1);
glutAddMenuEntry("Quit", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void init(void)
{
/* background color */
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(red, green, blue);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, window_w, 0.0, window_h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void FPS(int val)
{
glutPostRedisplay();
glutTimerFunc(0, FPS, 0);
}
void callbackInit()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutTimerFunc(17, FPS, 0);
}
void printGuide()
{
std::cout << "#########################################################################\n"
<< "# Welcome to use this Paint tool! #\n"
<< "#########################################################################\n"
<< "A list of commands:\n"
<< "Right click\t"
<< "-> show menu\n"
<< "Left click\t"
<< "-> choose option\n"
<< "Menu \"Colour\"\t"
<< "-> You can choose Red, Green, Blue, Yellow or Random, the default color is Red.\n"
<< "Menu \"Shapes\"\t"
<< "-> The default shape is Point.\n"
<< "\tPoint\t\t"
<< "-> draw a dot at the point clicked with the mouse. Clicking and dragging will draw points constantly like free-form drawing.\n"
<< "\tLine\t\t"
<< "-> draw a linebetween two subseauently clicked points. (Bresenham's algorithm is used here.)\n"
<< "\tRectangle\t"
<< "-> draw a rectangle with top-left corner specified by the first click and the bottom-right corner specified by a second click. If the second click is bottom-left, top-right or top-left comparing to the first click, a warning will show in the console.\n"
<< "\tCircle\t\t"
<< "-> draw a circle centered at the position of the first click, with its radius set by a second click.\n"
<< "\tAirbrush\t"
<< "-> draw multiple points as brush around the clicked point. There are four options of size.\n"
<< "Menu \"Radical..\""
<< "-> draw multiple points as brush around the centre point of the document. You can choose True or False. Only useful when shape is point. The default option is false. After choosing point in shape, it becomes False.\n"
<< "Menu \"Eraser\"\t"
<< "-> erase the points by clicking and dragging.\n"
<< "Menu \"Undo\"\t"
<< "-> undo, the history can keep maximum 20 records.\n"
<< "Menu \"Redo\"\t"
<< "-> redo, the history can keep maximum 20 records.\n"
<< "Menu \"Clear\"\t"
<< "-> clear all the points and clear the history.\n"
<< "Menu \"Quit\"\t"
<< "-> close the window.\n"
<< "Keyboard 'q'\t"
<< "-> close the window.\n"
<< "Keyboard 'esc'\t"
<< "-> close the window.\n"
<< "Keyboard 'c'\t"
<< "-> clear all the points and clear the history.\n"
<< "Keyboard '+'\t"
<< "-> larger size of eraser or brush.\n"
<< "Keyboard '-'\t"
<< "-> smaller size of eraser or brush.\n"
<< "Keyboard 'u'\t"
<< "-> undo, the history can keep maximum 20 records.\n"
<< "Keyboard 'r'\t"
<< "-> redo, the history can keep maximum 20 records.\n"
<< "################################# Paint #################################" << std::endl;
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(window_w, window_h);
glutInitWindowPosition(100, 100);
glutCreateWindow("Paint");
callbackInit();
init();
printGuide();
createOurMenu();
glutMainLoop();
return (0);
}