Explanation
This C code implements the Midpoint Ellipse Drawing Algorithm using the Borland Graphics Interface (BGI), a common graphics library for C/C++ in older DOS-based environments. The goal is to draw smooth ellipses on a graphical screen by calculating points along the ellipse's arc and plotting them.
Here's a step-by-step breakdown:
-
Includes:
graphics.h: This header file provides functions for graphics operations like initializing the graphics mode, drawing pixels (putpixel), and closing the graphics screen.conio.h: This header provides console input/output functions, specificallygetch()which is used here to pause the program until a key is pressed.stdio.h: This is the standard input/output library, though it's not strictly used in this particular code snippet for I/O, it's a common include.
-
plotPoints(int xc, int yc, int x, int y)Function:- This helper function takes the center coordinates of the ellipse (
xc,yc) and a point(x, y)relative to the center. - It leverages the eight-way symmetry of an ellipse. For every point
(x, y)calculated in the first quadrant, there are four corresponding symmetric points in all four quadrants relative to the center(xc, yc). - It uses
putpixel(x_coord, y_coord, WHITE)to draw these four pixels in white color. The four points are(xc+x, yc+y),(xc-x, yc+y),(xc+x, yc-y), and(xc-x, yc-y).
- This helper function takes the center coordinates of the ellipse (
-
drawEllipse(int xc, int yc, int rx, int ry)Function:- This is the core function that implements the Midpoint Ellipse Algorithm.
- It takes the center coordinates (
xc,yc) and the major and minor radii (rx,ry). - Initialization: It calculates
rx2 = rx * rxandry2 = ry * ry(squared radii) andtwoRx2 = 2 * rx2,twoRy2 = 2 * ry2. These pre-calculations optimize the loops by avoiding repeated multiplications.xandyare initialized to0andryrespectively, starting at the topmost point of the ellipse(0, ry)relative to its center.pxandpyare also initialized; these represent2 * ry^2 * xand2 * rx^2 * yrespectively, which are used in the decision parameter updates. - Region 1: This region covers the part of the ellipse where the absolute value of the slope
|dy/dx|is less than or equal to 1. It starts from(0, ry)and goes untilpx >= py.- It uses a decision parameter
pto determine which pixel to choose next: either(x+1, y)or(x+1, y-1). - The initial
pis set based on the midpoint concept. - Inside the
whileloop:plotPointsis called to draw the current(x, y)point and its symmetric counterparts.xis incremented.pxis updated (px += twoRy2).- If
p < 0, the midpoint is inside the ellipse, so the next point chosen is(x+1, y).pis updated accordingly. - If
p >= 0, the midpoint is outside or on the ellipse, so the next point chosen is(x+1, y-1).yis decremented,pyis updated, andpis updated.
- It uses a decision parameter
- Region 2: This region covers the part of the ellipse where the absolute value of the slope
|dy/dx|is greater than 1. It starts from where Region 1 left off and goes untilybecomes 0.- The decision parameter
pis re-initialized for this region using the last(x, y)point from Region 1. - Inside the
whileloop:plotPointsis called for the current(x, y)point.yis decremented.pyis updated (py -= twoRx2).- If
p > 0, the midpoint is outside the ellipse, so the next point chosen is(x, y-1).pis updated. - If
p <= 0, the midpoint is inside or on the ellipse, so the next point chosen is(x+1, y-1).xis incremented,pxis updated, andpis updated.
- The loop continues until
yreaches 0, completing the quarter ellipse segment.
- The decision parameter
-
main()Function:int gd = DETECT, gm;:gd(graphics driver) is set toDETECTto auto-detect the graphics hardware, andgm(graphics mode) will be set byinitgraph.initgraph(&gd, &gm, "C:\\TC\\BGI");: This function initializes the graphics system. It loads the appropriate graphics driver (e.g., EGA, VGA) from the specified path (C:\TC\BGI) and sets the system to graphics mode.drawEllipse(162,239,70,100);: CallsdrawEllipseto draw an ellipse centered at(162, 239)withrx=70andry=100. This will be a taller, vertically oriented ellipse.drawEllipse(486,239,100,70);: CallsdrawEllipseagain to draw another ellipse centered at(486, 239)withrx=100andry=70. This will be a wider, horizontally oriented ellipse.getch();: Pauses the program execution until a key is pressed, allowing the user to view the drawn ellipses.closegraph();: De-initializes the graphics system and returns the screen to text mode.return 0;: Indicates successful program execution.
In essence, the code calculates one segment of an ellipse (from (0, ry) to (rx, 0) in the first quadrant), and then uses symmetry to draw the other seven segments, resulting in a complete ellipse. It uses integer arithmetic and decision parameters for efficiency, avoiding floating-point calculations within the main loops as much as possible.