COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT [18CSL67]
[DOCUMENT TITLE]
PART – A
Brenham’s line drawing algorithm
1. Implement Brenham’s line drawing algorithm for all types of slop.
Bresenham’s line algorithm is an accurate and efficient raster line-generating algorithm,
developed by Bresenham, that uses only incremental integer calculations. In addition,
Bresenham’s line algorithm can be adapted to display circles and other curves.
For m > 1, find out whether you need to increment x while incrementing y each time.
After solving, the equation for decision parameter Pk will be very similar, just the x and y in
the equation gets interchanged.
Purpose: To demonstrate Bresenham’s line algorithm to draw lines.
Procedure: To read end points of lines from the user to demonstrate the working of a
Bresenham’s line algorithm
Input: End points of the line.
Expected Output: Draw a line between end points.
1.1Program
/*Implement Bresenham’s line drawing algorithm for all types
of slope.*/
#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
Page | 9
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT [18CSL67]
[DOCUMENT TITLE]
int xx,yy,xend,yend;
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
glMatrixMode(GL_MODELVIEW);
}
void setPixel(int x,int y)
{
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
glFlush();
}
void Bresenhamline()
{
int p, x, y;
//to draw line from right to left
if(xx>xend)
{
x=xend;
y=yend;
xend=xx;
yend=yy;
}
else{//to draw line from left to right
x=xx;
y=yy;
}
int dx=abs(xend-x), dy=abs(yend-y);
int twody=2*dy, twodyMinustwodx =2*(dy-dx);
int twodx=2*dx, twodxMinustwody =2*(dx-dy);
glColor3f(1,0,0); // Set color to red.
glPointSize(2); //Set point size to 3
if(dx > dy)
{
//For slope m<1
p=2*dy-dx;
setPixel(x,y);
while(x<xend)
{
x=x+1;
if(p<0)
p=p+twody;
else
{
y=y+1;
p=p+twodyMinustwodx;
}
Page | 10
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT [18CSL67]
[DOCUMENT TITLE]
setPixel(x,y);
}
}
else
{
//For slope m>1
p=2*dx-dy;
setPixel(x,y);
while(y<yend)
{
y=y+1;
if(p<0)
p=p+twodx;
else
{
x=x+1;
p=p+twodxMinustwody;
}
setPixel(x,y);
}
}
}
void display()
{
glClearColor(1,1,1,1); //Specifies a background
RGB color for a display window.
glClear(GL_COLOR_BUFFER_BIT);// Clear display window.
glViewport(300,300,100,100);
Bresenhamline();
glFlush(); // Process all OpenGL routines as quickly
as possible.
}
void main()
{
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);// Set
display mode.
glutInitWindowPosition(100,100); // Set
top-left display-window position.
glutInitWindowSize(500,500); // Set
display-window width and height.
glutCreateWindow("Bresenham's Line Drawing
Algorthm");// Create display window.
myinit();
// Execute initialization procedure.
printf("Enter co-ordinates of first point: ");
scanf("%d %d", &xx, &yy);
printf("Enter co-ordinates of second point: ");
scanf("%d %d", &xend, ¥d);
Page | 11
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT [18CSL67]
[DOCUMENT TITLE]
glutDisplayFunc(display);
//Invokes a function to create a picture within the
current display window.
glutMainLoop();
//Executes the computer-graphics program.
}
1.2 Output
Page | 12
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT [18CSL67]
[DOCUMENT TITLE]
Create and Rotate a Triangle
2. Create and rotate a triangle about the origin and a fixed point
Purpose: To demonstrate 2D geometric transformations.
Procedure: To read angle of rotation and point of rotation from the user to demonstrate the
transformation of triangle.
Input: Angle of rotation and point of rotation.
Expected Output: The triangle is drawn and rotated by given angle of rotation about the fixed
point given.
2.1 Program
//Program 2 Rotate a Triangle
#include<stdlib.h>
#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
float p[9][2]={{20,20},{40,40},{60,20}};
float xp,yp,theta,rtheta;
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100,100,-100,100);
glMatrixMode(GL_MODELVIEW);
}
void drawtraiangle()
{
glBegin(GL_TRIANGLES);
glVertex2fv(p[0]);
glVertex2fv(p[1]);
glVertex2fv(p[2]);
glEnd();
}
void display()
{
float x,y;
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
drawtraiangle();
rtheta=(theta*3.142)/180;
Page | 13
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT [18CSL67]
[DOCUMENT TITLE]
for(int i=0;i<3;i++)
{
x=p[i][0];
y=p[i][1];
p[i][0]= xp + (x-xp)*cos(rtheta) - (y-
yp)*sin(rtheta);
p[i][1]= yp + (x-xp)*sin(rtheta) + (y-
yp)*cos(rtheta);
}
glColor3f(0,1,0);
drawtraiangle();
glFlush();
void main()
{
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("Traingle rotation");
myinit();
printf("Enter the point of rotation\n");
scanf("%f%f",&xp,&yp);
printf("Enter the angle of rotation\n");
scanf("%f",&theta);
glutDisplayFunc(display);
glutMainLoop();
}
Page | 14
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT [18CSL67]
[DOCUMENT TITLE]
2.2 Output
Page | 15