-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart1.cpp
More file actions
168 lines (120 loc) · 3.4 KB
/
part1.cpp
File metadata and controls
168 lines (120 loc) · 3.4 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
// Created by Trung Huynh on 1/27/17.
// Copyright © 2017 Trung Huynh. All rights reserved.
//
//#include <iostream>
#include <math.h>
#include <iostream>
#include <GLUT/GLUT.h>
void keyboard (unsigned char key, int x, int y);
void mouse (int button, int state, int x, int y);
void init (void);
void display(void);
void drawCircle(int x,int y);
double distance(int x,int y, int a ,int b);
int pts[100][2];
int num_pts = 0;
int ptsIndex = 0;
int numberLine = 100;
int radius=50;
using namespace std;
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (640, 480);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Interactive drawing");
init ();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
void keyboard (unsigned char key, int x, int y)
{ int i;
switch (key)
{
case 'q':
glutDestroyWindow(glutGetWindow());
_Exit(0);
break;
case 'Q':
glutDestroyWindow(glutGetWindow());
_Exit(0);
break;
default:
break;
}
}
void mouse (int button, int state, int x, int y)
{
int i;
y = glutGet(GLUT_WINDOW_HEIGHT) - y;
switch (button)
{
case GLUT_LEFT_BUTTON:
{
if (state == GLUT_DOWN) {
pts[ptsIndex][0]=x;
pts[ptsIndex][1] = y;
ptsIndex = (ptsIndex +1)% 100;
glutPostRedisplay();
}
break;
}
case GLUT_RIGHT_BUTTON:
{
if (state == GLUT_DOWN) {
for(int i = ptsIndex-1 ; i>=0 ; i--)
if (distance(x, y, pts[i][0],pts[i][1]) <= radius) {
if (i<ptsIndex-1) {
pts[i][0] = pts[ptsIndex-1][0];
pts[i][1] = pts[ptsIndex-1][1];
}
--ptsIndex;
}
}
glutPostRedisplay();
}
break;
}
}
double distance(int x,int y, int a ,int b){
return sqrt ( pow((x-a), 2) + pow((y-b), 2) );
}
void init (void)
{
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);
// glClearColor (1, 1, 1, 1);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void display(void)
{
int i;
/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT);
/* Write code to display all lines */
glColor3f (1.0, 1.0, 1.0);
int currentPt = 0;
for (int i = 0 ; i <ptsIndex ; i++)
{
drawCircle(pts[i][0], pts[i][1]);
}
glFlush ();
}
void drawCircle(int x,int y){
glColor3d((x%100)/100.0, (y%100)/100.0, (x+y)%100/100);
glBegin(GL_LINE_LOOP);
for(int i = 0; i < numberLine; i++)
{
float theta = 2.0f * 3.1415926f * float(i) / float(numberLine);
float newX = radius * cosf(theta);
float newY = radius * sinf(theta);
glVertex2i( (GLint)x + (GLint)newX, (GLint)y + (GLint)newY);
}
glEnd();
}