Midpoint Ellipse Algorithm in C
Here you will learn the program code of midpoint ellipse algorithm in C programming using Computer Graphics.
What is midpoint ellipse algorithm
The Midpoint Ellipse Algorithm is a computational technique which is used to render and draw the visually appealing shapes of ellipses on various digital screens.
Program code of Midpoint Ellipse 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <alloc.h> void drawEllipse(int xc, int yc, int a, int b) { int x = 0, y = b; long a2 = a * a; long b2 = b * b; long twoA2 = 2 * a2; long twoB2 = 2 * b2; long fourA2 = 4 * a2; long fourB2 = 4 * b2; long d; // Region 1 d = b2 - a2 * b + 0.25 * a2; while (twoB2 * x <= twoA2 * y) { // Draw region 1 putpixel(xc + x, yc + y, 15); putpixel(xc - x, yc + y, 15); putpixel(xc + x, yc - y, 15); putpixel(xc - x, yc - y, 15); delay(50); if (d < 0) { x++; d += twoB2 * x + b2; } else { x++; y--; d += twoB2 * x - twoA2 * y + b2; } } // Region 2 d = b2 * (x + 0.5) * (x + 0.5) + a2 * (y - 1) * (y - 1) - a2 * b2; while (y >= 0) { // Draw points for all octants putpixel(xc + x, yc + y, 15); putpixel(xc - x, yc + y, 15); putpixel(xc + x, yc - y, 15); putpixel(xc - x, yc - y, 15); delay(50); if (d > 0) { y--; d -= twoA2 * y + a2; } else { x++; y--; d += twoB2 * x - twoA2 * y + a2; } } } int main() { int xc = 150; // center of x-coordinate int yc = 150; // center of y-coordinate int a = 90; // semi-major axis length int b = 60; // semi-minor axis length int gdriver=DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi"); errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } printf("\t\n Midpoint Ellipse Algoritm"); drawEllipse(xc, yc, a, b); getch(); return 0; } |
Output
Read Also
MidPoint Circle Drawing Algorithm in C
Midpoint Line drawing algorithm in C