0% found this document useful (0 votes)
39 views

Computer Graphics Assignment: Bresenham Line Algorithm

This document describes an assignment to implement the Bresenham line algorithm in C++ using OpenGL for computer graphics. It includes function definitions and code to draw lines on a 2D plane between given points based on the slope of the line. The main steps are initializing OpenGL, setting up pixel drawing functions, and implementing four cases of the Bresenham algorithm depending on the slope of the line.

Uploaded by

anon_616898497
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Computer Graphics Assignment: Bresenham Line Algorithm

This document describes an assignment to implement the Bresenham line algorithm in C++ using OpenGL for computer graphics. It includes function definitions and code to draw lines on a 2D plane between given points based on the slope of the line. The main steps are initializing OpenGL, setting up pixel drawing functions, and implementing four cases of the Bresenham algorithm depending on the slope of the line.

Uploaded by

anon_616898497
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

COMPUTER GRAPHICS

ASSIGNMENT
Bresenham Line Algorithm

Name-Utkarsh Baranwal
Year-3rd
Class-B.E. COE
Roll No. 101403191

#include<GL/glut.h>
#include <stdlib.h>
#include<math.h>
#include<iostream>
using namespace std;
double xs=300,xEnd=100,ys=50,yEnd=340;

void init(void)
{
/* Set clear color to white */
glClearColor(1.0,1.0,1.0,0);
/* Set fill color to black */
glColor3f(0.0,0.0,0.0);

gluOrtho2D(0 , 640 , 0 , 480);


}
void setPixel(int xCoordinate, int yCoordinate)
{
glBegin(GL_POINTS);
glVertex2i(xCoordinate,yCoordinate);
glEnd();
glFlush(); //executes all OpenGL functions as quickly as possible
}
//
////Bresenham line-drawing procedure for |m| < 1.0
//void lineBres(int xs, int ys, int xEnd, int yEnd)
//{
//int dx = fabs(xEnd - xs);
//int dy = fabs(yEnd - ys);
//int p = 2 * dy - dx;

//int twoDy = 2 * dy;


//int twoDyMinusDx = 2 * (dy-dx);
//int x,y;
//// determine which endpoint to use as start position
//if (xs > xEnd){
//x = xEnd;
//y = yEnd;
//xEnd = x;
//}else{
//x = xs;
//y = ys;
//}
//setPixel(x,y);
//while(x<xEnd){
//x++;
//if(p<0)
//p += twoDy;
//else{
//y++;
//p += twoDyMinusDx;
//}
//setPixel(x,y);
//}
//}

//------------------------------------------------------------------------void bresenham1()
{
if(xs>xEnd)
{
float temp;
temp = xs;

xs = xEnd;
xEnd = temp;
temp = ys;
ys = yEnd;
yEnd = temp;
}
int x = xs, y = ys;
int dx = xEnd-xs;
int dy = yEnd-ys;
int dT = 2*(dy-dx);
int dS = 2*dy;
int d = 2*dy-dx;

setPixel(x,y);
while(x<xEnd)
{
x++;
if(d<0)
{
d = d+dS;
}
else
{
d = d+dT;
y++;
}
setPixel(x,y);
}
setPixel(xEnd,yEnd);
}

void bresenham2()
{
if(xs>xEnd)
{
float temp;
temp = xs;
xs = xEnd;
xEnd = temp;
temp = ys;
ys = yEnd;
yEnd = temp;
}
int x = xs, y = ys;
int dx = xEnd-xs;
int dy = yEnd-ys;
int dT = 2*(dy+dx);
int dS = 2*dy;
int d = -(2*dx+dy);

setPixel(x,y);
while(x<xEnd)
{
x++;
if(d<0)
{
d = d-dS;
}
else
{
y--;
d = d-dT;
}

setPixel(x,y);
}
setPixel(xEnd,yEnd);
}

void bresenham3()
{
if(ys>yEnd)
{
float temp;
temp = xs;
xs = xEnd;
xEnd = temp;
temp = ys;
ys = yEnd;
yEnd = temp;
}
int x = xs, y = ys;
int dx = xEnd-xs;
int dy = yEnd-ys;
int dT = 2*(dx-dy);
int dS = 2*dx;
int d = 2*dx-dy;

setPixel(x,y);
while(y<yEnd)
{
y++;
if(d<0)
{
d = d+dS;
}

else
{
x++;
d = d+dT;
}
setPixel(x,y);
}
setPixel(xEnd,yEnd);
}

void bresenham4()
{
if(ys>yEnd)
{
float temp;
temp = xs;
xs = xEnd;
xEnd = temp;
temp = ys;

ys = yEnd;
yEnd = temp;
}
int x = xs, y = ys;
int dx = xEnd-xs;
int dy = yEnd-ys;
int dT = 2*(dy+dx);
int dS = 2*dx;
int d = -(2*dy+dx);

setPixel(x,y);
while(y<yEnd)

{
y++;
if(d<0)
{
d = d-dS;
}
else
{
x--;
d = d-dT;
}
setPixel(x,y);
}
setPixel(xEnd,yEnd);
}
//------------------------------------------------------------------------void drawMyLine(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
//glPointSize(4.0);
//int xs = 100;
//int ys = 150;
//int xEnd = 200;
//int yEnd = 200;

float m = (yEnd-ys)/(xEnd-xs);
//bresenham1();
cout<<m<<endl;
if(0<m && m<1) bresenham1();
else if(0>m && m>-1) bresenham2();

else if(1<m) bresenham3();


else if(-1>m) bresenham4();
}
int main(int argc, char**argv)
{

//initialize GLUT
glutInit(&argc,argv);
//initialize display mode
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//set display-window width & height
glutInitWindowSize(600,480);
//set display-window upper-left position
glutInitWindowPosition(0,0);
//create display-window with a title
glutCreateWindow("Bresenham Line Drawing");
//initialze OpenGL
init();
//call graphics to be displayed on the window
glutDisplayFunc(drawMyLine);
//display everything and wait
glutMainLoop();
}

You might also like