-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filling.txt
101 lines (78 loc) · 1.81 KB
/
Filling.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
#include<GL/glut.h>
float x1, x2, x3, x4, y1, y2, y3, y4;
void draw_pixel(int x, int y)
{
glColor3f(0.0, 1.0, 0.0);
glPointSize(1.0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void edgedetect(float x1, float y1, float x2, float y2, int* le, int* re)
{
float temp, x, mx;
int i;
if (y1 > y2)
{
temp = x1, x1 = x2, x2 = temp;
temp = y1, y1 = y2, y2 = temp;
}
if (y1 == y2)
mx = x2 - x1;
else
mx = (x2 - x1) / (y2 - y1);
x = x1;
for (i = int(y1); i <= (int)y2; i++)
{
if (x < (float)le[i]) le[i] = (int)x;
if (x > (float)re[i]) re[i] = (int)x;
x += mx;
}
}
void scanfill(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
{
int le[500], re[500], i, j;
for (i = 0; i < 500; i++)
le[i] = 500, re[i] = 0;
edgedetect(x1, y1, x2, y2, le, re);
edgedetect(x2, y2, x3, y3, le, re);
edgedetect(x3, y3, x4, y4, le, re);
edgedetect(x4, y4, x1, y1, le, re);
for (j = 0; j < 500; j++)
{
if (le[j] <= re[j])
for (i = le[j]; i < re[j]; i++)
draw_pixel(i, j);
}
}
void display()
{
x1 = 250.0; y1 = 200.0; x2 = 150.0; y2 = 300.0; x3 = 250.0;
y3 = 400.0; x4 = 350.0; y4 = 300.0;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
glVertex2f(100, 300);
glVertex2f(200, 100);
glVertex2f(200, 200);
glVertex2f(100, 200);
glEnd();
scanfill(100, 300, 200, 100, 200, 200, 100, 200);
glFlush();
}
void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 499.0, 0.0, 499.0);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("scanline");
glutDisplayFunc(display);
init();
glutMainLoop();
}