Home Docs Tutorials
graphics.h Tutorials
Compiler
Beginner Project ⏱ 8 min read

Beginner Project · Tutorial

How to Draw a House Using graphics.h in C

In this tutorial you will build a complete house drawing program using graphics.h in Turbo C — step by step. You will use rectangle() for the walls and door, line() for the roof triangles, circle() for the sun, and floodfill() to add colors. The full program is runnable directly in your browser.

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.

💡 Tip: You can run each step's code snippet in the online compiler to see the partial result before moving on. You do not need Turbo C installed locally.

Functions Used in This Tutorial

FunctionPurposeDoc Page
initgraph()Initialize the graphics windowGraphics Initialization
rectangle()Draw the house walls and doorrectangle()
line()Draw the triangular roofline()
circle()Draw the suncircle()
floodfill()Fill areas with colorPolygons & Fill
setfillstyle()Set the fill color/patternFill & Patterns
setcolor()Set the drawing colorColors
closegraph()Close the graphics windowGraphics Initialization
1

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.

Initialization Template
#include <graphics.h>
#include <conio.h>

int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "");

    /* Drawing code goes here */

    getch();
    closegraph();
    return 0;
}
2

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.

House Walls
setcolor(WHITE);
/* rectangle(left, top, right, bottom) */
rectangle(180, 250, 460, 420); /* house body */
3

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.

Triangular Roof
/* 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) */
4

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 and Window
/* Door: center-bottom of the house */
rectangle(295, 340, 365, 420);

/* Window: left side of the house */
rectangle(210, 285, 275, 330);
5

Draw the Sun

Use circle(x, y, radius) to draw a sun in the top-right sky area.

Sun
setcolor(YELLOW);
circle(540, 80, 45); /* sun in the top-right corner */
6

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.

⚠ Important: The point you pass to floodfill() must be inside the closed shape, not on the border. If you get it wrong, the color will flood the entire screen.
Color Fills
/* 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;
}

Click Run to test the house program in your browser.

Common Errors and Fixes

ProblemCauseFix
Color floods entire screenfloodfill() point is on the border or outside the shapeMove the point 1–2 pixels inside the shape boundary
Roof does not close properlyLine endpoints do not meet the rectangle corners exactlyUse the same x/y values as the rectangle() call
Graphics window does not openinitgraph() path or driver issuePass "" as the path with DETECT driver
Colors look wrongWrong color constant usedSee the Colors & Palette reference for all 16 constants
What to Try Next Add a chimney using bar(), draw clouds with ellipse(), or try the Animation tutorial to make your house scene come to life.