-
Notifications
You must be signed in to change notification settings - Fork 0
/
rectangle using floodfill.cpp
131 lines (101 loc) · 2.46 KB
/
rectangle using floodfill.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
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
#include <iostream>
#include <GL/glut.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
int stx1,sty1,enx2,eny2;
struct Point {
GLint x;
GLint y;
};
struct Color {
GLfloat r;
GLfloat g;
GLfloat b;
};
void initial_value() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
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 floodFill_4dir(GLint x, GLint y, Color oldColor, Color newColor) {
Color color;
color = getPixelColor(x, y);
if(color.r == oldColor.r && color.g == oldColor.g && color.b == oldColor.b)
{
setPixelColor(x, y, newColor);
floodFill_4dir(x+1, y, oldColor, newColor);
floodFill_4dir(x, y+1, oldColor, newColor);
floodFill_4dir(x-1, y, oldColor, newColor);
floodFill_4dir(x, y-1, oldColor, newColor);
}
return;
}
void clickTheMouse(int button, int state, int x, int y)
{
Color newColor = {1.0f, 0.0f, 0.0f};
Color oldColor = {1.0f, 1.0f, 1.0f};
GLfloat pntstx1 = (stx1+enx2)/2;
GLfloat pntsty1 = (sty1+eny2)/2;
floodFill_4dir(pntstx1, pntsty1, oldColor, newColor);
}
void pointPlot(GLfloat x, GLfloat y){
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void draw_boundary(Point st, Point en) {
for(GLfloat i=st.x; i<=en.x; i = i + 0.002){
pointPlot(i,st.y);
}
for(GLfloat i=st.x; i<=en.x; i = i + 0.002){
pointPlot(i,en.y);
}
for(GLfloat i=st.y; i<=en.y; i = i + 0.002){
pointPlot(en.x,i);
}
for(GLfloat i=st.y; i<=en.y; i = i + 0.002){
pointPlot(st.x,i);
}
}
void displayMe(void) {
Point st = {stx1,sty1};
Point en = {enx2,eny2};
glClear(GL_COLOR_BUFFER_BIT);
// glBegin(GL_POINTS);
draw_boundary(st, en);
// glEnd();
glFlush();
}
int main(int argc, char** argv)
{
cout<<"Enter lower left coordinate of Rectangle : ";
cin>>stx1>>sty1;
cout<<"Enter upper right coordinate of Rectangle : ";
cin>>enx2>>eny2;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);
glutCreateWindow ("Rectangle Drawing using FloodFill");
initial_value();
glutDisplayFunc(displayMe);
glutMouseFunc(clickTheMouse);
glutMainLoop();
return 0;
}