0% found this document useful (0 votes)
427 views

Lab Report 4: Title: Theory

The document describes using the mid-point circle drawing algorithm to draw a circle in C programming. It explains the theory behind the algorithm, the steps to determine the next pixel coordinate, and provides the full code to implement the algorithm. The code takes the circle center coordinates and radius as input, calculates the initial pixel using the boundary condition formula, and then uses a while loop to iteratively determine the next pixel and draw the circle by plotting pixels until the radius is reached. The output is two figures showing the user input and the drawn circle.

Uploaded by

Dewanand Giri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
427 views

Lab Report 4: Title: Theory

The document describes using the mid-point circle drawing algorithm to draw a circle in C programming. It explains the theory behind the algorithm, the steps to determine the next pixel coordinate, and provides the full code to implement the algorithm. The code takes the circle center coordinates and radius as input, calculates the initial pixel using the boundary condition formula, and then uses a while loop to iteratively determine the next pixel and draw the circle by plotting pixels until the radius is reached. The output is two figures showing the user input and the drawn circle.

Uploaded by

Dewanand Giri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab Report 4

Title: To use mid-point circle drawing algorithm to draw a circle.


Theory:
The mid-point circle drawing algorithm is an algorithm used to determine the points needed for
rasterizing a circle.
We use the mid-point algorithm to calculate all the perimeter points of the circle in the first
octant and then print them along with their mirror points in the other octants. This will work
because a circle is symmetric about it’s centre.
The algorithm is very similar to the Mid-Point Line Generation Algorithm. Here, only the
boundary condition is different.
For any given pixel (x, y), the next pixel to be plotted is either (x, y+1) or (x-1, y+1). This can be
decided by following the steps below.
1. Find the mid-point p of the two possible pixels i.e (x-0.5, y+1)
2. If p lies inside or on the circle perimeter, we plot the pixel (x, y+1), otherwise if it’s
outside we plot the pixel (x-1, y+1)
Boundary Condition : Whether the mid-point lies inside or outside the circle can be decided by
using the formula given below.
Given a circle centered at (0,0) and radius r and a point p(x,y):
F(p) = x2 + y2 – r2
if F(p)<0, the point is inside the circle
F(p)=0, the point is on the perimeter
F(p)>0, the point is outside the circle

We denote F(p) with P. The value of P is calculated at the mid-point of the two contending
pixels i.e. (x-0.5, y+1). Each pixel is described with a subscript k.

Pk = (Xk — 0.5)2 + (yk + 1)2 – r2


Now,
xk+1 = xk or xk-1 , yk+1= yk +1
∴ Pk+1 = (xk+1 – 0.5)2 + (yk+1 +1)2 – r2
= (xk+1 – 0.5)2 + [(yk +1) + 1]2 – r2
= (xk+1 – 0.5)2 + (yk +1)2 + 2(yk + 1) + 1 – r2
= (xk+1 – 0.5)2 + [ – (xk – 0.5)2 +(xk – 0.5)2 ] + (yk + 1)2 – r2 + (yk + 1) + 1
= Pk + (xk+1 – 0.5)2 – (xk – 0.5)2 + 2(yk + 1) + 1
= Pk + (x2k+1 – x2k)2 + (xk+1 – xk)2 + 2(yk + 1) + 1
= Pk + 2(yk +1) + 1, when Pk <=0 i.e the midpoint is inside the circle (xk+1 = xk)
= Pk + 2(yk +1) – 2(xk – 1) + 1, when Pk>0 i.e the mid point is outside the
circle (xk+1 = xk-1)

The first point to be plotted is (r, 0) on the x-axis. The initial value of P is calculated as
follows:

P1 = (r – 0.5)2 + (0+1)2 – r2
= 1.25 – r
= 1 -r (When rounded off).

The code:

The code for the asked program is given below:


// Including PreProcessor directives
#include <stdio.h>
#include <graphics.h>

// Main Function
int main()
{
// Declaring the variables
float x1,y1,radius,p,x,y;

// Asking for the inputs from the user


printf("Enter the co-ordinates of center of the circle\n");
scanf("%f %f",&x1,&y1);

// Enter the radius of the circle


printf("Enter the radius of the center of the circle\n");
scanf("%f",&radius);

// Calculating the initial point and p0


x=0;
y=radius;
p=1-radius;

// Initialize the graphics


int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
putpixel(x+x1,y+y1,7);

// By Mid-point of the circle algorithm


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

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

// Concluding the program


delay(5000000);
closegraph();
return 0;
}
The output:
The output of the asked program is given below:

Figure 1: Input mid-point and radius of circle

Figure 2: Circle drawn using the mid-point circle algorithm


Conclusion:
Thus, as shown in the program above, we can draw a circle by drawing individual pixels with the
mid-point circle drawing algorithm using the various functions available in the graphics.h header
file.

You might also like