-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polygon Filling Using BoundaryFill.cpp
96 lines (85 loc) · 2.26 KB
/
Polygon Filling Using BoundaryFill.cpp
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
#include<windows.h>
#include<GL/glut.h>
#include<bits/stdc++.h>
using namespace std;
float fillColor[3] = {0.0, 1.0, 0.0};
float borderColor[3] = {0.0, 0.0, 1.0};
int xi, yi;
int z1 = 800, z2 = 800;
void polygon(int x1, int y1, int x2, int y2)
{
glColor3fv(borderColor);
glBegin(GL_LINES);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y1);
glVertex2i(x2, y2);
glVertex2i(x1, y1);
glVertex2i(x2, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y2);
glEnd();
glFlush();
}
void display()
{
glClearColor(0.6, 0.5, 0.1, 0.1);
glClear(GL_COLOR_BUFFER_BIT);
polygon(100, 250, 200, 350);
glFlush();
}
void pp(int x, int y, float f[3])
{
glBegin(GL_POINTS);
glColor3fv(f);
glVertex2i(x, y);
glEnd();
glFlush();
}
void gp(int x, int y, float pixels[3])
{
glReadPixels(x, y, 1.0, 1.0, GL_RGB, GL_FLOAT, pixels);
}
void boundaryFill(int x, int y, float fillColor[3], float borderColor[3])
{
float interiorColor[3];
gp(x, y, interiorColor);
if((interiorColor[0] != borderColor[0]) && (interiorColor[1] != borderColor[1]) && (interiorColor[2] != borderColor[2]) &&
(interiorColor[0] != fillColor[0]) && (interiorColor[1] != fillColor[1]) && (interiorColor[2] != fillColor[2]))
{
pp(x, y, fillColor);
boundaryFill(x+1, y, fillColor, borderColor);
boundaryFill(x-1, y, fillColor, borderColor);
boundaryFill(x, y+1, fillColor, borderColor);
boundaryFill(x, y-1, fillColor, borderColor);
}
}
void mouse(int btn, int state, int x, int y)
{
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
xi = x;
yi= z2 - y;
boundaryFill(xi, yi, fillColor, borderColor);
}
}
void myinit()
{
glViewport(0,0,z1,z2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)z1,0.0,(GLdouble)z2);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(z1,z2);
glutCreateWindow("Boundary Fill");
glutDisplayFunc(display);
myinit();
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}