Editor
Initializing Python runtime…
Output

Write a Fracta script and click Run

fractal output

FRACTA

A lightweight domain-specific language to define, render, and explore fractals.

1. Overview

Fracta is a line-oriented plaintext DSL that expresses fractal geometry through three computational engines: L-systems (string-rewriting turtle graphics), PIXEL (escape-time complex-plane iteration), and IFS (Iterated Function Systems via affine transforms). Scripts are deterministic and portable across any conforming interpreter.

A minimal Fracta program selects an engine, sets parameters, and issues RENDER:

ENGINE L_SYSTEM
AXIOM F--F--F
RULE F -> F+F--F+F
ANGLE 60
ITER 4
RENDER

2. Lexical Rules

3. Engines

Select the rendering engine with the ENGINE directive at the top of your script.

L_SYSTEM   Lindenmayer String-Rewriting

Applies production rules to a seed string (axiom) iteratively, then interprets the result as turtle-graphics commands. Produces curves, trees, snowflakes, and space-filling patterns.

Required: AXIOM, RULE (one or more), ANGLE, ITER

ENGINE L_SYSTEM
AXIOM FX
RULE X -> X+YF+
RULE Y -> -FX-Y
ANGLE 90
ITER 12
RENDER

Use RENDER_AS_GRID instead of RENDER to visualize 1D cellular automaton evolution as a 2D grid. In grid mode RULE uses context syntax: RULE center > right -> output.

PIXEL   Escape-Time Complex Iteration

Evaluates a complex formula on a grid of points, counting how many iterations before |z| > 2 (escape time). Classic Mandelbrot, Julia, Burning Ship, and custom formulas.

Required: FORMULA, X_RANGE, Y_RANGE, RES, ITER

Optional: COLORMAP (any matplotlib colormap), C_VAL (fixes c for Julia sets)

ENGINE PIXEL
FORMULA z**2 + c
X_RANGE -2.0 0.5
Y_RANGE -1.25 1.25
RES 600
ITER 100
COLORMAP magma
RENDER

Variables available in FORMULA: z (current iterate), c (parameter point or fixed constant), np (NumPy).

IFS   Iterated Function System

Randomly applies affine transformations with given probabilities. Each rule encodes prob, a, b, c, d, e, f where the transform is x' = ax + by + e, y' = cx + dy + f.

Required: RULE (one or more with 7 space-separated floats), ITER (number of random steps)

ENGINE IFS
ITER 80000
RULE 0.01  0.0   0.0   0.0   0.16  0.0  0.0
RULE 0.85  0.85  0.04 -0.04  0.85  0.0  1.6
RULE 0.07  0.2  -0.26  0.23  0.22  0.0  1.6
RULE 0.07 -0.15  0.28  0.26  0.24  0.0  0.44
RENDER

4. Directive Reference

DirectiveEnginesSyntaxDescription
ENGINEallENGINE L_SYSTEM | PIXEL | IFSSelects the rendering backend.
AXIOML_SYSTEMAXIOM <string>Seed string at iteration 0.
RULEL_SYSTEM / IFSRULE <pred> -> <succ>Production rule (L_SYSTEM) or affine transform row (IFS).
ANGLEL_SYSTEMANGLE <float> or ANGLE <float>rRotation step in degrees, or radians with r suffix. Positive = counterclockwise.
ITERallITER <int>Recursion depth (L_SYSTEM), escape iterations (PIXEL), or random steps (IFS).
FORMULAPIXELFORMULA <expr>Python expression in z, c, np.
X_RANGEPIXELX_RANGE <min> <max>Real-axis bounds.
Y_RANGEPIXELY_RANGE <min> <max>Imaginary-axis bounds.
RESPIXELRES <int>Grid resolution (pixels per side).
C_VALPIXELC_VAL <real>+<imag>jFixed parameter for Julia sets.
COLORMAPPIXEL / GRIDCOLORMAP <name>Any matplotlib colormap name.
RENDERallRENDERExecute and render the fractal.
RENDER_AS_GRIDL_SYSTEMRENDER_AS_GRIDRender evolution as 2D cell grid (cellular automata mode).

5. Turtle Graphics Alphabet (L_SYSTEM)

TokenActionState change
FDraw forwardx, y advance; line is drawn
GDraw forward (synonym)Same as F; useful for dual-variable rules
fMove forward (pen up)x, y advance; no line drawn
+Rotate counterclockwiseθ′ = θ + ANGLE
-Rotate clockwiseθ′ = θ − ANGLE
[Push stateSaves (x, y, θ) onto LIFO stack
]Pop stateRestores (x, y, θ) from stack
otherNOPPlaceholder only (e.g. X, Y for substitution)

The turtle starts at (0, 0) heading 90° (straight up). Angles follow the standard radial (anticlockwise-positive) convention.

6. ANGLE — Degrees vs Radians

By default ANGLE accepts degrees:

ANGLE 60       # 60 degrees

Append r to supply radians directly:

ANGLE 1.0472r  # π/3 ≈ 60°
ANGLE 3.14159r # π = 180°

Both formats follow the counterclockwise-positive convention: + increases θ, - decreases it.

7. Formal Grammar (EBNF)

<fracta_script> ::= { <statement> <newline> } ( <render_cmd> | <render_grid_cmd> )
<statement>     ::= <comment> | <engine_def> | <axiom_def> | <rule_def>
                  | <angle_def> | <iter_def> | <param_def>

<engine_def>    ::= "ENGINE " ( "L_SYSTEM" | "PIXEL" | "IFS" )
<axiom_def>     ::= "AXIOM " <string_payload>
<rule_def>      ::= "RULE " <char_token> " -> " <string_payload>
<angle_def>     ::= "ANGLE " <float_val> [ "r" ]
<iter_def>      ::= "ITER " <int_val>
<param_def>     ::= <upper_ident> " " <value_string>
<render_cmd>    ::= "RENDER"
<render_grid_cmd> ::= "RENDER_AS_GRID"

<float_val>     ::= [0-9]+ [ "." [0-9]+ ]
<int_val>       ::= [0-9]+
<comment>       ::= "#" { <any_char> }

8. Runtime Pipeline

┌─────────────────────────────────────┐
│ 1. Lexical Scanner                  │
│    Lines → directives → param map  │
└────────────────┬────────────────────┘
                 │
                 ▼
┌─────────────────────────────────────┐
│ 2. Production Expansion (L_SYSTEM)  │
│    Axiom → apply rules × ITER       │
└────────────────┬────────────────────┘
                 │
                 ▼
┌─────────────────────────────────────┐
│ 3. Render                           │
│    Turtle trajectory / PIXEL grid / │
│    IFS random walk → matplotlib     │
└─────────────────────────────────────┘

State vector during phase 3: S = ⟨x, y, θ, Stack⟩ where θ is initialized to 90° and Stack is a LIFO of (x, y, θ) triples. Unmatched ] tokens raise a runtime error.

9. Examples

Koch Snowflake

ENGINE L_SYSTEM
AXIOM F--F--F
RULE F -> F+F--F+F
ANGLE 60
ITER 4
RENDER

Fractal Plant

ENGINE L_SYSTEM
AXIOM X
RULE X -> F+[[X]-X]-F[-FX]+X
RULE F -> FF
ANGLE 25
ITER 5
RENDER

Heighway Dragon

ENGINE L_SYSTEM
AXIOM FX
RULE X -> X+YF+
RULE Y -> -FX-Y
ANGLE 90
ITER 12
RENDER

Mandelbrot Set

ENGINE PIXEL
FORMULA z**2 + c
X_RANGE -2.0 0.5
Y_RANGE -1.25 1.25
RES 600
ITER 100
COLORMAP magma
RENDER

Julia Set (c = −0.7 + 0.27i)

ENGINE PIXEL
FORMULA z**2 + c
C_VAL -0.7+0.27j
X_RANGE -1.5 1.5
Y_RANGE -1.5 1.5
RES 500
ITER 80
COLORMAP twilight_shifted
RENDER

Barnsley Fern (IFS)

ENGINE IFS
ITER 80000
RULE 0.01  0.0   0.0   0.0   0.16  0.0  0.0
RULE 0.85  0.85  0.04 -0.04  0.85  0.0  1.6
RULE 0.07  0.2  -0.26  0.23  0.22  0.0  1.6
RULE 0.07 -0.15  0.28  0.26  0.24  0.0  0.44
RENDER