Explanation
This C code implements the Midpoint Ellipse Drawing Algorithm, a fundamental technique in computer graphics for rendering ellipses efficiently. The program uses the BGI (Borland Graphics Interface) library, a common choice for graphics programming in older C/C++ environments, to draw shapes on a graphical screen.
The core idea behind the Midpoint Ellipse Algorithm is to draw an ellipse by choosing the next pixel based on a decision parameter, which helps determine whether to move horizontally, vertically, or diagonally to best approximate the ellipse curve. This algorithm leverages the symmetry of an ellipse to reduce the number of calculations needed: it only computes points for one octant (or quadrant, depending on how you view the regions) and then uses plotPoints to reflect these points across the center to draw the full ellipse.
Let's break down the code:
-
main()function:- This is the entry point of the program.
int gd = DETECT, gm;declares variables for graphics driver and mode.DETECTautomatically detects the graphics hardware.initgraph(&gd, &gm, "C:\\TC\\BGI");initializes the graphics system. It tells the program to look for the BGI driver files in the specified path ("C:\TC\BGI"). If successful, it switches the display to graphics mode.drawEllipse(324,239,50,50);calls thedrawEllipsefunction to draw a circle (an ellipse with equal radii) centered at (324, 239) with a radius of 50 pixels.drawEllipse(324,239,100,50);callsdrawEllipseagain to draw a wider ellipse, also centered at (324, 239), with a horizontal radius (rx) of 100 and a vertical radius (ry) of 50.getch();waits for the user to press any key before proceeding. This keeps the graphics window open so you can see the output.closegraph();unloads the graphics driver and switches the display back to text mode.return 0;indicates successful program execution.
-
plotPoints(int xc, int yc, int x, int y)function:- This helper function takes the center coordinates (
xc,yc) and a relative offset(x, y)from the origin (0,0) of the current octant. - It then plots four symmetric points around the center
(xc, yc):(xc + x, yc + y): The point in the first quadrant.(xc - x, yc + y): Reflection across the Y-axis.(xc + x, yc - y): Reflection across the X-axis.(xc - x, yc - y): Reflection across both X and Y axes.
putpixel(x_coord, y_coord, WHITE);draws a single pixel at the specified coordinates in white color. This effectively draws portions of the ellipse in all four quadrants simultaneously.
- This helper function takes the center coordinates (
-
drawEllipse(int xc, int yc, int rx, int ry)function:-
This is the core implementation of the Midpoint Ellipse Algorithm.
-
It takes the center coordinates (
xc,yc) and the horizontal (rx) and vertical (ry) radii. -
Variable Initialization:
rx2 = (long)rx * rx;andry2 = (long)ry * ry;pre-calculate the squares of the radii, cast tolongto prevent overflow for larger radii.twoRx2 = 2 * rx2;andtwoRy2 = 2 * ry2;pre-calculate2*rx^2and2*ry^2for efficiency, as these terms are used repeatedly in the decision parameter updates.x = 0; y = ry;sets the starting point for drawing in the first region, which is(0, ry)(the topmost point of the ellipse).px = 0; py = twoRx2 * y;initializespxandpy, which represent2*ry^2*xand2*rx^2*yrespectively. These values are crucial for determining the slope condition to switch between regions.
-
Region 1 (Top-Right Octant/Quadrant portion):
- This region starts from
(0, ry)and continues until the slope of the ellipse becomes -1 (i.e.,px < pycondition). p = (long)(ry2 - (rx2 * ry) + (0.25 * rx2) + 0.5);initializes the decision parameterp. This initial value is derived from the midpoint equation for the ellipse at the starting point. The0.25and0.5terms are used for midpoint evaluation.while (px < py): The loop continues as long as2*ry^2*x < 2*rx^2*y, which implies that the absolute value of the slopedy/dxis less than 1.plotPoints(xc, yc, x, y);draws the current point and its symmetric counterparts.x++; px += twoRy2;incrementsxand updatespx(which is2*ry^2*x).if (p < 0): Ifpis negative, the midpoint is inside the ellipse, so the East (E) pixel is chosen.pis updated by addingry2 + px.else: Ifpis non-negative, the midpoint is outside or on the ellipse, so the South-East (SE) pixel is chosen.y--decrementsy,py -= twoRx2updatespy, andpis updated by addingry2 + px - py.
- This region starts from
-
Region 2 (Bottom-Right Octant/Quadrant portion):
- This region takes over from where Region 1 left off, continuing until
yreaches 0 (the rightmost point of the ellipse). In this region, the absolute value of the slopedy/dxis greater than 1. p = (long)(ry2 * (x + 0.5) * (x + 0.5) + rx2 * (y - 1) * (y - 1) - rx2 * ry2 + 0.5);re-initializes the decision parameterpfor Region 2. This formula uses the last(x, y)from Region 1 to calculate the midpoint for the next pixel choice.while (y > 0): The loop continues as long asyis greater than 0.plotPoints(xc, yc, x, y);draws the current point and its symmetric counterparts.y--; py -= twoRx2;decrementsyand updatespy(which is2*rx^2*y).if (p > 0): Ifpis positive, the midpoint is outside the ellipse, so the South (S) pixel is chosen.pis updated by addingrx2 - py.else: Ifpis non-positive, the midpoint is inside or on the ellipse, so the South-East (SE) pixel is chosen.x++incrementsx,px += twoRy2updatespx, andpis updated by addingrx2 - py + px.
- This region takes over from where Region 1 left off, continuing until
-
This two-region approach ensures that the algorithm always takes steps in the direction that best approximates the ellipse's curve, minimizing visual jaggedness.