Beginner Project · Tutorial
How to Draw a House Using graphics.h in C
What You Will Build
By the end of this tutorial you will have a complete C program that draws a house scene: a rectangular house body with a triangular roof, a door, a window, a sun in the sky, and color fills applied to each part. This is one of the most common beginner projects assigned in Indian university labs for Turbo C graphics courses.
The coordinate system in graphics.h starts from (0, 0) at the top-left corner of the screen.
X increases to the right, Y increases downward. Keep this in mind when placing shapes.
Functions Used in This Tutorial
| Function | Purpose | Doc Page |
|---|---|---|
initgraph() | Initialize the graphics window | Graphics Initialization |
rectangle() | Draw the house walls and door | rectangle() |
line() | Draw the triangular roof | line() |
circle() | Draw the sun | circle() |
floodfill() | Fill areas with color | Polygons & Fill |
setfillstyle() | Set the fill color/pattern | Fill & Patterns |
setcolor() | Set the drawing color | Colors |
closegraph() | Close the graphics window | Graphics Initialization |
Set Up the Graphics Window
Every graphics.h program starts with initgraph().
This initializes the BGI graphics mode and opens the drawing window. We use DETECT
so graphics.h automatically picks the best available driver.
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
/* Drawing code goes here */
getch();
closegraph();
return 0;
}
Draw the House Body
Use rectangle(left, top, right, bottom) to draw the main walls.
We will position the house in the center-bottom area of a 640×480 window.
setcolor(WHITE); /* rectangle(left, top, right, bottom) */ rectangle(180, 250, 460, 420); /* house body */
Draw the Roof
graphics.h has no built-in triangle function, so we draw the roof using three line() calls —
one for each side of the triangle. The peak is centered above the house body.
/* Roof: three lines forming a triangle */ line(180, 250, 320, 130); /* left side of roof */ line(460, 250, 320, 130); /* right side of roof */ line(180, 250, 460, 250); /* base of roof (top wall) */
Add a Door and Window
Use two more rectangle() calls — one for the door at the bottom-center of the house,
and one for a window on the left side.
/* Door: center-bottom of the house */ rectangle(295, 340, 365, 420); /* Window: left side of the house */ rectangle(210, 285, 275, 330);
Draw the Sun
Use circle(x, y, radius) to draw a sun in the top-right sky area.
setcolor(YELLOW); circle(540, 80, 45); /* sun in the top-right corner */
Fill Colors with floodfill()
floodfill(x, y, border_color) fills an area starting from
point (x, y) until it hits a pixel matching border_color.
Call setfillstyle(SOLID_FILL, color) first to set the fill color.
floodfill() must be inside
the closed shape, not on the border. If you get it wrong, the color will flood the entire screen.
/* Fill house body with CYAN */ setfillstyle(SOLID_FILL, CYAN); floodfill(320, 350, WHITE); /* Fill roof with RED */ setfillstyle(SOLID_FILL, RED); floodfill(320, 200, WHITE); /* Fill door with BROWN */ setfillstyle(SOLID_FILL, BROWN); floodfill(330, 380, WHITE); /* Fill window with LIGHTBLUE */ setfillstyle(SOLID_FILL, LIGHTBLUE); floodfill(240, 307, WHITE); /* Fill sun with YELLOW */ setfillstyle(SOLID_FILL, YELLOW); floodfill(540, 80, YELLOW);
Complete Program — Draw a House in graphics.h
Here is the full program combining all steps above. You can copy and run it in the online compiler instantly.
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
/* --- House body --- */
setcolor(WHITE);
rectangle(180, 250, 460, 420);
/* --- Roof --- */
line(180, 250, 320, 130);
line(460, 250, 320, 130);
line(180, 250, 460, 250);
/* --- Door --- */
rectangle(295, 340, 365, 420);
/* --- Window --- */
rectangle(210, 285, 275, 330);
/* --- Sun --- */
setcolor(YELLOW);
circle(540, 80, 45);
/* --- Color fills --- */
setfillstyle(SOLID_FILL, CYAN);
floodfill(320, 350, WHITE); /* house walls */
setfillstyle(SOLID_FILL, RED);
floodfill(320, 200, WHITE); /* roof */
setfillstyle(SOLID_FILL, BROWN);
floodfill(330, 380, WHITE); /* door */
setfillstyle(SOLID_FILL, LIGHTBLUE);
floodfill(240, 307, WHITE); /* window */
setfillstyle(SOLID_FILL, YELLOW);
floodfill(540, 80, YELLOW); /* sun */
getch();
closegraph();
return 0;
}
Common Errors and Fixes
| Problem | Cause | Fix |
|---|---|---|
| Color floods entire screen | floodfill() point is on the border or outside the shape | Move the point 1–2 pixels inside the shape boundary |
| Roof does not close properly | Line endpoints do not meet the rectangle corners exactly | Use the same x/y values as the rectangle() call |
| Graphics window does not open | initgraph() path or driver issue | Pass "" as the path with DETECT driver |
| Colors look wrong | Wrong color constant used | See the Colors & Palette reference for all 16 constants |