You are on page 1of 5

EXPERIMENT- 3

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);}

3. Set x=0 and y=r. Calculate pk as


Pk=1-r
Pixel(xc,yc,x,y)
4. While(x<y){
If(Pk<0){
a
Pk=Pk+(2*x)+1;
}else{
x=x+1;
y=y-1;
Pk=Pk+(2*x)-(2*y)+1; }
Pixel(xc,yc,x,y);

Flowchart:-
Code:-
#include<stdio.h>
#include<graphics.h>
#include <conio.h>

void drawcircle(int x0, int y0, int radius)


{
int x = radius;
int y = 0;
int e = 0;

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");

printf("Enter radius of circle: ");


scanf("%d", &r);

printf("Enter co-ordinates of center(x and y): ");


scanf("%d%d", &x, &y);
midPointGenerationCircle(x, y, r);

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.

Findings and Learning:-


We have successfully implemented the Midpoint circle algorithm by writing a C++
program. Through the course of the implementation, we observed and learnt
various props and cons of using the Midpoint circle algorithm. They are as follows:

• Advantages of Midpoint Circle Algorithm


1. The midpoint method is used for deriving efficient scan-conversion
algorithms to draw geometric curves on raster displays.
2. The method is general and is used to transform the nonparametric
equation f(x,y) = 0 which describes the curve, into an algorithm that
draws the curve.
• Disadvantages of Midpoint Circle Algorithm
1. This method is very time consuming.
2. The distance between the pixels is not equal so we won’t get smooth
circle.

You might also like