2bfajnyrcoCG Assignment 4
2bfajnyrcoCG Assignment 4
Class: CS SY Div: C
Roll no.: 3
PRN: 12310137
CGAVR Assignment 4
A) Boundary Fill
Code:
#include <GL/glut.h>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#include <iostream>
while (true) {
glBegin(GL_POINTS);
glVertex2i(x1, y1);
glEnd();
boundaryPoints.insert({x1, y1});
while (!pointStack.empty()) {
pair<int, int> currentPoint = pointStack.top();
pointStack.pop();
int x = currentPoint.first;
int y = currentPoint.second;
if (boundaryPoints.find(currentPoint) != boundaryPoints.end() ||
filledPoints.find(currentPoint) != filledPoints.end() ||
!isPointInsidePolygon(x, y)) {
continue;
}
filledPoints.insert(currentPoint);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
pointStack.push({x + 1, y});
pointStack.push({x - 1, y});
pointStack.push({x, y + 1});
pointStack.push({x, y - 1});
pointStack.push({x + 1, y + 1});
pointStack.push({x + 1, y - 1});
pointStack.push({x - 1, y + 1});
pointStack.push({x - 1, y - 1});
}
glFlush();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
fillPolygon(0, 0);
glFlush();
}
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glPointSize(3.0f);
gluOrtho2D(-100, 100, -100, 100);
}
void takeInput() {
int n;
cout << "Enter the number of vertices: ";
cin >> n;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Manual 8-Neighbor Polygon Fill with Point Inside Check");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
A) Flood Fill
Code:
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
// Window size
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
typedef struct
{
float r, g, b;
} Color;
int numVertices;
int vertices[100][2];
// (8-connected)
floodFill(x + 1, y, oldColor, newColor);
floodFill(x - 1, y, oldColor, newColor);
floodFill(x, y + 1, oldColor, newColor);
floodFill(x, y - 1, oldColor, newColor);
floodFill(x+1, y + 1, oldColor, newColor);
floodFill(x-1, y - 1, oldColor, newColor);
floodFill(x+1, y - 1, oldColor, newColor);
floodFill(x+1, y + 1, oldColor, newColor);
}
}
void getPolygonCoordinates()
{
printf("Enter the number of vertices: ");
scanf("%d", &numVertices);
if(numVertices < 3)
{
printf("A polygon must have at least 3 vertices!\n");
exit(1);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
for(int i = 0; i < numVertices; i++)
{
glVertex2i(vertices[i][0], vertices[i][1]);
}
glEnd();
glFlush();
// Initialization function
void init()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // White background
//glColor3f(0.0f, 0.0f, 0.0f); // Black color for drawing
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT);
}
// Main function
int main(int argc, char** argv)
{
// Get the polygon coordinates from the user
getPolygonCoordinates();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Flood Fill on Any Polygon");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output: