Thick Line Program Using Bresenham Algorithm In C Graphics
Thick Line Program
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void bresenham(float xa, float ya, float xb, float yb, int thickness);
void main()
{
int gdriver=DETECT, gmode;
int thickness;
initgraph(&gdriver, &gmode, "C:\\Turboc3\\bgi");
printf("\nEnter the thickness of line: ");
scanf("%d", &thickness);
if(thickness<=1)
printf("Invalid Size...");
else
{
bresenham(300, 0, 300, 100, thickness);
bresenham(300, 0, 350, 40, thickness);
bresenham(350, 50, 300, 70, thickness);
bresenham(300, 100, 200, 200, thickness);
bresenham(300, 100, 400, 200, thickness);
bresenham(200, 200, 200, 350, thickness);
bresenham(200, 200, 400, 200, thickness);
bresenham(400, 200, 400, 350, thickness);
bresenham(200, 350, 400, 350, thickness);
bresenham(225, 225, 225, 260, thickness);
bresenham(225, 225, 260, 225, thickness);
bresenham(225, 260, 260, 260, thickness);
bresenham(260, 225, 260, 260, thickness);
bresenham(340, 225, 375, 225, thickness);
bresenham(340, 225, 340, 260, thickness);
bresenham(340, 260, 375, 260, thickness);
bresenham(375, 225, 375, 260, thickness);
bresenham(275, 285, 275, 350, thickness);
bresenham(275, 285, 315, 285, thickness);
bresenham(315, 285, 315, 350, thickness);
}
getch();
}
void bresenham(float xa, float ya, float xb, float yb, int thickness)
{
int dx, dy, p, xend, yend, twody, twodydx, x, y, twodx, twodxdy, wy, wx=0, i;
float m, slop, po1=0, po2=0, sq=0;
dx=abs(xa-xb);
dy=abs(ya-yb);
m=(float)(yb-ya)/(xb-xa);
po1=pow((xb-xa),2);
po2=pow((yb-ya),2);
sq=sqrt(po1+po2);
if(m<1)
wy=((thickness-1)*sq)/(2*fabs(xb-xa));
else
wx=((thickness-1)*sq)/(2*fabs(yb-ya));
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;
}
while(x<xend)
{
if(wx==0)
for(i=y+wy;i>y-wy;i--)
putpixel(x, i, 2);
else
for(i=x+wx;i>x-wx;i--)
putpixel(i, y, 2);
x++;
if(p<0)
{
p=p+twody;
}
else
{
if(m>0 && m<1)
{
y++;
}
else
{
y--;
p=p+twodydx;
}
}
}
}
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;
}
while(y<yend)
{
if(wx==0)
for(i=y+wy;i>y-wy;i--)
putpixel(x, i, 2);
else
for(i=x+wx;i>x-wx;i--)
putpixel(i, y, 2);
y++;
if(p<0)
{
p=p+twodx;
}
else
{
if(m>=1)
{
x++;
}
else
{
x--;
p=p+twodxdy;
}
}
}
}
}
Linestyle program using DDA algorithm
Midpoint circle drawing algorithm
Comment your views on this Article :)
No comments
Comment your views on this article