You are on page 1of 3

Lab 3 - WAP to draw a circle using Mid-point circle drawing Algorithm

Theory:

The midpoint circle drawing algorithm is a graphics algorithm used to draw a circle. It works by
calculating points of the circle using symmetry properties of the circle and incrementally
updating a decision parameter to determine the next points. It's efficient because it uses integer
arithmetic and avoids using costly floating-point operations.

Algorithm:

1. Initialize the center (x_center, y_center) and the radius r of the circle.

2. Set the initial decision parameter P to (5/4) - r.

3. Start from the point (0, r).

4. At each step, consider the next point candidates: (x + 1, y) and (x + 1, y - 1).

5. Based on the decision parameter P, select the next point to plot.

6. Update P based on the chosen point.

7. Repeat steps 4-6 until x becomes greater than or equal to y

Source Code

#include <stdio.h>

#include <graphics.h>

void drawCircle(int x_center, int y_center, int r) {

int x = 0, y = r;

int P = 1 - r;

// Plot the initial point

putpixel(x_center + x, y_center + y, WHITE);

// Midpoint circle drawing algorithm

while (x < y) {

if (P < 0)

P += 2 * x + 3;

else {

P += 2 * (x - y) + 5;

y--;
}

x++;

// Plot the points using symmetry

putpixel(x_center + x, y_center + y, WHITE);

putpixel(x_center - x, y_center + y, WHITE);

putpixel(x_center + x, y_center - y, WHITE);

putpixel(x_center - x, y_center - y, WHITE);

putpixel(x_center + y, y_center + x, WHITE);

putpixel(x_center - y, y_center + x, WHITE);

putpixel(x_center + y, y_center - x, WHITE);

putpixel(x_center - y, y_center - x, WHITE);

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, "");

int x_center = 100;

int y_center = 100;

int radius = 50;

drawCircle(x_center, y_center, radius);

getch();

closegraph();

return 0;

Output

You might also like