forked from sprintr/opengl-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OpenGL-BoundaryFill-Circle.cpp
94 lines (79 loc) · 2.01 KB
/
OpenGL-BoundaryFill-Circle.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
#include <math.h>
#include <gl/glut.h>
struct Point {
GLint x;
GLint y;
};
struct Color {
GLfloat r;
GLfloat g;
GLfloat b;
};
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 640, 0, 480);
}
Color getPixelColor(GLint x, GLint y) {
Color color;
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, &color);
return color;
}
void setPixelColor(GLint x, GLint y, Color color) {
glColor3f(color.r, color.g, color.b);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
void BoundaryFill(int x, int y, Color fillColor, Color boundaryColor) {
Color currentColor = getPixelColor(x, y);
if(currentColor.r != boundaryColor.r && currentColor.g != boundaryColor.g && currentColor.b != boundaryColor.b) {
setPixelColor(x, y, fillColor);
BoundaryFill(x+1, y, fillColor, boundaryColor);
BoundaryFill(x-1, y, fillColor, boundaryColor);
BoundaryFill(x, y+1, fillColor, boundaryColor);
BoundaryFill(x, y-1, fillColor, boundaryColor);
}
}
void onMouseClick(int button, int state, int x, int y)
{
Color fillColor = {1.0f, 0.0f, 0.0f}; // red color will be filled
Color boundaryColor = {0.0f, 0.0f, 0.0f}; // black- boundary
Point p = {321, 241}; // a point inside the circle
BoundaryFill(p.x, p.y, fillColor, boundaryColor);
}
void draw_circle(Point pC, GLfloat radius) {
GLfloat step = 1/radius;
GLfloat x, y;
for(GLfloat theta = 0; theta <= 360; theta += step) {
x = pC.x + (radius * cos(theta));
y = pC.y + (radius * sin(theta));
glVertex2i(x, y);
}
}
void display(void) {
Point pt = {320, 240};
GLfloat radius = 20;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
draw_circle(pt, 50);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);
glutCreateWindow("Open GL");
init();
glutDisplayFunc(display);
glutMouseFunc(onMouseClick);
glutMainLoop();
return 0;
}