-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloodFill.c
More file actions
115 lines (108 loc) · 2.54 KB
/
FloodFill.c
File metadata and controls
115 lines (108 loc) · 2.54 KB
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
AIM:- WAP to fill a region using flood fill algorithm using 4 or 8 connected approaches.
CODE:-
#include<GL/glut.h>
#include<stdio.h>
#include<stdbool.h>
int x,y,z;
int numberOfPoints;
float ox = 200,oy = 200;//Origin Coordinates
int count = 0;
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-200,200,-200,200.0);
}
void drawLine(float x1, float y1, float x2, float y2, float R, float G, float B){
glColor3f(R,G,B);
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
}
void getPixelColor(int x, int y, float *pixel){
glReadPixels(x+oy, y+ox, 1.0, 1.0, GL_RGB, GL_FLOAT, pixel);
}
bool arrCmp(float *arr1, float *arr2, int size){
for(int i=0;i<size;i++){
if(arr1[i] != arr2[i])
return false;
}
return true;
}
void plotPoint(int x,int y, float R, float G, float B)
{
glColor3f(R,G,B);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
glFlush();
}
void drawPolygon(int (*arr)[2], int n, float R, float G, float B){
glColor3f(R,G,B);
glBegin(GL_POLYGON);
for(int i = 0; i < n; i++)
glVertex2i(arr[i][0],arr[i][1]);
glEnd();
}
void floodFill4(int x, int y, float *fill, float *old){
float current[3];
getPixelColor(x,y,current);
if(arrCmp(old,current,3)){
plotPoint(x,y,fill[0],fill[1],fill[2]);
floodFill4(x+1,y,fill,old);
floodFill4(x-1,y,fill,old);
floodFill4(x,y+1,fill,old);
floodFill4(x,y-1,fill,old);
}
}
void mousePlotPoint(GLint button, GLint action, GLint xMouse, GLint yMouse)
{
GLint wh=400;
float x=xMouse-ox;
float y=wh-yMouse-oy;
static int arr[3][2];
if(button == GLUT_LEFT_BUTTON && action == GLUT_UP)
{
if(count == -1){
float fill[] = {x,y,z};
float old[3];
getPixelColor(x,y,old);
floodFill4(x,y,fill,old);
}
else if(count == 2){
printf("\nPoint: (%f, %f)",x, y);
arr[2][0] = x;
arr[2][1] = y;
count++;
drawPolygon(arr,count,1,0,0);
count = -1;
}
else{
printf("\nPoint: (%f, %f)",x, y);
arr[count][0] = x;
arr[count][1] = y;
count++;
}
}
glFlush();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
}
int main(int argc, char** argv)
{ printf("Enter RGB values for new colour= ");
//std::cin>>x>>y>>z;
scanf("%d%d%d", &x, &y, &z);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(400,400);
glutCreateWindow("Flood Fill");
init();
glutDisplayFunc(display);
glutMouseFunc(mousePlotPoint);
glutMainLoop();
return 0;
}