Mid-point Circle Algorithm

#include<graphics.h>
#include<stdio.h>
#include<conio.h>

void circle_plot(int xc,int yc,int x,int y);

void main()
{
    int gd=DETECT,gm;
    int x,y,r,xc,yc,pk;
    clrscr();
    initgraph(&gd,&gm,"C:\\TC\\BGI");
    printf("\nEnter the center point of circle:");
    scanf("%d%d",&xc,&yc);
    printf("\nEnter the radious of circle:");
    scanf("%d",&r);
    x=0;
    y=r;
    pk=1-r;
    circle_plot(xc,yc,x,y);
    while(x<=y)
    {
        if(pk<0)
        {
            x=x+1;
            pk=pk+(2*x)+1;
        }
        else
        {
            x=x+1;
            y=y-1;
            pk=pk+(2*x)-(2*y)+1;
        }
        circle_plot(xc,yc,x,y);
    }
    getch();
    closegraph();
}

void circle_plot(int xc,int yc,int x,int y)
{
    putpixel(xc+x,yc+y,3);
    putpixel(xc+x,yc-y,3);
    putpixel(xc-x,yc+y,3);
    putpixel(xc-x,yc-y,3);
    putpixel(xc+y,yc+x,3);
    putpixel(xc+y,yc-x,3);
    putpixel(xc-y,yc+x,3);
    putpixel(xc-y,yc-x,3);
}

Outputmid.png

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s