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

Beginner Project · Tutorial

Drawing Patterns and Designs with graphics.h

Learn to create geometric patterns and repeating designs in graphics.h using loops and drawing functions. This tutorial covers a checkerboard, a spiral, and a star pattern — all with complete Turbo C source code you can run in the browser.

Pattern Drawing Techniques

Patterns in graphics.h are created by combining loops with drawing functions. The key insight is that most geometric patterns are based on mathematical relationships — offsets, angles, radii — that you express as loop variables. Here are the core patterns and what drives them:

Grid Patterns

Use nested for loops with fixed step sizes. Increment X and Y by the cell size each iteration.

Radial Patterns

Loop over angles (0–360°). Use sin() and cos() from <math.h> to compute endpoints.

Concentric Shapes

Loop with an increasing radius. Draw the same shape (circle, rectangle) at each radius step.

Pattern 1 — Checkerboard

A checkerboard is drawn using a nested loop. For each cell, alternate between filling with WHITE and DARKGRAY based on whether the sum of the row and column index is even or odd. We use bar() for filled rectangles.

Checkerboard Pattern
#include <graphics.h>
#include <conio.h>

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

    int cellSize = 40;
    int cols = getmaxx() / cellSize;
    int rows = getmaxy() / cellSize;

    int r, c;
    for (r = 0; r < rows; r++) {
        for (c = 0; c < cols; c++) {
            /* Alternate colors based on row + column parity */
            if ((r + c) % 2 == 0)
                setfillstyle(SOLID_FILL, WHITE);
            else
                setfillstyle(SOLID_FILL, DARKGRAY);

            bar(c * cellSize, r * cellSize,
                (c + 1) * cellSize, (r + 1) * cellSize);
        }
    }

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

Click Run to see the pattern in the browser.

💡 Customize it: Change cellSize to 20 for a fine grid, or try CYAN and BLUE for a colored checkerboard.

Pattern 2 — Concentric Circles

Draw a series of circles from the center of the screen, each one larger than the last. Alternate colors between each ring to create a target/bullseye effect.

Concentric Circles Pattern
#include <graphics.h>
#include <conio.h>

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

    int cx = getmaxx() / 2;
    int cy = getmaxy() / 2;
    int maxRadius = (cx < cy) ? cx : cy; /* use smaller dimension */

    int colors[] = {RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA, WHITE};
    int numColors = 7;
    int step = maxRadius / numColors;

    int i;
    for (i = numColors - 1; i >= 0; i--) {
        setfillstyle(SOLID_FILL, colors[i]);
        fillellipse(cx, cy, (i + 1) * step, (i + 1) * step);
    }

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

Click Run to see the pattern.

Pattern 3 — Star Burst (Radial Lines)

A star burst is drawn by looping from 0 to 360 degrees and drawing a line from the center to each angle's point on the circumference. We use sin() and cos() from <math.h> to compute the endpoint.

Radians: graphics.h angles in arc() and ellipse() use degrees. But sin() and cos() from <math.h> use radians. Convert: radians = degrees * 3.14159 / 180.
Star Burst Pattern
#include <graphics.h>
#include <conio.h>
#include <math.h>

#define PI 3.14159265

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

    int cx = getmaxx() / 2;
    int cy = getmaxy() / 2;
    int radius = 200;
    int angle;

    int colors[] = {RED,YELLOW,GREEN,CYAN,BLUE,MAGENTA,WHITE,LIGHTRED,LIGHTGREEN,LIGHTCYAN};

    for (angle = 0; angle < 360; angle += 5) {
        double rad = angle * PI / 180.0;
        int ex = cx + (int)(radius * cos(rad));
        int ey = cy + (int)(radius * sin(rad));

        setcolor(colors[(angle / 5) % 10]);
        line(cx, cy, ex, ey);
    }

    /* Draw center dot */
    setfillstyle(SOLID_FILL, WHITE);
    fillellipse(cx, cy, 6, 6);

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

Click Run to see the pattern.

Pattern 4 — Color Gradient Bars

Draw vertical color bars that cycle through all 16 graphics.h color constants. Each bar is one-sixteenth of the screen width. This demonstrates how to use all colors in a single program.

Color Gradient Bars
#include <graphics.h>
#include <conio.h>

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

    int maxX = getmaxx();
    int maxY = getmaxy();
    int barWidth = maxX / 16;
    int i;

    for (i = 0; i < 16; i++) {
        setfillstyle(SOLID_FILL, i); /* colors 0–15 */
        bar(i * barWidth, 0, (i + 1) * barWidth, maxY);
    }

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

Click Run to see all 16 graphics.h colors.

This is the fastest way to see all available colors. Note that color 0 = BLACK and color 15 = WHITE. See the full reference in Colors & Palette.

Challenge: Combine the Patterns

Now that you have learned four individual patterns, try combining them into one program:

  1. Draw the checkerboard as the background.
  2. Draw the concentric circles in the center.
  3. Draw the star burst on top of the circles.

This is a great lab assignment challenge. Use setviewport() to restrict drawing to different screen regions, or layer shapes directly.

💡 Hint: Draw the checkerboard first, then the circles, then the lines. Each subsequent draw goes on top of the previous one.
What to Try Next Go back to the Draw a House tutorial, or explore Fill Styles and Patterns to learn how to apply hatching and dot patterns to shapes.