Search Here

C program to draw Circle using Bresenham algorithm



/* C program for Bresenham 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,p;
    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;
    //initgraph(&gd, &gm, "C:\\TC\\BGI"); /* For Windows Turbo-C users */
    initgraph(&gd,&gm,NULL); /* For Linux users */
    setbkcolor(WHITE);
    
    circlepoints(x,y);
    p=3-2*r;
    while(y>x)
    {
        if(p<0)
            p=p+4*x+6;
        else
        {
            y=y-1;
            p=p+4*(x-y)+10;
        }
        x=x+1;
        circlepoints(x,y);
    }
    delay(5000);
    closegraph();
    
    system("clear");
    return 0;
}


C program to draw a circle using Bresenham algorithm
Output

No comments:

Post a Comment