Midpoint Circle algorithm not working for unequal center coordinates

151 views Asked by At

My midpoint circle algorithm C Program is given below:

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

void cir(int,int,float);
void main()
{
    int gd=DETECT;
    int gm=DETECT;
    int x1,y1;
    float r;
    initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
    printf("\nenter the cordinates of center: ");
    scanf("%d",&x1);
    scanf("%d",&y1);
    printf("\nenter the radius : ");
    scanf("%f",&r);
    cir(x1,y1,r);
    getch();
    closegraph();
}

void cir(int x1,int y1,float r)
{
    int x,y;
    float d;
    x=0;
    y=r;
    d=1.25-r;
    putpixel(x1+x,y1+y,RED);
    putpixel(y1+y,x1+x,RED);
    putpixel(y1+y,x1-x,RED);
    putpixel(x1+x,y1-y,RED);
    putpixel(x1-x,y1-y,RED);
    putpixel(y1-y,x1-x,RED);
    putpixel(y1-y,x1+x,RED);
    putpixel(x1-x,y1+y,RED);
    while(x<y){

        if(d<0){
            x=x+1;
            d=d+2*x+1;
        }else{
            x=x+1;
            y=y-1;
            d=d+2*(x-y)+1;
        }
        putpixel(x1+x,y1+y,RED);
        putpixel(y1+y,x1+x,RED);
        putpixel(y1+y,x1-x,RED);
        putpixel(x1+x,y1-y,RED);
        putpixel(x1-x,y1-y,RED);
        putpixel(y1-y,x1-x,RED);
        putpixel(y1-y,x1+x,RED);
        putpixel(x1-x,y1+y,RED);
    }
}

When I provide center coordinates as (200,200) the output is a perfect circle but when I provide any other coordinate where (Y<X) example (200,140) the output is not a perfect circle. Please suggest if any changes is needed in my code.

cir function implements midpoint circle algorithm. Does midpoint algorithm only works for equal center coordinates

1

There are 1 answers

0
I.Omar On

The result from that is inconsistent as y won't start correctly for any float and d aswell.

y=r;
d=1.25-r;

Why are you using float type in integer based algorithm and with pixel as the unit?

change

d=1.25-r;

to

d=1-r;

and all float type to int type.

as the incrementation is by 1/-1