Write A Program To Create and Fill Two Dimensional Object by Using Boundary Fill Algorithm
Write A Program To Create and Fill Two Dimensional Object by Using Boundary Fill Algorithm
Write a program to create and fill two dimensional object by using boundary fill
algorithm.
#include <GL/glut.h>
#include<stdio.h>
int ww = 600, wh = 500;
float fillCol[3] ={0.4,0.0,0.0};
float borderCol[3] = {0.0,0.0,0.0};
void setPixel(int, int, float[]);
void init();
void drawPolygon(int, int, int, int);
void boundaryFill4(int,int,float[],float[]);
void display();
void mouse(int, int, int, int);
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(ww,wh);
glutInitWindowPosition(500, 50);
glutCreateWindow("Bountry-Fill-Recursive");
glutDisplayFunc(display);
glutMouseFunc(mouse);
//calls whenever frame buffer window is resized
init();
glutMainLoop();
}
void setPixel(int pointx, int pointy, float f[3])
{
glBegin(GL_POINTS);
glColor3fv(f);
glVertex2i(pointx,pointy);
glEnd();
glFlush();
}
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,600.0,0.0,500.0);
}
void drawPolygon(int x1, int y1, int x2, int y2)
{
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2,y2);
glVertex2i(x2,y1);
glEnd();
glFlush();
void display()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
//If you modify following values, u should change condition in mouse() also.
drawPolygon(150,250,200,300);
glFlush();
}
void boundaryFill4(int x,int y,float fillColor[3],float borderColor[3])
{
float interiorColor[3];
glReadPixels(x,y,1.0,1.0,GL_RGB,GL_FLOAT,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])))
{
setPixel(x,y,fillColor);
boundaryFill4(x+1,y,fillColor,borderColor);
boundaryFill4(x-1,y,fillColor,borderColor);
boundaryFill4(x,y+1,fillColor,borderColor);
boundaryFill4(x,y-1,fillColor,borderColor);
}