Friday, 6 September 2013

Program for Line Drawing using Bresenham’s algorithm

#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd,gm,i;
float x,y,x1,y1,x2,y2,dx,dy,length,e;

printf("enter the value of x1:\n");
scanf("%f",&x1);
printf("enter the value of y1:\n");
scanf("%f",&y1);
printf("enter the value of x2:\n");
scanf("%f",&x2);
printf("enter the value of y2:\n");
scanf("%f",&y2);

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");

dx=x2-x1;
dy=y2-y1;

x=x1;
y=y1;
e=2*dy-dx;

       for(i=0;i<dx;i++)
       {
putpixel(x,y,RED);

while(e>=0)
{
y++;
e=e-2*dx;
}
x++;
e=e+2*dy;
}
getch();
closegraph();

}

Program for Circle Generation using midpoint algorithm

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int r,x,y,xc,yc;
float d;
clrscr();
initgraph(&gd,&gm,"..\\bgi");
printf("Enter Radius\n");
scanf("%d",&r);
printf("Enter Center of circle\n");

scanf("%d",&xc);
scanf("%d",&yc);
d=1.25-r;
x=0;
y=r;
do
{
if(d<0)
{

x=x+1;
d=d+2*x+1;
}
else
{

x=x+1;
y=y-1;
d=d+2*x-2*y+1;
}
putpixel(xc+x,yc+y,5);
putpixel(xc-y,yc-x,5);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc+x,5);
putpixel(xc+y,yc+x,5);
putpixel(xc-x,yc-y,5);
putpixel(xc+x,yc-y,5);
putpixel(xc-x,yc+y,5);
}
while(x<y);
getch();
closegraph();
}