C Exercise Example58
## C Programming Exercise - Example 58: Drawing Rectangles with Graphics
In this exercise, we will explore how to use the `graphics.h` library in C to draw geometric shapes. Specifically, we will learn how to use the `rectangle()` function to draw nested squares, combine them with other shapes like circles and lines, and render text on the screen.
---
### 1. Introduction to `graphics.h` and `rectangle()`
The `graphics.h` library was historically used in DOS-based environments (such as Turbo C/C++) to create 2D graphics.
The `rectangle()` function is used to draw a 2D outline of a rectangle on the screen using the current drawing color and line style.
#### Syntax:
```c
void rectangle(int left, int top, int right, int bottom);
```
#### Parameter Description:
* **`left`**: The X-coordinate of the top-left corner.
* **`top`**: The Y-coordinate of the top-left corner.
* **`right`**: The X-coordinate of the bottom-right corner.
* **`bottom`**: The Y-coordinate of the bottom-right corner.
---
### 2. Problem Analysis
The goal of this program is to draw a series of concentric, expanding squares (rectangles with equal width and height) using a loop.
1. **Initialization**: Initialize the graphics system using `initgraph()`.
2. **Background Color**: Set the background color to yellow using `setbkcolor(YELLOW)`.
3. **Looping Rectangles**: Start with a small central square defined by coordinates `(x0, y0)` and `(x1, y1)`. In each iteration of a `for` loop:
* Draw the rectangle.
* Expand the boundaries outward by shifting the top-left corner up and left (`x0 -= 5`, `y0 -= 5`) and the bottom-right corner down and right (`x1 += 5`, `y1 += 5`).
4. **Text and Accents**: Display a text message using `outtextxy()`, draw an underline using `line()`, and draw an outer bounding circle using `circle()`.
---
### 3. Source Code Implementation
Below is the complete C program. This code is designed to run in environments that support the legacy Turbo C graphics library (such as Turbo C++, WinBGIm, or DOSBox).
```c
/**
* Description: C Exercise Example 58 - Drawing Rectangles
* Target Environment: Turbo C / DOSBox / WinBGIm
*/
#include
#include
int main()
{
int x0, y0, x1, y1, driver, mode, i;
// Set graphics driver to VGA and mode to VGAHI (640x480 resolution)
driver = VGA;
mode = VGAHI;
// Initialize the graphics system
initgraph(&driver, &mode, "");
// Set background color to yellow
setbkcolor(YELLOW);
// Initial coordinates for the innermost square
x0 = 263;
y0 = 263;
x1 = 275;
y1 = 275;
// Draw 19 nested squares, expanding outwards
for(i = 0; i <= 18; i++)
{
setcolor(1); // Set drawing color (Blue in standard VGA palette)
rectangle(x0, y0, x1, y1);
// Expand coordinates for the next iteration
x0 = x0 - 5;
y0 = y0 - 5;
x1 = x1 + 5;
y1 = y1 + 5;
}
// Set text style: Default font, Horizontal direction, Size 2
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
// Output text at coordinates (150, 40)
outtextxy(150, 40, "How beautiful it is!");
// Draw a horizontal line under the text
line(130, 60, 480, 60);
// Set color to Green (2) and draw a bounding circle around the squares
setcolor(2);
circle(269, 269, 137);
// Wait for a key press before closing the graphics window
getch();
closegraph();
return 0;
}
```
---
### 4. Key Functions Explained
* **`initgraph(&driver, &mode, "path")`**: Initializes the graphics system by loading the graphics driver from disk and putting the system into graphics mode.
* **`setbkcolor(color)`**: Sets the background color of the viewport.
* **`setcolor(color)`**: Sets the current drawing color. In the standard 16-color VGA palette, `1` represents Blue and `2` represents Green.
* **`circle(x, y, radius)`**: Draws a circle with its center at `(x, y)` and the specified `radius`.
* **`outtextxy(x, y, text)`**: Displays a string at the specified `(x, y)` coordinate.
---
### 5. Modern Compilation Considerations
Since `graphics.h` is a non-standard, deprecated library from the MS-DOS era, compiling this code on modern operating systems (Windows 10/11, macOS, Linux) with modern compilers (GCC/Clang) requires extra setup:
1. **Using Turbo C++ in DOSBox**: This is the easiest way to run the code exactly as written. You can download a pre-configured Turbo C++ environment that runs inside the DOSBox emulator.
2. **Using WinBGIm (Windows)**: WinBGIm is a port of the Turbo C graphics library for modern GCC compilers (like MinGW). You can link `libbgi.a` and include `graphics.h` in IDEs like Code::Blocks or Dev-C++.
3. **Modern Alternatives**: If you are building modern graphical applications in C, it is highly recommended to use modern cross-platform libraries such as **SDL2**, **Raylib**, or **OpenGL** instead of the legacy `graphics.h`.
YouTip