C++ Program to implement Bresenham's line algorithm (Graphics)

Bresenham's line algorithm is an algorithm that determines the points of an n-dimensional raster that should be selected in order to form a close approximation to a straight line between two points. It is commonly used to draw lines on a computer screen, as it uses only integer addition, subtraction and bit shifting, all of which are very cheap operations in standard computer architectures. It is one of the earliest algorithms developed in the field of computer graphics. An extension to the original algorithm may be used for drawing circles.


Program

#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include<dos.h>
void bsline(int x,int y,int x2,int y2)
{
int dx,dy,p;
dx=x2-x;
dy=y2-y;
p = 2 * (dy) - (dx);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,RED);
delay(10);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\Tc\\BGI");
int x1,x2,y1,y2;
cout<<"Enter the x1,y1,x2,y2 values : ";
cin>>x1>>y1>>x2>>y2;
bsline(x1,y1,x2,y2);
getch();
closegraph();
}

8 comments:

  1. I don't need such a fucking program go to hell.

    ReplyDelete
  2. graphics.h is no longer in use
    it is not working visual studio

    ReplyDelete
  3. Wrong code Provided.....Your code can give output in only '\' this type of lines ..not for '/'...give the points (x1,y1) = (100, 400) and (x2,y2) = (350,150)...correct your code.

    ReplyDelete