Beginner Project · Tutorial
Drawing Patterns and Designs with graphics.h
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.
#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;
}
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.
#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;
}
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.
sin() and cos() from <math.h> use radians.
Convert: radians = degrees * 3.14159 / 180.
#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;
}
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.
#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;
}
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:
- Draw the checkerboard as the background.
- Draw the concentric circles in the center.
- 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.