CG Lab 05

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Lab Report-05

Course Code: CSE 422


Course Title: Computer Graphics Lab

Submitted To:
Md Shakil Ahmed(MSA)
Lectural
Department of CSE
Daffodil International University

Submitted by:
Name: Mir Saem Hasan
ID: 211-15-4066
Section: 59_C1
Department of CSE
Daffodil International University
Report: Bresenham's Line Drawing Algorithm in OpenGL
1. Objective
The objective of this lab is to understand and implement Bresenham's Line Drawing Algorithm
using OpenGL. The algorithm is used to efficiently draw lines on a raster display by using
integer calculations.

2.Theory:
Bresenham’s algorithm is an efficient way to draw a straight line between two points on a raster
grid. It avoids the use of floating-point arithmetic by using only integer operations, which are
faster and more suited for pixel-based screens.

Given two endpoints (x0,y0)(x_0, y_0)(x0​,y0​) and (x1,y1)(x_1, y_1)(x1​,y1​), the goal is to
determine which pixels should be plotted to form a close approximation of the line.

3. Code-I (User input)


#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
float x1, y1, x2, y2, m, i, j, p;
int dx = 0, dy = 0;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0); // Line color set to red
glBegin(GL_POINTS);
p = 2 * dy - dx;
for(i = x1, j = y1; i <= x2;)
{
if(p >= 0)
{
i = i + 1;
j = j + 1;
if((i > x2) && (j > y2))
{
break;
}
printf("%.2f %.2f\n", i, j);
glVertex3f((i / 100), (j / 100), 0.0);
p = p + 2 * (dy - dx);
}
else
{
i = i + 1;
if((i > x2) && (j > y2))
{
break;
}
printf("%.2f %.2f\n", i, j);
glVertex3f((i / 100), (j / 100), 0.0);
p = p + 2 * dy;
}
}
glEnd();
glFlush();
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0); // Background color set to white
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int argc, char** argv)
{
printf("Enter the first point: \n");
scanf("%f %f", &x1, &y1);
printf("Enter the second position: \n");
scanf("%f %f", &x2, &y2);
dx = x2 - x1;
dy = y2 - y1;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Lab-5 Saem");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

4. Code-II (Fixed point)


#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h
float x1 = 10, y1 = 20, x2 = 87, y2 = 86; // Fixed points
float m, i, j, p;
int dx = 0, dy = 0;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0); // Line color set to red
glBegin(GL_POINTS);
p = 2 * dy - dx;
for(i = x1, j = y1; i <= x2;)
{
if(p >= 0)
{
i = i + 1;
j = j + 1;
if((i > x2) && (j > y2))
{
break;
}
printf("%.2f %.2f\n", i, j);
glVertex3f((i / 100), (j / 100), 0.0);
p = p + 2 * (dy - dx);
}
else
{
i = i + 1;
if((i > x2) && (j > y2))
{
break;
}
printf("%.2f %.2f\n", i, j);
glVertex3f((i / 100), (j / 100), 0.0);
p = p + 2 * dy;
}
}
glEnd();
glFlush();
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0); // Background color set to white
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int argc, char** argv)
{
// Fixed coordinates
x1 = 10;
y1 = 20;
x2 = 87;
y2 = 86;
dx = x2 - x1;
dy = y2 - y1;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Lab-5 Saem");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

4. Output:
Upon running the program, a 500x500 pixel window opens displaying its Bresenham’s
algorithm.

Fig:01 User input


Fig:02 Fixed point

6. Conclusion
The DDA algorithm is effective for drawing lines by incrementing coordinates based on the In
this lab, we implemented Bresenham’s Line Drawing Algorithm in OpenGL. The algorithm
allows for efficient rasterization of lines using only integer operations, making it ideal for
drawing lines on pixel-based displays. The OpenGL implementation demonstrates the practical
application of this algorithm in computer graphics.

You might also like