-
Notifications
You must be signed in to change notification settings - Fork 0
/
Boundary_Fill.c
83 lines (74 loc) · 2.01 KB
/
Boundary_Fill.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
#include <windows.h>
#include<GL/glut.h>
#include<math.h>
#include<stdio.h>
int xmin, ymin, xmax, ymax;
float fill[3] = {0.0, 255, 0.0};
float border[3] = {255, 0.0, 0.0};
void Color_Pixel(int x, int y)
{
glBegin(GL_POINTS);
glColor3fv(fill);
glVertex2i(x, y);
glEnd();
glFlush();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3fv(border);
glLineWidth(5);
glBegin(GL_LINES);
glVertex2i(xmin, ymin); glVertex2i(xmin, ymax);
glVertex2i(xmax, ymin); glVertex2i(xmax, ymax);
glVertex2i(xmin, ymin); glVertex2i(xmax, ymin);
glVertex2i(xmin, ymax); glVertex2i(xmax, ymax);
glEnd();
glFlush();
}
void BoundaryFill(int x,int y)
{
float curr[3];
glReadPixels(x, y, 1.0, 1.0, GL_RGB, GL_FLOAT, curr);
if((curr[0] != border[0] && (curr[1]) != border[1] &&
(curr[2])!= border[2]) && (curr[0] != fill[0] &&
(curr[1]) != fill[1] && (curr[2]) != fill[2]))
{
Color_Pixel(x, y);
BoundaryFill(x+1, y);
BoundaryFill(x-1, y);
BoundaryFill(x, y+1);
BoundaryFill(x, y-1);
BoundaryFill(x+1, y+1);
BoundaryFill(x+1, y-1);
BoundaryFill(x-1, y+1);
BoundaryFill(x-1, y-1);
}
}
void mouse(int btn, int state, int x, int y)
{
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
BoundaryFill(x, 500-y);
}
}
void init()
{
glClearColor(0.101, 1.0, 0.980, 1.0); //Background color - cyan
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 500, 0, 500);
}
int main(int argc, char** argv)
{
printf("Enter Polygon Coordinates: ");
scanf("%d%d%d%d", &xmin ,&ymin,&xmax,&ymax);
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Boundary-Fill Algorithm");
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}