Mid Point Circle Algorithm
Mid Point Circle Algorithm
Mid Point Circle Algorithm
Aim:-
Write a program to implement Mid Point Circle Algorithm.
Theory:-
This algorithm draws all eight octants simultaneously, starting from each cardinal
direction (0,90,180,270) and extends both ways to reach the nearest multiple of
45 (45, 135, 225, 315). It can determine where to stop because when y=x , it has
reached 45. The reasons for using these angles is shown in the above picture: As y
increases, it does not skip nor repeat any value y until reaching 45. So during the
while loop, y increments by 1 each iteration, and x decrements by 1 on occasion,
never exceeding 1 in one iteration. This changes at 45 because that is the point
where the tangent is rise=run. Whereas rise>run before and rise<run after.
Algorithm:-
1. Input radius(r) and point(xc,yc)
2. Make pixel function
void pixel(int xc, int yc, int x, int y){
putpixel(xc+x,yc+y,7);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,7);
putpixel(xc-x,yc+y,7);
putpixel(xc-x,yc-y,7);
putpixel(xc-y,yc-x,7);
putpixel(xc+y,yc-x,7);
putpixel(xc+x,yc-y,7);}
Flowchart:-
Code:-
#include<stdio.h>
#include<graphics.h>
#include <conio.h>
while (x >= y)
{
putpixel(x0+x, y0+y, WHITE);
putpixel(x0+y, y0+x, WHITE);
putpixel(x0-y, y0+x, WHITE);
putpixel(x0-x, y0+y, WHITE);
putpixel(x0-x, y0-y, WHITE);
putpixel(x0-y, y0-x, WHITE);
putpixel(x0+y, y0-x, WHITE);
putpixel(x0+x, y0-y, WHITE);
if (e <= 0)
{
y += 1;
e += 2*y + 1;
}
if (e > 0)
{
x -= 1;
e -= 2*x + 1;
}
}
}
int main()
{
int gd=DETECT, gm, x, y, r;
initgraph(&gd, &gm, "c:\\TURBO3\\BGI");
return 0;
}
Output:-
Discussion:-
If radius r is specified as an integer since all increments are integers, then we
Simply round d0 to (1-r) i.e.,
d0= 1 – r
The error of being % less than the precise value does not prevent d from getting
the appropriate sign. It does not even affect the rest of the scan-conversion
process either because the decision variable is updated with integer increments
only in the subsequent steps.
As in Bresenham’s line algorithm, the midpoint calculates pixel positions along the
circumference of a circle using integer addition and subtractions, assuming that
the circle parameters are specified in integer screen coordinate.