-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.txt
102 lines (102 loc) · 1.82 KB
/
9.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
//8. ScanlinePolygonFill.cpp
#include<stdio.h>
#include<GL/glut.h>
float le[400], re[400], flag = 0, m;
void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(0, 400, 0, 400);
}
void edgeDetect(int x0, int y0, int x1, int y1)
{
int x, i;
if (y1 < y0)
{
y1 = y1 + y0;
y0 = y1 - y0;
y1 = y1 - y0;
x1 = x1 + x0;
x0 = x1 - x0;
x1 = x1 - x0;
}
x = x0;
m = (y1 - y0) / (x1 - x0);
for (i = y0; i < y1; i++)
{
if (x < le[i])
le[i] = x;
if (x > re[i])
re[i] = x;
x += (1 / m);
}
}
void display()
{
int i, j;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glBegin(GL_LINE_LOOP);
glVertex2i(200, 100);
glVertex2i(300, 200);
glVertex2i(200, 300);
glVertex2i(100, 200);
glEnd();
for (i = 0; i < 400; i++)
{
le[i] = 400;
re[i] = 0;
}
edgeDetect(200, 100, 300, 200);
edgeDetect(300, 200, 200, 300);
edgeDetect(200, 300, 100, 200);
edgeDetect(100, 200, 200, 100);
if (flag == 1)
{
for (i = 0; i < 400; i++)
{
if (le[i] < re[i])
{
for (j = le[i]; j < re[i]; j++)
{
glPointSize(2.0);
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_POINTS);
glVertex2i(j, i);
glEnd();
glFlush();
}
}
}
}
glFlush();
}
void menuid(int id)
{
switch (id)
{
case 1: flag = 1;
display();
break;
case 2: flag = 0;
display();
break;
case 3: exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("SCAN LINE POLYGON FILL");
init();
glutDisplayFunc(display);
glutCreateMenu(menuid);
glutAddMenuEntry("Fill Polygon", 1);
glutAddMenuEntry("Clear Polygon", 2);
glutAddMenuEntry("Exit", 3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0;
}