A recursive binary fractal tree renderer built in C using raylib. Each branch splits into two children at a configurable angle, with length and thickness tapering across n generations.
- Recursive
DrawBranchfunction with configurable depth - Adjustable branch angle, reduction factor, and thickness
- All parameters exposed as
#defineconstants — easy to tweak
- C compiler — GCC, Clang, or MSVC
- raylib — installation guide
gcc main.c -o fractal -lraylib -lm
./fractalgcc main.c -o fractal.exe -lraylib -lwinmm -lgdi32 -lopengl32 -lm
fractal.exeAll tuneable parameters are at the top of main.c:
| Constant | Default | Description |
|---|---|---|
WIDTH / HEIGHT |
1500 |
Window dimensions in pixels |
BASE_LENGTH |
300 |
Length of the trunk branch |
BASE_THICKNESS |
15 |
Thickness of the trunk branch |
REDUCTION_FACTOR |
0.8 |
Length/thickness multiplier per generation |
BASE_ANGLE |
0 |
Starting angle in radians (0 = straight up) |
ANGLE_INCREASE_FACTOR |
0.3 |
Angle offset added/subtracted at each split |
MAX_GENERATIONS |
10 |
Maximum recursion depth |
BACKGROUND_COLOUR |
RAYWHITE |
Background fill colour |
FRACTAL_COLOUR |
BLACK |
Branch draw colour |
Wider spread — increase the angle factor:
#define ANGLE_INCREASE_FACTOR 0.6Denser canopy — increase generations and tighten reduction:
#define MAX_GENERATIONS 14
#define REDUCTION_FACTOR 0.75Leaning tree — offset the base angle:
#define BASE_ANGLE 0.3DrawBranch is a simple recursive function:
- Computes the endpoint of the current branch from its start position, length, and angle using
sinf/cosf. - Draws the line with
DrawLineEx. - Calls itself twice — once rotating left by
ANGLE_INCREASE_FACTOR, once rotating right — with reduced length and thickness. - Returns when
generation > MAX_GENERATIONS.
The total number of branches drawn is 2^(MAX_GENERATIONS+1) - 1 (2047 at the default depth of 10).
MIT — do whatever you like with it.
