Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AakashCode12/a1ce4f24aa184080c86752c98cb55587 to your computer and use it in GitHub Desktop.
Save AakashCode12/a1ce4f24aa184080c86752c98cb55587 to your computer and use it in GitHub Desktop.
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <dos.h>
#include <graphics.h>
void bresenhamsthick (int x1,int y1, int x2, int y2);
void bresenhamsthick (int x1,int y1, int x2, int y2)
{
int dx,dy,p0,pk;
dx=(x2-x1);
dy=(y2-y1);
p0 =2*dy-dx;
int x,y;
x=x1;
y=y1;
pk=p0;
putpixel(x,y,WHITE);
while(x<=x2){
if(pk<0){
x=x+1;
y=y;
putpixel(x,y,WHITE);
pk=pk+2*dy;
}
else{
x=x+1;
y=y+1;
putpixel(x,y,WHITE);
pk=pk+(2*(dy-dx));
}
}
}
void bresenhamsdotted (int x1,int y1, int x2, int y2);
void bresenhamsdotted (int x1,int y1, int x2, int y2)
{
int dx,dy,p0,pk;
dx=(x2-x1);
dy=(y2-y1);
p0=2*dy-dx;
int x,y;
x=x1;
y=y1;
pk=p0;
putpixel(x,y,WHITE);
int i=0;
while(x<=x2){
if(pk<0){
x=x+1;
y=y;
if(i%2!=0)
{
putpixel(x,y,WHITE);
}
pk=pk+2*dy;
}
else{
x=x+1;
y=y+1;
if(i%2!=0)
{
putpixel(x,y,WHITE);
}
pk=pk+(2*(dy-dx));
}
i++;
}
}
void bresenhamsdashed (int x1,int y1, int x2, int y2);
void bresenhamsdashed (int x1,int y1, int x2, int y2)
{
int dx,dy,p0,pk;
dx=(x2-x1);
dy=(y2-y1);
p0 =2*dy-dx;
int x,y;
x=x1;
y=y1;
pk=p0;
putpixel(x,y,WHITE);
int i=0;
while(x<=x2){
if(pk<0){
x=x+1;
y=y;
if(i%8!=0)
{
putpixel(x,y,WHITE);
}
pk=pk+2*dy;
}
else{
x=x+1;
y=y+1;
if(i%8!=0)
{
putpixel(x,y,WHITE);
}
pk=pk+(2*(dy-dx));
}
i++;
}
}
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,option;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
cleardevice();
cout<< "Bresenham's Line Generation Algorithm";
cout<<"\n Enter the starting co-ordinates for drawing a line";
cin>>x1>>y1;
cout<<"\n Enter the ending co-ordinates";
cin>>x2>>y2;
cout<<"\n1) Thick\n2) Dotted \n3) Dashed\nEnter your options: \n";
cin>>option;
//switch case
switch(option){
case 1: bresenhamsthick(x1,y1,x2,y2);
break;
case 2:bresenhamsdotted(x1,y1,x2,y2);
break;
case 3:bresenhamsdashed(x1,y1,x2,y2);
break;
default: cout<<"\n Invalid option";
break;
}
getch();
closegraph();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment