0% found this document useful (0 votes)
13 views11 pages

2021BCS0103 CSE411 Lab5

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

2021BCS0103 CSE411 Lab5

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

CSE 411 COMPUTER GRAPHICS LAB-5

Name-S.Vaishnavi Reddy
Roll no-2021BCS0103
Batch-1
Q) Fill a rhombus using scanline polygon
filling algorithm.

Code:
#include <windows.h>
#include <GL/glut.h>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// Struct to define a point (vertex)


struct Point_of_rhombus {
float x, y;
};
// Define the vertices of the rhombus
// Center the rhombus around (200, 200)
Point_of_rhombus rhombus[4] = {
{200, 300}, // Top
{300, 200}, // Right
{200, 100}, // Bottom
{100, 200} // Left
};

int ymin = 100, ymax = 300;

// Function to draw a point at (x, y)


void func_drawPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

// Function to draw the rhombus edges


void func_drawPolygon(Point_of_rhombus
poly[], int n) {
glBegin(GL_LINE_LOOP);
for (int i = 0; i < n; i++) {
glVertex2i(poly[i].x, poly[i].y);
}
glEnd();
}

// Scanline algorithm to fill the rhombus


void func_scanlineFill(Point_of_rhombus
poly[], int n) {
vector<int> intersections;

// Loop through each scanline (horizontal


line)
for (int y = ymin; y <= ymax; y++) {
intersections.clear();

// Check each edge for intersections with


the current scanline
for (int i = 0; i < n; i++) {
Point_of_rhombus p1 = poly[i];
Point_of_rhombus p2 = poly[(i + 1) %
n]; // Wrap around to the first point after the
last

// Check if the scanline crosses the


edge
if ((p1.y <= y && p2.y > y) || (p2.y <= y
&& p1.y > y)) {
// Calculate the x-coordinate of the
intersection
float x = p1.x + (y - p1.y) * (p2.x -
p1.x) / (p2.y - p1.y);

intersections.push_back(static_cast<int>(x));
}
}

// Sort the intersections


sort(intersections.begin(),
intersections.end());

// Fill between pairs of intersections


for (int i = 0; i < intersections.size(); i += 2)
{
if (i + 1 < intersections.size()) {
for (int x = intersections[i]; x <=
intersections[i + 1]; x++) {
func_drawPixel(x, y);
}
}
}
}
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear
the screen
glColor3f(1.0, 1.0, 1.0); // Set drawing
color to white for the rhombus border

// Draw the outline of the rhombus


func_drawPolygon(rhombus, 4);

// Set color for filling


glColor3f(1.0, 1.0, 0.0);

// Call the scanline fill function to fill the


rhombus
func_scanlineFill(rhombus, 4);

glFlush(); // Ensure everything is drawn


}
// Initialize OpenGL settings
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0); // Set
background color to black
glMatrixMode(GL_PROJECTION); //
Switch to projection mode
gluOrtho2D(0, 400, 0, 400); // Set up
the orthographic view with a 400x400
window
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB); // Single buffering and RGB color
mode
glutInitWindowSize(400, 400); //
Set window size
glutInitWindowPosition(100, 100); //
Set window position on screen
glutCreateWindow("Drawing Rhombus
using Centered Scanline Polygon Fill
Algorithm"); // Create window with title
init(); // Initialize
OpenGL settings
glutDisplayFunc(display); // Set
the display callback for when window needs
to be redrawn
glutMainLoop(); // Start the
main event loop
return 0;
}
Output

You might also like