-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPolygonFilling.c
161 lines (93 loc) · 2.53 KB
/
PolygonFilling.c
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
#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
int v, p, q, input[2][10], x, y;
float boundarycolor[3], fillcolor[3];
int Round(int f){
return f + 0.5;
}
void dda(int x1, int y1, int x2, int y2){
int dx, dy, steps;
dx = x2 - x1;
dy = y2 - y1;
if(abs(dx) > abs(dy)){
steps = abs(dx);
}else{
steps = abs(dy);
}
float xinc, yinc;
xinc = (float)(dx)/steps;
yinc = (float)(dy)/steps;
float x = x1, y = y1;
glVertex2d(Round(x),Round(y));
for(int i = 0; i < steps; i++){
x = x + xinc;
y = y + yinc;
glVertex2d(Round(x), Round(y));
}
}
void boundaryfill(int p, int q, float boundarycolor[3], float fillcolor[3]){
float color[3];
glReadPixels(p, q, 1.0, 1.0, GL_RGB, GL_FLOAT, color);
/*for(int i = 0; i<3; i++){
printf("%f\t", color[i]);
}*/
printf("\n");
if((color[0]!=boundarycolor[0]) || (color[1]!=boundarycolor[1]) || (color[1]!=boundarycolor[1])){
glColor3f(fillcolor[0], fillcolor[1], fillcolor[2]);
glVertex2d(x, y);
boundaryfill(p+1, q, boundarycolor, fillcolor);
boundaryfill(p-1, q, boundarycolor, fillcolor);
boundaryfill(p, q+1, boundarycolor, fillcolor);
boundaryfill(p, q-1, boundarycolor, fillcolor);
}
}
void display_point(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
//glVertex2d(100,100);
//dda(0,0,300,300);
int j;
for(j = 0; j < v-1; j++){
dda(input[0][j], input[1][j], input[0][j+1], input[1][j+1]);
}
dda(input[0][j], input[1][j], input[0][0], input[1][0]);
boundaryfill(p, q, boundarycolor, fillcolor);
glEnd();
glFlush();
}
int main(int argc, char **argv){
printf("Vetices: ");
scanf("%d", &v);
//Coordinates for shape
for(int x = 0; x < v; x++){
printf(" X-coordinates = ");
scanf("%d", &input[0][x]);
printf(" Y-coordinates = ");
scanf("%d", &input[1][x]);
}
//Change pixel color
printf(" Enter 3 bit values for pixel fill color (R,G,B) = ");
for(int x = 0; x < 3; x++){
scanf("%f", &fillcolor[x]);
}
//Inside Pixel
printf(" Coordinate for inside pixel = ");
scanf("%d %d", &p, &q);
//Background Fill Colour
printf(" Enter values for background fill color = ");
for(int x = 0; x < 3; x++){
scanf("%f", &boundarycolor[x]);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(640, 480);
glutCreateWindow("Polygon Filling");
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(boundarycolor[0], boundarycolor[1], boundarycolor[2]);//Boundary
gluOrtho2D(0, 640, 0, 480);
glutDisplayFunc(display_point);
glutMainLoop();
return 0;
}