Parameters
| Name | Type | Description |
|---|---|---|
left | int | The x-coordinate of the left edge. Smaller x values are closer to the left side of the screen. |
top | int | The y-coordinate of the top edge. Smaller y values are closer to the top of the screen. |
right | int | The x-coordinate of the right edge. This should usually be greater than left. |
bottom | int | The y-coordinate of the bottom edge. This should usually be greater than top. |
The rectangle is drawn with the active color and line style. If you call setcolor(GREEN) before rectangle(), the border appears green. If you call setlinestyle() before drawing, the border can become dotted, dashed, or thicker, depending on the mode you select.
Return Value
rectangle() returns void. It does not report invalid corner order, clipping, or visibility. If part of a rectangle lies outside the viewport, graphics.h clips it to the visible region. In most assignments this is acceptable, but for neat diagrams you should calculate coordinates so the full box remains on screen.
Code Examples
Simple rectangle border
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setcolor(LIGHTGREEN);
rectangle(150, 100, 500, 350);
getch();
closegraph();
return 0;
}
The output is a light green rectangular outline. Its left edge starts at x = 150, top edge at y = 100, right edge at x = 500, and bottom edge at y = 350.
Nested frames
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
int i;
initgraph(&gd, &gm, "");
for (i = 0; i < 5; i++) {
setcolor(i + 1);
rectangle(80 + i * 25, 60 + i * 20, 560 - i * 25, 420 - i * 20);
}
getch();
closegraph();
return 0;
}
This creates several rectangular frames inside each other. Each loop iteration moves the top-left corner inward and the bottom-right corner inward, so the boxes become progressively smaller.
Rectangle with diagonals
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
int left = 140, top = 90, right = 500, bottom = 360;
initgraph(&gd, &gm, "");
setcolor(CYAN);
rectangle(left, top, right, bottom);
line(left, top, right, bottom);
line(right, top, left, bottom);
getch();
closegraph();
return 0;
}
The output is a rectangle crossed by two diagonals. This example is useful when explaining corners, center intersection, and bounding boxes.
Common Mistakes
Swapping top and bottom
Beginners sometimes use a larger y value for top and a smaller y value for bottom. Remember that y increases downward, so the top edge normally has the smaller number.
Expecting the inside to be filled
rectangle() draws the outline only. Use bar() to draw a filled rectangle, or draw the outline after bar() if you need both fill and border.
Using width and height as arguments
The third and fourth parameters are not width and height. They are the right and bottom coordinates. To draw from a position with a known size, use right = left + width and bottom = top + height.
Try it online
Open the compiler, paste an example, and adjust the four edge coordinates. Watching the rectangle move is the fastest way to understand graphics.h screen geometry.