Search Here

C program to draw Circle using Midpoint algorithm



/* C profram for Midpoint Circle Drawing */

#include<stdio.h>
#include<graphics.h>
int xc,yc;

void circlepoints(int x, int y)
{
    putpixel(xc+x,yc-y,BLACK);
    putpixel(xc-x,yc-y,BLACK);
    putpixel(xc+x,yc+y,BLACK);
    putpixel(xc-x,yc+y,BLACK);
    putpixel(xc+y,yc-x,BLACK);
    putpixel(xc-y,yc-x,BLACK);
    putpixel(xc+y,yc+x,BLACK);
    putpixel(xc-y,yc+x,BLACK);
}

int main()
{
    int gd = DETECT,gm;
    int r,x,y,d;
    
    printf("Enter the center of the circle:");
    scanf("%d %d",&xc,&yc);
    printf("Enter the radius of the circle:");
    scanf("%d",&r);
    
    x=0; y=r; d=1-r;
    //initgraph(&gd, &gm, "C:\\TC\\BGI"); /* For Windows Turbo-C users */
    initgraph(&gd,&gm,NULL); /* For Linux users */
    setbkcolor(WHITE);
    
    circlepoints(x,y);
    while(y>x)
    {
        x++;
        if(d<0)
            d=d+2*x+3;
        else
        {
            y--;
            d=d+2*x-2*y+5;
        }
        circlepoints(x,y);
    }
    delay(5000);
    closegraph();
   
    system("clear");
    return 0;
}


C program to draw a circle using Midpoint algorithm
Output

No comments:

Post a Comment