Bresenham Line Algorithm Using OpenGL and C++.

// bresenham_line_algorithm.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "GL/glut.h";

float x1,x2,y1,y2,b=0;

void setPixel(int x, int y)

{

glBegin(GL_POINTS);

glVertex2i(x,y);

glEnd();

glFlush();

}

void horizontal()

{

if(x1>x2)

{

float temp;

temp = x1;

x1 = x2;

x2 = temp;

}

for(float x=x1; x<=x2; x++)

{

setPixel(x,y1);

}

}

void vertical()

{

if(y1>y2)

{

float temp;

temp = y1;

y1 = y2;

y2 = temp;

}

for(float y=y1; y<=y2; y++)

{

setPixel(x1,y);

}

}

void bresenham1()

{

if(x1>x2)

{

float temp;

temp = x1;

x1 = x2;

x2 = temp;

temp = y1;

y1 = y2;

y2 = temp;

}

int x = x1, y = y1;

int dx = x2-x1;

int dy = y2-y1;

int dT = 2*(dy-dx);

int dS = 2*dy;

int d = 2*dy-dx;

setPixel(x,y);

while(x<x2)

{

x++;

if(d<0)

{

d = d+dS;

}

else

{

d = d+dT;

y++;

}

setPixel(x,y);

}

setPixel(x2,y2);

}

void bresenham2()

{

if(x1>x2)

{

float temp;

temp = x1;

x1 = x2;

x2 = temp;

temp = y1;

y1 = y2;

y2 = temp;

}

int x = x1, y = y1;

int dx = x2-x1;

int dy = y2-y1;

int dT = 2*(dy+dx);

int dS = 2*dy;

int d = -(2*dx+dy);

setPixel(x,y);

while(x<x2)

{

x++;

if(d<0)

{

d = d-dS;

}

else

{

y–;

d = d-dT;

}

setPixel(x,y);

}

setPixel(x2,y2);

}

void bresenham3()

{

if(y1>y2)

{

float temp;

temp = x1;

x1 = x2;

x2 = temp;

temp = y1;

y1 = y2;

y2 = temp;

}

int x = x1, y = y1;

int dx = x2-x1;

int dy = y2-y1;

int dT = 2*(dx-dy);

int dS = 2*dx;

int d = 2*dx-dy;

setPixel(x,y);

while(y<y2)

{

y++;

if(d<0)

{

d = d+dS;

}

else

{

x++;

d = d+dT;

}

setPixel(x,y);

}

setPixel(x2,y2);

}

void bresenham4()

{

if(y1>y2)

{

float temp;

temp = x1;

x1 = x2;

x2 = temp;

temp = y1;

y1 = y2;

y2 = temp;

}

int x = x1, y = y1;

int dx = x2-x1;

int dy = y2-y1;

int dT = 2*(dy+dx);

int dS = 2*dx;

int d = -(2*dy+dx);

setPixel(x,y);

while(y<y2)

{

y++;

if(d<0)

{

d = d-dS;

}

else

{

x–;

d = d-dT;

}

setPixel(x,y);

}

setPixel(x2,y2);

}

void myMouse(int button, int state, int x, int y)

{

if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)

{

if(b==0)

{

x1 = x;

y1 = 480-y;

b = 1;

}

else if(b==1)

{

x2 = x;

y2 = 480-y;

b = 0;

if(y1==y2) horizontal();

else if(x1==x2) vertical();

float m = (y2-y1)/(x2-x1);

if(0<m && m<1) bresenham1();

else if(0>m && m>-1) bresenham2();

else if(1<m) bresenham3();

else if(-1>m) bresenham4();

}

}

}

void myDisplay(void)

{

glClearColor(1.0,1.0,1.0,0.0);

glColor3f(1.0,0.0,0.0);

gluOrtho2D(0.0,640.0,0.0,480.0);

glClear(GL_COLOR_BUFFER_BIT);

glutMouseFunc(myMouse);

glFlush();

}

int _tmain(int argc, char** argv)

{

glutInitWindowSize(640,480);

glutInitWindowPosition(100, 100);

glutCreateWindow ("A simple Line");

glutDisplayFunc(myDisplay);

glutMainLoop ( );

return 0;

}

To get complete code and project contact with me through email , mobile. You may commented below as your requirement.

Al Hizbul Bahar hello, I’d like you to send me your project on rasterization of lines and circles. I am a student of computer science at the Federal University of Roraima and am currently studying computer graphics. your project can help me and take lots of doubts.

Write a program in C/C++ using OpenGL to implement Bresenhamâ€™s line drawing algorithm with all values of slopes

Did not you get any help from my article? You should try yourself and take some help from others or google but not complete code.

Hye there. Did you know where to get complete glut library?

Hello I want bresenham program with output whatever code I got from this site I am not getting correct output