-
Notifications
You must be signed in to change notification settings - Fork 0
/
boundaryfill.c
93 lines (84 loc) · 1.75 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
#include<stdio.h>
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glut.h>
typedef struct Point {
GLint x;
GLint y;
}
Point;
typedef struct Color {
GLfloat r;
GLfloat g;
GLfloat b;
}
Color;
void setpixel(int x, int y)
{
glColor3f(0.0, 1.0, 1.0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
//this function returns the pixel data from the frame buffer
Color getpixel(GLint x, GLint y) {
Color color;
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, &color);
return color;
}
//drawing a polygon to fill
void polygon(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y2);
glVertex2i(x2, y1);
glVertex2i(x3, y4);
glVertex2i(x4, y3);
glVertex2i(x3, y4);
glVertex2i(x4, y3);
glEnd();
glFlush();
}
//display function
void display(void)
{
glClearColor(0.2, 0.4,0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
polygon(150,250,200,300, 350, 421, 534, 564);
glFlush();
}
//actual boundary fill
void boundaryfill(int x, int y, Color fillc, Color boundaryc)
{
Color color;
color = getpixel(x,y);
if(color.r == fillc.r && color.g == fillc.g && color.b == fillc.b)
{
//setcolor(fillc) ;
setpixel(x,y);
boundaryfill(x+1,y,fillc,boundaryc);
boundaryfill(x-1,y,fillc,boundaryc);
boundaryfill(x,y+1,fillc,boundaryc);
boundaryfill(x,y-1,fillc,boundaryc);
}
}
void myinit()
{
glViewport(0,0,600,500);
gluOrtho2D(0.0,(GLdouble)600,0.0,(GLdouble)500);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600,500);
glutCreateWindow("Boundary-Fill-Recursive");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}