-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguard.cpp
More file actions
executable file
·284 lines (196 loc) · 6.74 KB
/
Copy pathguard.cpp
File metadata and controls
executable file
·284 lines (196 loc) · 6.74 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
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
/*
Startup code for the art gallery project
Computational Geometry
Laura Toma
*/
#include "geom.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <vector>
using namespace std;
GLfloat red[3] = {1.0, 0.0, 0.0};
GLfloat green[3] = {0.0, 1.0, 0.0};
GLfloat blue[3] = {0.0, 0.0, 1.0};
GLfloat black[3] = {0.0, 0.0, 0.0};
GLfloat white[3] = {1.0, 1.0, 1.0};
GLfloat gray[3] = {0.5, 0.5, 0.5};
GLfloat yellow[3] = {1.0, 1.0, 0.0};
GLfloat magenta[3] = {1.0, 0.0, 1.0};
GLfloat cyan[3] = {0.0, 1.0, 1.0};
/* global variables */
const int WINDOWSIZE = 750;
//the current polygon
vector<point2d> poly;
//coordinates of the last mouse click
double mouse_x=-10, mouse_y=-10; //initialized to a point outside the window
/* forward declarations of functions */
void display(void);
void keypress(unsigned char key, int x, int y);
void mousepress(int button, int state, int x, int y);
void timerfunc();
void initialize_polygon();
void print_polygon(vector<point2d>& poly);
/* ****************************** */
int main(int argc, char** argv) {
initialize_polygon();
print_polygon(poly);
/* initialize GLUT */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOWSIZE, WINDOWSIZE);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
/* register callback functions */
glutDisplayFunc(display);
glutKeyboardFunc(keypress);
glutMouseFunc(mousepress);
//glutIdleFunc(timerfunc); //register this if you want it called at every fraame
/* init GL */
/* set background color black*/
glClearColor(0, 0, 0, 0);
/* give control to event handler */
glutMainLoop();
return 0;
}
/* ****************************** */
/* initialize polygon stored in global variable poly
The points are in our local coordinate system (0,WINSIZE) x (0, WINSIZE)
with the origin in the lower left corner.
*/
void initialize_polygon() {
//clear the vector, in case something was in it
poly.clear();
double rad = 100;
point2d p;
for (double a=0; a<2*M_PI; a+=.5) {
p.x = WINDOWSIZE/2 + rad * cos (a);
p.y = WINDOWSIZE/2 + rad * sin (a);
poly.push_back (p);
}
}
/* ****************************** */
/* draw the polygon */
void draw_polygon(vector<point2d>& poly){
if (poly.size() == 0) return;
//set color and polygon mode
glColor3fv(yellow);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
int i;
for (i=0; i<poly.size()-1; i++) {
//draw a line from point i to i+1
glBegin(GL_LINES);
glVertex2f(poly[i].x, poly[i].y);
glVertex2f(poly[i+1].x, poly[i+1].y);
glEnd();
}
//draw a line from the last point to the first
int last=poly.size()-1;
glBegin(GL_LINES);
glVertex2f(poly[last].x, poly[last].y);
glVertex2f(poly[0].x, poly[0].y);
glEnd();
}
/* draw a circle with center at (x,y) of radius r
Our coordinate system is (0,WINSIZE) x (0, WINSIZE)
with the origin in the lower left corner.
*/
void draw_circle(double x, double y, int r){
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3fv(blue);
glBegin(GL_POLYGON);
for(double theta = 0; theta < 2*M_PI; theta+=.3){
glVertex2f(x + r*cos(theta), y + r*sin(theta));
}
glEnd();
}
/* ******************************** */
void print_polygon(vector<point2d>& poly) {
printf("polygon:");
for (unsigned int i=0; i<poly.size()-1; i++) {
printf("\tedge %d: [(%d,%d), (%d,%d)]\n",
i, poly[i].x, poly[i].y, poly[i+1].x, poly[i+1].y);
}
//print last edge from last point to first point
int last = poly.size()-1;
printf("\tedge %d: [(%d,%d), (%d,%d)]\n",
last, poly[last].x, poly[last].y, poly[0].x, poly[0].y);
}
/* ****************************** */
/* This is the function that renders the window. We registered this
function as the "displayFunc". It will be called by GL everytime
the window needs to be rendered.
*/
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
//clear all modeling transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* The default GL window is [-1,1]x[-1,1]x[-1,1] with the origin in
the center. The camera is at (0,0,0) looking down negative
z-axis.
The points are in the range [0, WINSIZE] x [0, WINSIZE] so they
need to be mapped to [-1,1]x [-1,1] */
//scale the points to [0,2]x[0,2]
glScalef(2.0/WINDOWSIZE, 2.0/WINDOWSIZE, 1.0);
//first translate the points to [-WINDOWSIZE/2, WINDOWSIZE/2]
glTranslatef(-WINDOWSIZE/2, -WINDOWSIZE/2, 0);
//draw the polygon stored in the global variable "poly"
draw_polygon(poly);
//draw a circle where the mouse was last clicked. Note that this
//point is stored in global variables mouse_x, mouse_y, which are
//updated by the mouse handler function
draw_circle(mouse_x, mouse_y, 20);
/* execute the drawing commands */
glFlush();
}
/* ****************************** */
void keypress(unsigned char key, int x, int y) {
switch(key) {
case 'q':
exit(0);
break;
}
}
/*
void glutMouseFunc(void (*func)(int button, int state, int x, int y));
glutMouseFunc sets the mouse callback for the current window. When a
user presses and releases mouse buttons in the window, each press and
each release generates a mouse callback. The button parameter is one
of GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON. For
systems with only two mouse buttons, it may not be possible to
generate GLUT_MIDDLE_BUTTON callback. For systems with a single mouse
button, it may be possible to generate only a GLUT_LEFT_BUTTON
callback. The state parameter is either GLUT_UP or GLUT_DOWN
indicating whether the callback was due to a release or press
respectively. The x and y callback parameters indicate the window
relative coordinates when the mouse button state changed. If a
GLUT_DOWN callback for a specific button is triggered, the program can
assume a GLUT_UP callback for the same button will be generated
(assuming the window still has a mouse callback registered) when the
mouse button is released even if the mouse has moved outside the
window.
*/
void mousepress(int button, int state, int x, int y) {
if(state == GLUT_DOWN) { //mouse click detected
//(x,y) are in window coordinates, where the origin is in the upper
//left corner; our reference system has the origin in lower left
//corner, this means we have to reflect y
mouse_x = (double)x;
mouse_y = (double)(WINDOWSIZE - y);
printf("mouse pressed at (%.1f,%.1f)\n", mouse_x, mouse_y);
}
glutPostRedisplay();
}
//this function is called every frame. Use for animations
void timerfunc() {
printf("timerfunc: \n");
//if you want the window to be re-drawn, call this
// glutPostRedisplay();
}