Bresenham Line Drawing Algorithm
To draw a line, you need two points between which you can draw a line The Bresenham algorithm is another incremental scan conversion algorithm which is calculate lines coordinates between two points.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.
Code
#include<iostream.h> #include<conio.h> #include<math.h> #include<graphics.h> void get_driver() { clrscr(); //clear screen int gDriver=DETECT,gMode; initgraph(&gDriver,&gMode,"C:\\TC\\BGI"); } void main() { get_driver(); //initialize graphics int dx, dy, p, end; float x1, x2, y1, y2, x, y; x1=x2=y1=y2=x=y=p=end=dx=dy=0; cout<<"Enter the x1 value : "; cin>>x1; cout<<"Enter the y1 value : "; cin>>y1; cout<<"Enter the x2 value : "; cin>>x2; cout<<"Enter the y2 value : "; cin>>y2; cleardevice(); //clear Screen for graphics setbkcolor(15);//set background white dx = abs(x1 - x2); dy = abs(y1 - y2); p = 2 * dy - dx; if(x1 > x2) { x = x2; y = y2; end = x1; } else { x = x1; y = y1; end = x2; } putpixel(x, y, RED); //put first pixel while(x < end) { x = x + 1; if(p < 0) { p = p + 2 * dy; } else { y = y + 1; p = p + 2 * (dy - dx); } putpixel(x,y,RED); //put pixel in loop } getch(); }
OUTPUT
Bresenham Line Drawing Calculator
(Visited 5,378 times, 1 visits today)
Written by:
Great post, I think blog owners should larn a lot from this weblog its very user friendly.