Explanation
This C code implements the Midpoint Circle Algorithm to draw circles on a graphical display using the Borland Graphics Interface (BGI). The BGI is a legacy graphics library commonly used with compilers like Turbo C/C++.
Let's break down the code:
-
#includedirectives:graphics.h: This header file provides access to the BGI functions (likeinitgraph,putpixel,circle,rectangle,line,getch,closegraph).conio.h: This header is typically used for console input/output, specificallygetch()in this case, which waits for a key press.stdio.h: Standard input/output library, though not strictly necessary for this specific graphics code, it's a common inclusion.
-
circlePlotPoints(int xc, int yc, int x, int y)function:- This helper function takes the center coordinates (
xc,yc) and a point(x, y)from the first octant (0 to 45 degrees) of the circle. - Due to the 8-way symmetry of a circle, if we know one point
(x, y)in the first octant, we can derive seven other symmetric points. putpixel(x_coordinate, y_coordinate, color)is a BGI function that draws a single pixel at the specified coordinates with the given color (here,WHITE).- This function plots all eight symmetric points for a given
(x, y)pair, effectively drawing a segment of the circle in all octants simultaneously.
- This helper function takes the center coordinates (
-
circleMidpoint(int xc, int yc, int r)function:- This is the core function that implements the Midpoint Circle Algorithm.
xc,ycare the center coordinates of the circle, andris its radius.- It initializes
x = 0andy = r. These are the starting coordinates, representing the topmost point of the circle (or rightmost, depending on perspective, butx=0, y=ris on the y-axis, 90 degrees). p = 1 - r: This is the initial decision parameter. The decision parameter helps determine which pixel to choose in the next step (either(x+1, y)or(x+1, y-1)) by checking if the midpoint between these two candidate pixels is inside or outside the circle.circlePlotPoints(xc, yc, x, y): Plots the initial point and its 7 symmetric counterparts.while (x < y): The loop continues as long asxis less thany. This condition ensures that we only calculate points for the first octant (from 0 to 45 degrees). Oncexbecomes equal to or greater thany, we have covered the first 45 degrees, and the rest of the circle can be generated by symmetry.x++: In each iteration,xis incremented.- Decision Logic (
if (p < 0)/else):- If
p < 0, the midpoint is inside the circle or on its boundary. The next pixel chosen is(x+1, y). The decision parameterpis updated byp += 2 * x + 1. - If
p >= 0, the midpoint is outside the circle. The next pixel chosen is(x+1, y-1). In this case,yis decremented (y--), andpis updated byp += 2 * (x - y) + 1.
- If
circlePlotPoints(xc, yc, x, y): After deciding the next(x, y)coordinate, all eight symmetric points are plotted.
-
main()function:int gd = DETECT, gm;:gd(graphics driver) is set toDETECTto auto-detect the graphics hardware, andgm(graphics mode) will be determined byinitgraph.initgraph(&gd, &gm, "C:\\TC\\BGI");: This crucial BGI function initializes the graphics system. It takes pointers togdandgmand the path to the BGI driver files (e.g.,EGAVGA.BGI). The pathC:\\TC\\BGIis a common location for these files in a Turbo C++ installation.- The code then makes several calls to
circleMidpointto draw circles at the screen corners and other locations. - It also uses standard BGI functions like
rectangle(left, top, right, bottom)andline(x1, y1, x2, y2)to draw rectangles and lines, creating a composite graphic. Notice the drawing of a "sun" with rays using a circle and multiple lines. getch(): This function fromconio.hwaits for the user to press any key before proceeding. This keeps the graphics window open until the user is ready to close it.closegraph(): This BGI function unloads the graphics driver and restores the screen to its original text mode.return 0;: Indicates successful program execution.
In essence, this program demonstrates how to draw circles efficiently using an integer-only algorithm, then combines these circles with other basic shapes to create a simple graphical output.