Write a c program to implement mid-point circle drawing algorithm.
The Mid-Point Circle Drawing Algorithm is an efficient algorithm used to draw a circle in computer graphics. It is based on the concept of decision parameters to determine the next pixel to plot. Below is a C program that implements the Mid-Point Circle Drawing Algorithm:
#include <stdio.h>
#include <graphics.h>
// Function to implement Mid-Point Circle Drawing Algorithm
void drawCircle(int xc, int yc, int radius) {
int x = 0;
int y = radius;
int p = 1 - radius; // Initial decision parameter
// Plot the initial points
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
// Loop to plot the circle
while (x < y) {
x++;
if (p < 0) {
p = p + 2 * x + 1;
} else {
y--;
p = p + 2 * (x - y) + 1;
}
// Plot the symmetric points
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
}
}
int main() {
int gd = DETECT, gm;
int xc, yc, radius;
// Initialize graphics mode
initgraph(&gd, &gm, NULL);
// Input center and radius of the circle
printf("Enter the center coordinates of the circle (xc yc): ");
scanf("%d %d", &xc, &yc);
printf("Enter the radius of the circle: ");
scanf("%d", &radius);
// Draw the circle using Mid-Point Circle Drawing Algorithm
drawCircle(xc, yc, radius);
// Wait for a key press
getch();
// Close the graphics mode
closegraph();
return 0;
}