Program Code for Circle drawing

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void disp(float,int,int);
void disp1(float,int,int);
void main()
{
int gd=DETECT,gm,h,k,y,fl=1,m;
float r;

initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter the radius " );
scanf("%f",&r);
printf("Enter the center (h,k) :");
scanf("%d %d",&h,&k);

m=r;
while(1)
{
 if(kbhit())
 break;


if(r<=0)
fl=0;
if(r>=m)
fl=1;
if(fl==1)
{
r--;
disp(r,h,k);
}
else
{
r++;
disp1(r,h,k);
}
sleep(1);

}
}

void disp(float r,int h,int k)
{
int x,xe,y;
x=0;
xe=r/1.414;
while(x<xe)
{
y=sqrt(pow(r,2)-pow(x,2));
putpixel(x+h,y+k,1);
putpixel(-x+h,y+k,2);
putpixel(x+h,-y+k,3);
putpixel(-x+h,-y+k,4);
putpixel(y+h,x+k,1);
putpixel(-y+h,x+k,2);
putpixel(y+h,-x+k,3);
putpixel(-y+h,-x+k,4);
x++;
}
}


void disp1(float r,int h,int k)
{
int x,xe,y;
x=0;
xe=r/1.414;
while(x<xe)
{
y=sqrt(pow(r,2)-pow(x,2));
putpixel(x+h,y+k,BLACK);
putpixel(-x+h,y+k,BLACK);
putpixel(x+h,-y+k,BLACK);
putpixel(-x+h,-y+k,BLACK);
putpixel(y+h,x+k,BLACK);
putpixel(-y+h,x+k,BLACK);
putpixel(y+h,-x+k,BLACK);
putpixel(-y+h,-x+k,BLACK);
x++;
}
}

I/O:

Enter the radius : 90
Enter the center (h,k): 80 90


sit back and relax and enjoy the output.

Comments

Popular posts from this blog

Pascal Triangle using 2D array

JAVA program to add two distance