-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboundaryFill.c
141 lines (123 loc) · 2.62 KB
/
boundaryFill.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
#include<stdio.h>
#include <stdlib.h>
#include<GL/glut.h>
#include <unistd.h>
#include<math.h>
#define PI 3.1415926535897932384626433832795
double Point[100][100];
int k=0;
int numbPoint=0;
float fillCol[3]={1,1,1};
float borderCol[3]={1,1,1};
void type(char *string, double X, double Y)
{
char *c;
glColor3f(1.0, 1.0, 1.0);
glRasterPos2f(X+1,Y);
for (c=string; *c != '\0'; c++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *c);
}
}
void myName()
{
glClear(GL_COLOR_BUFFER_BIT);
type("Ayush Jain", 5, 990);
type("CSE-CCVT B-1",5, 975);
type("R110217039",5, 960);
glFlush();
}
// function to initialize
void myInit (void)
{
// making background color black as first
// 3 arguments all are 0.0
gluOrtho2D(0,1000,0,1000);
glClearColor(0.0, 0.0, 0.0, 0.0);
myName();
}
void setPixel(float x, float y){
glColor3f(1.0,1.0,1.0);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}
void bfill(int x, int y, float fill[3], float boundary[3])
{
float current[3];
glReadPixels(x, y, 1, 1, GL_RGB,GL_FLOAT, current);
if(
(current[0]!=boundary[0] && current[1]!=boundary[1] && current[2]!=boundary[2]) &&
(current[0]!=fill[0] && current[1]!=fill[1] && current[2]!=fill[2])
)
{
setPixel(x,y);
bfill(x,y-1,fill,boundary);
bfill(x-1,y,fill,boundary);
bfill(x+1,y,fill,boundary);
bfill(x,y+1,fill,boundary);
//bfill(x-1,y-1,fill,boundary);
//bfill(x-1,y+1,fill,boundary);
//bfill(x+1,y-1,fill,boundary);
//bfill(x+1,y-1,fill,boundary);
glFlush();
}
}
void Draw()
{
glLineWidth(3);
glBegin(GL_LINE_LOOP);
for(int i=k; i<numbPoint; i++)
{
glColor3f(1.0,1.0,1.0);
glVertex2f(Point[i][0], Point[i][1]);
}
glEnd();
glFlush();
k = numbPoint;
}
void store(double X, double Y)
{
Point[numbPoint][0] = X;
Point[numbPoint++][1] = Y;
glPointSize(2);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
glVertex2f(X,Y);
glEnd();
glFlush();
}
static void Key(unsigned char key, int x, int y)
{
switch (key)
{
case 32:
Draw();
break;
}
}
void mouseClick(int button, int state, int X, int Y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
store(X,(1000-Y));
}
if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
bfill(X,(1000-Y), fillCol, borderCol);
}
}
int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// giving window size in X- and Y- direction
glutInitWindowSize(1000, 1000);
// Giving name to window
glutCreateWindow("Boundary Fill Algorithm");
myInit();
glutKeyboardFunc(Key);
glutMouseFunc(mouseClick);
glutMainLoop();
return 0;
}