-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp1.cpp
291 lines (256 loc) · 7.36 KB
/
exp1.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
#include<windows.h>
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include<vector>
#include<iostream>
#include <algorithm>
#include "onMidPointEllispe.h"
#define OFFSETx (xy1[0][0]+xy2[0][0])/2
#define OFFSETy (xy1[0][1]+xy2[0][1])/2
int menu=0,submenu1=0,submenu2;
using namespace std;
int iPointNum = 0; //以确定点的数目
vector<vector<int> > xy1(100,vector<int>(2));
vector<vector<int> > xy2(100,vector<int>(2));
int n = 0;
int i=0; //标记第i个点
int iMode=0; //菜单模式选择
int winWidth = 400, winHeight =300; //窗口大小
void Initial(void)
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //窗口背景颜色
}
void ChangeSize(int w, int h)
{
winHeight = h;
winWidth =w;
glViewport(0,0,w,h); //指定窗口显示区域
glMatrixMode(GL_PROJECTION); //设置窗口显示区域
glLoadIdentity(); //调用单位矩阵, 去掉以前的投影参数设置
gluOrtho2D(0.0, winWidth, 0.0, winHeight); //设置投影参数
}
void onMidpointEllispe(int a,int b) //中点Bresenham算法画椭圆
{
int x,y;
float d1,d2;
x = 0;y = b;
d1=b*b+a*a*(-b+0.25);
glPointSize(5); //设置画笔尺寸
glBegin(GL_POINTS);
glVertex2i(OFFSETx+x,OFFSETy+y);
glVertex2i(OFFSETx-x,OFFSETy-y);
glVertex2i(OFFSETx-x,OFFSETy+y);
glVertex2i(OFFSETx+x,OFFSETy-y);
glEnd();
while(b*b*(x+1) < a*a*(y-0.5)){
if(d1<=0){
d1+=b*b*(2*x+3);
x++;
}
else{
d1+=b*b*(2*x+3)+a*a*(-2*y+2);
x++;
y--;
}
glBegin(GL_POINTS);
glVertex2i(OFFSETx+x,OFFSETy+y);
glVertex2i(OFFSETx-x,OFFSETy-y);
glVertex2i(OFFSETx-x,OFFSETy+y);
glVertex2i(OFFSETx+x,OFFSETy-y);
glEnd();
}//while上半部分
d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;
while(y>0){
if(d2<=0){
d2+=b*b*(2*x+2)+a*a*(-2*y+3);
x++,y--;
}
else{
d2+=a*a*(-2*y+3);
y--;
}
glBegin(GL_POINTS);
glVertex2i(OFFSETx+x,OFFSETy+y);
glVertex2i(OFFSETx-x,OFFSETy-y);
glVertex2i(OFFSETx-x,OFFSETy+y);
glVertex2i(OFFSETx+x,OFFSETy-y);
glEnd();
}
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT); //用当前背景色填充窗口
glColor3f(1.0f, 0.0f, 0.0f); //指定当前绘图颜色
if(iMode==1)
{
for(i=0;i<n;i++)
{
glBegin(GL_LINES); //维持
glVertex2i(xy1[i][0],xy1[i][1]);
glVertex2i(xy2[i][0],xy2[i][1]);
glEnd();
}
if(iPointNum == 1)
{
glBegin(GL_LINES);
glVertex2i(xy1[i][0],xy1[i][1]);
glVertex2i(xy2[i][0],xy2[i][1]);
glEnd();
}
}
else if(iMode==2){
glBegin(GL_LINE_LOOP);
glVertex2i(xy1[0][0],xy1[0][1]);
glVertex2i(xy2[0][0],xy1[0][1]);
glVertex2i(xy2[0][0],xy2[0][1]);
glVertex2i(xy1[0][0],xy2[0][1]);
glEnd();
glutPostRedisplay();
onMidpointEllispe(abs(xy2[0][0]-xy1[0][0])/2,abs(xy2[0][1]-xy1[0][1])/2);
}
glutSwapBuffers(); //交换缓冲区
}
//鼠标实现
void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse)
{
int a,b;
if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
{
if(iPointNum == 0)
{
iPointNum = 1;
xy1[n][0] = xMouse;
xy1[n][1] = winHeight - yMouse;
}
else
{
xy2[n][0] = xMouse;
int mod = glutGetModifiers();
if (mod==GLUT_ACTIVE_CTRL)
{
if(winHeight-yMouse>xy1[n][1])
xy2[n][1] = xy1[n][1]+abs(xy2[n][0]-xy1[n][0]);
else
xy2[n][1] = xy1[n][1]-abs(xy2[n][0]-xy1[n][0]);
}
else
xy2[n][1] = winHeight - yMouse;
n++;
xy1[n][0] = xy2[n-1][0];
xy1[n][1] = xy2[n-1][1];
glutPostRedisplay();
}
}
if(button == GLUT_MIDDLE_BUTTON && action == GLUT_DOWN)
{
iPointNum = 0;
n=0;
i=0;
glutPostRedisplay();
}
}
//键盘实现
void key(unsigned char key, int x, int y)
{
int a,b;
switch(key)
{
case 'p':
if(iPointNum == 0)
{
iPointNum = 1;
xy1[n][0] = x;
xy1[n][1] = winHeight - y; //确定直线的第一个端点
}
else
{
xy2[n][0] = x;
xy2[n][1] = winHeight - y;
n++;
xy1[n][0] = xy2[n-1][0];
xy1[n][1] = xy2[n-1][1];
glutPostRedisplay();
}
break;
case 'c':
{
iPointNum = 0;
n=0;
i=0;
glutPostRedisplay();
}
break;
default: break;
}
}
void processNormalKeys(unsigned char key, int x, int y)
{
}
void SpecialKeys(int key, int x, int y)
{
if(key==GLUT_KEY_UP)
{
xy2[n][1]+=10;
}
if(key==GLUT_KEY_LEFT)
{
xy2[n][0]-=10;
}
if(key==GLUT_KEY_DOWN)
{
xy2[n][1]-=10;
}
if(key==GLUT_KEY_RIGHT)
{
xy2[n][0]+=10;
}
glutPostRedisplay();
}
void PassiveMouseMove(GLint xMouse, GLint yMouse)
{
xy2[n][0] = xMouse;
xy2[n][1] = winHeight - yMouse;
glutPostRedisplay();
}
void ProcessMenu(int value)
{
i = 0;
iMode=value;
//每次画完重新选择模式时都要清除上次画的数据,避免选择完直接绘制
iPointNum = 0;
n=0;
glutPostRedisplay();
glutPostRedisplay();
}
//菜单
void menufuc(void)
{
glClearColor(1.0,1.0,1.0,1.0);
//创建菜单
glutCreateMenu(ProcessMenu);//菜单注册函数
//在菜单中加入菜单项
glutAddMenuEntry("LINE",1);//画直线
glutAddMenuEntry("OVAL",2);//画椭圆
glutAttachMenu(GLUT_RIGHT_BUTTON);//关联菜单和某鼠标按键
}
int main(int argc,char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); //使用双缓存及RGB模型
glutInitWindowSize(400,300); //指定窗口尺寸
glutInitWindowPosition(100 ,100); //指定窗口在屏幕上的位置
glutCreateWindow("椭圆的扫描转换算法的实现");
glutDisplayFunc(Display);
glutReshapeFunc(ChangeSize); //指定窗口再整形回调函数
glutMouseFunc(MousePlot); //指定鼠标响应函数
glutKeyboardFunc(key); //指定键盘响应函数
glutSpecialFunc(SpecialKeys); //指定键盘移动响应函数
glutPassiveMotionFunc(PassiveMouseMove); //指定鼠标移动响应函数
Initial();
//菜单
menufuc();
glutMainLoop();
return 0;
}