0% found this document useful (0 votes)
41 views2 pages

Write A Program To Create and Fill Two Dimensional Object by Using Boundary Fill Algorithm

This document provides code for a program that uses boundary fill algorithm to fill a 2D shape. It defines functions for initializing the window, drawing a polygon, setting pixel colors, boundary fill recursion. The main function initializes the window and calls display and mouse functions. Display draws the polygon outline. Mouse function calls boundary fill if the user clicks inside the polygon.

Uploaded by

Mohammed Kafeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Write A Program To Create and Fill Two Dimensional Object by Using Boundary Fill Algorithm

This document provides code for a program that uses boundary fill algorithm to fill a 2D shape. It defines functions for initializing the window, drawing a polygon, setting pixel colors, boundary fill recursion. The main function initializes the window and calls display and mouse functions. Display draws the polygon outline. Mouse function calls boundary fill if the user clicks inside the polygon.

Uploaded by

Mohammed Kafeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

5.

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);

//set projection paramaters

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);
}

void mouse(int btn, int state, int x, int y)


{
//This check is based on size of the polygon mentioned in display() function
if(((x<150 || x>200) ||( y<200 || y>250)))
printf("Invalid click !!\n");
else
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
int xi = x;
int yi = (wh-y);
boundaryFill4(xi,yi,fillCol,borderCol);
}
}

You might also like