-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCase Study 2-6-6.txt
181 lines (156 loc) · 3.41 KB
/
Case Study 2-6-6.txt
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
#include<Windows.h>
#include<gl/GL.h>
#include<gl/glut.h>
class GLintPoint {
public:
GLint x, y;
};
#define NUM 20
static GLintPoint List[NUM][NUM];
static int polylineNum = 0; // last index used so far
static int lineNum = 0; // last index used so far
int mouseMode;
bool empty = true;
int buffer1 = -1;
int buffer2 = -1;
int inputx;
int inputy;
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(R, G, B);
glPointSize(4.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640, 0.0, 480);
}
void drawDot(GLint x, GLint y)
{
glBegin(GL_POINTS);
glPointSize(10);
glVertex2i(x, y);
glEnd();
glFlush();
}
void myMouse(int button, int state, int x, int y)
{
// test for mouse button as well as for a full array
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && lineNum < NUM - 1)
{
inputx = x;
inputy = 480 - y;
switch (mouseMode)
{
case 1:
List[polylineNum][lineNum].x = inputx; // add new point to list
List[polylineNum][lineNum].y = inputy; // window height is 480
lineNum++;
break;
case 2:
for (int ia = 0; ia < 20; ia++)
{
for (size_t i = 0; i < 20; i++)
{
if ((abs(List[ia][i].x - inputx) < 5) && (abs(List[ia][i].y - inputy) < 5))
{
for (size_t ib = i; ib < 20; ib++)
{
List[ia][ib].x = List[ia][ib + 1].x;
List[ia][ib].y = List[ia][ib + 1].y;
}
}
}
}
break;
case 3:
if (empty)
{
for (int ia = 0; ia < 20; ia++)
{
for (size_t i = 0; i < 20; i++)
{
if ((abs(List[ia][i].x - inputx) < 5) && (abs(List[ia][i].y - inputy) < 5))
{
buffer1 = ia;
buffer2 = i;
empty = false;
}
}
}
}
else
{
List[buffer1][buffer2].x = inputx;
List[buffer1][buffer2].y = inputy;
buffer1 = -1;
buffer2 = -1;
empty = true;
}
break;
default:
break;
}
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glColor3f(1.0, 0.0, 0.0);
for (int ia = 0; ia < 20; ia++)
{
glBegin(GL_LINE_STRIP);
glLineWidth(4);
for (size_t i = 0; i < 20; i++)
{
if (List[ia + 1][i].x == 0 && List[ia + 1][i].y == 0) break; // stop if polygon end
glVertex2i(List[ia + 1][i].x, List[ia + 1][i].y);
}
glEnd();
glFlush();
}
}
}
void myKeyboard(unsigned char theKey, int mouseX, int mouseY) {
GLint x = mouseX; //grab the mouse position
mouseY = 480 - mouseY;
GLint y = mouseY; // flip it as usual
switch (theKey)
{
case 'b': // create a new polyline
polylineNum++;
lineNum = 0;
mouseMode = 1;
break;
case 'd':
mouseMode = 2;
break;
case 'm':
mouseMode = 3;
break;
case 'r':
for (int ia = 0; ia < 20; ia++)
{
for (size_t i = 0; i < 20; i++)
{
List[ia][i].x = 0;
List[ia][i].y = 0;
}
}
break;
case 'q':
exit(-1); //terminate the program
break;
default:
break; // do nothing
}
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(50, 50);
glutCreateWindow("Case Study 2.6.6");
glutKeyboardFunc(myKeyboard);
glutMouseFunc(myMouse);
//glutDisplayFunc(house);
myInit();
glutMainLoop();
}