Basic Shapes
rectangle() in graphics.h
The rectangle() function draws an unfilled rectangle using two corner coordinates. It is useful for UI boxes, boundaries, and shape composition.
On This Page
Syntax
void rectangle(int left, int top, int right, int bottom);
Draws rectangle outline from top-left (left, top) to bottom-right (right, bottom).
Parameters
left, top: top-left cornerright, bottom: bottom-right corner
rectangle(100, 100, 300, 220);
This draws a rectangle 200 px wide and 120 px high.
Rectangle edges are aligned with screen axes, so this function is very useful for windows, panels, UI borders, and framing regions in drawings.
Full Program
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
rectangle(80, 80, 280, 200);
rectangle(120, 120, 360, 260);
getch();
closegraph();
return 0;
}
Common Mistakes
- Passing corners in wrong order (left > right or top > bottom).
- Confusing
rectangle()(outline) withbar()(filled rectangle). - Forgetting graphics initialization and cleanup calls.
Next: continue with bar(), bar3d(), and Polygons & Fill.