C++ program for Bresenham's line drawing algorithm
kw.cpp
#include<graphics.h>
#include<iostream>usingnamespacestd;classBresenham{private:intx,y,x1,y1,x2,y2;public:voidgetdata();voidcalc();};voidBresenham::getdata(){cout<<"Enter the x1 coordinate ";cin>>x1;cout<<"Enter the y1 coordinate ";cin>>y1;cout<<"Enter the x2 coordinate ";cin>>x2;cout<<"Enter the y2 coordinate ";cin>>y2;}voidBresenham::calc(){intdx,dy,p;dx=abs(x2-x1);dy=abs(y2-y1);p=2*dy-dx;if(x1>x2){x=x2;y=y2;x2=x1;}else{x=x1;y=y1;}putpixel(x,y,15);while(x<x2){x++;if(p<0){p+=2*dy;}else{y++;p=p+(2*dy)-(2*dx);}delay(10);putpixel(x,y,15);}}intmain(){intgd=DETECT,gm=9;Bresenhamb;b.getdata();initgraph(&gd,&gm,NULL);b.calc();delay(7000);return0;}
Output
kodingwindow@kw:~$ g++ kw.cpp -lgraph kodingwindow@kw:~$ ./a.out
Enter the x1 coordinate 20
Enter the y1 coordinate 20
Enter the x2 coordinate 150
Enter the y2 coordinate 150
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.