Midpoint Circle Drawing Algorithm in C
Here you will learn and get the program code of Midpoint circle drawing algorithm in C and C++ using computer graphics.
What is midpoint circle algorithm
The midpoint circle algorithm is a technique for drawing a circle in computer graphics. It is used to calculate the position of the point to be printed, and for printing that pixel we uses the putpixel() function.
Mid point circle program in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <stdio.h> #include <conio.h> #include <graphics.h> #include <dos.h> int main() { int midx, midy, x, y, radius, dp; radius = 100; midx = 200; midy = 200; dp = 1 - radius; x = 0, y = radius; do { putpixel(midx + x, midy + y, WHITE); putpixel(midx - x, midy + y, WHITE); putpixel(midx + x, midy - y, WHITE); putpixel(midx - x, midy - y, WHITE); putpixel(midx + y, midy + x, WHITE); putpixel(midx - y, midy + x, WHITE); putpixel(midx + y, midy - x, WHITE); putpixel(midx - y, midy - x, WHITE); delay(100); x = x + 1; if (dp < 0) { dp = dp + 2 * x + 1; } else { y = y - 1; dp = dp + 2 * (x - y) + 1; } } while (x < y); getch(); return 0; } |
Program for Midpoint circle drawing algorithm in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include <stdio.h> #include <graphics.h> #include<math.h> void midPointCircleDraw(int x_centre, int y_centre, int r) { int x = r, y = 0, P; putpixel(x + x_centre, y + y_centre, 7); if (r > 0) { putpixel(x + x_centre, -y + y_centre, 7); putpixel(y + x_centre, x + y_centre, 7); putpixel(-y + x_centre, x + y_centre, 7); } P = 1 - r; while (x > y) { y++; if (P <= 0) P = P + 2*y + 1; else { x--; P = P + 2*y - 2*x + 1; } if (x < y) break; putpixel(x + x_centre, y + y_centre, 7); putpixel(-x + x_centre, y + y_centre, 7); putpixel(x + x_centre, -y + y_centre, 7); putpixel(-x + x_centre, -y + y_centre, 7); delay(50); if (x != y) { putpixel(y + x_centre, x + y_centre, 7); putpixel(-y + x_centre, x + y_centre, 7); putpixel(y + x_centre, -x + y_centre, 7); putpixel(-y + x_centre, -x + y_centre, 7); delay(50); } } } int main() { int gd = DETECT, gm; int x,y,radius; initgraph(&gd, &gm, "c:\turboc3\bgi"); printf("nEnter X and Y cordinates :"); scanf("%d%d",&x,&y); printf("nEnter radius :"); scanf("%d",&radius); midPointCircleDraw(x, y, radius); getchar(); closegraph(); return 0; } |
Program for Midpoint Circle Drawing Algorithm in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include <stdio.h> #include <graphics.h> #include<math.h> void midPointCircleDraw(int x_centre, int y_centre, int r) { int x = r, y = 0, P; putpixel(x + x_centre, y + y_centre, 7); if (r > 0) { putpixel(x + x_centre, -y + y_centre, 7); putpixel(y + x_centre, x + y_centre, 7); putpixel(-y + x_centre, x + y_centre, 7); } P = 1 - r; while (x > y) { y++; if (P <= 0) P = P + 2*y + 1; else { x--; P = P + 2*y - 2*x + 1; } if (x < y) break; putpixel(x + x_centre, y + y_centre, 7); putpixel(-x + x_centre, y + y_centre, 7); putpixel(x + x_centre, -y + y_centre, 7); putpixel(-x + x_centre, -y + y_centre, 7); delay(50); if (x != y) { putpixel(y + x_centre, x + y_centre, 7); putpixel(-y + x_centre, x + y_centre, 7); putpixel(y + x_centre, -x + y_centre, 7); putpixel(-y + x_centre, -x + y_centre, 7); delay(50); } } } int main() { int gd = DETECT, gm; int x,y,radius; initgraph(&gd, &gm, "c:\turboc3\bgi"); cout<<"nEnter X and Y cordinates :"; cin>>x,y); cout<<"nEnter radius :"; cin>>radius; midPointCircleDraw(x, y, radius); getchar(); closegraph(); return 0; } |
Output
Read Also
Bresenham Circle Drawing Algorithm in C and C++
Midpoint Ellipse Algorithm in C