Draw All Types And Styles of Lines Using Bresenham Algorithm
Draw all types and styles of lines
#include<conio.h>
#include<graphics.h>
#include<math.h>
int gdriver=DETECT,gmode;
int dotted_dash[8]={1, 1, 0, 1, 0, 1, 1, 0};
int dotted[8]={1, 0, 1, 0, 1, 0, 1, 0};
int dash[8]={1, 1, 0, 0, 1, 1, 0, 0};
int solid[8]={1, 1, 1, 1, 1, 1, 1, 1};
int value;
void bresenham(int xa, int ya, int xb, int yb, int a[]);
void main()
{
do{
clrscr();
initgraph(&gdriver, &gmode, "C:\\Turboc3\\BGI");
printf("\n************************************\n");
printf("\nPress 1 for Solid Line");
printf("\nPress 2 for Dotted Line");
printf("\nPress 3 for Dash Line");
printf("\nPress 4 for Dotted Dash Line");
printf("\nPress 5 for Exit from the program\n");
printf("\n************************************\n");
printf("\nEnter your Choice : ");
scanf("%d",&value);
switch(value){
case 1: bresenham(200, 200, 350, 200, solid);
bresenham(200, 200, 200, 300, solid);
bresenham(200, 200, 350, 300, solid);
bresenham(350, 200, 200, 300, solid);
break;
case 2: bresenham(200, 200, 350, 200, dotted);
bresenham(200, 200, 200, 300, dotted);
bresenham(200, 200, 350, 300, dotted);
bresenham(350, 200, 200, 300, dotted);
break;
case 3: bresenham(200, 200, 350, 200, dash);
bresenham(200, 200, 200, 300, dash);
bresenham(200, 200, 350, 300, dash);
bresenham(350, 200, 200, 300, dash);
break;
case 4: bresenham(200, 200, 350, 200, dotted_dash);
bresenham(200, 200, 200, 300, dotted_dash);
bresenham(200, 200, 350, 300, dotted_dash);
bresenham(350, 200, 200, 300, dotted_dash);
break;
case 5: printf("Exit");
break;
default :printf("Kindly enter valid choice: ");
}
getch();
}while(value!=5);
}
void bresenham(int xa, int ya, int xb, int yb, int a[]){
int p, x, y, dx, dy, twodydx, twody, twodxdy, twodx, xend, yend;
float m;
int i=0;
dx=abs(xa-xb);
dy=abs(ya-yb);
m=(float)(yb-ya)/(xb-xa);
if(dx>dy)
{
p=2*dy-dx;
twody=2*dy;
twodydx=2*(dy-dx);
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x,y,3);
while(x<xend)
{
x++;
if(p<0)
p=p+twody;
else
{
if(0<m && m<1)
y++;
else
y--;
p=p+twodydx;
}
if(a[i]==1)
putpixel(x,y,3);
i++;
if(i==7)
i=0;
}
}
else
{
p=2*dx-dy;
twodx=2*dx;
twodxdy=2*(dx-dy);
if(ya>yb)
{
x=xb;
y=yb;
yend=ya;
}
else
{
x=xa;
y=ya;
yend=yb;
}
putpixel(x,y,3);
while(y<yend)
{
y++;
if(p<0)
p=p+twodx;
else
{
if(m>=1)
x++;
else
x--;
p=p+twodxdy;
}
if(a[i]==1)
putpixel(x,y,3);
i++;
if(i==7)
i=0;
}
}
}
Draw a house in computer graphics
Know more about Mobile App Development Process
Comment your views on this Article :)
No comments
Comment your views on this article