## Friday, June 14, 2019

Midpoint circle algorithm

IT is based on the following function for testing the relationship between and arbitrary point (x,y) and a circle of radius are centered at the origin so, It we have a function of x,y then
f(x,y) = x^2  + y^2-r^2 is equal to
if < 0 for (x,y) inside the circle
if = 0 for (x,y) on the circle
if > 0 for (x,y) outside the circle

Now consider the coordinates of the points half way between the pixel inner and outer as (xi+1,yi -1/2) this is called the mid point and we use it to define a decision parameter

Pi=function of (xi+1,yi-1/2)=((xi+1)^2+( yi-1/2)^2-r^2)

If Pi is -ve the midpoint is inside the circle on the other hand it Pi is the +ve the midpoint is outside the circle.
With the help of decision promoter from Pi+n for n=0 to till n point.

Program: -

#include<stdio.h>
#include<conio.h>
void pixel(int xr, int yr, int x, int y);
int main()
{
int gd, gm, xr, yr, r, x, y, p;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,”c://tc//bgi”);
scanf(“%d”,&r);
x=0;
y=r;
P=1-r;
pixel(xr, yr, x, y);
while(x<y)
{
If(p<0)
{
x++;
P=P+2*x+1;
}
else
{
X++;
y--;
P=P+2*(x-y)+1;
}
rixel(xr, yr, x, y);
}
return 0;
}
void pixel(int xr, int yr, int x, int y)
{
putpixel(xr+x, yr+y, white);
putpixel(xr+x, yr-y, white);
putpixel(xr-x, yr+y, white);
putpixel(xr-x, yr-y, white);
putpixel(xr+x, yr+y, white);
putpixel(xr+x, yr-y, white);
putpixel(xr-x, yr+y, white);
putpixel(xr-x, yr-y, white);
}