Universe

Create a variety of wonderful fractals in Typst.

Examples

The example below creates a dragon curve of the 12th iteration with the dragon-curve function.

The rendered dragon curve

Show code
#set page(width: auto, height: auto, margin: 0pt)

#dragon-curve(
  12,
  step-size: 6,
  stroke-style: stroke(
    paint: gradient.linear(..color.map.crest, angle: 45deg),
    thickness: 3pt,
    cap: "square"
  )
)

Features

  • Use SVG backend for image rendering.
  • Generate fractals using L-system.
  • The number of iterations, step size, fill and stroke styles, etc. of generated fractals could be customized.

Usage

Import the latest version of this package with:

#import "@preview/fractusist:0.1.0": *

Each function generates a specific fractal. The input and output arguments of all functions have a similar style. Typical input arguments are as follows:

  • n: the number of iterations (the valid range of values depends on the specific function).
  • step-size: step size (in pt).
  • fill-style: fill style, can be none or color or gradient (exists only when the curve is closed).
  • stroke-style: stroke style, can be none or color or gradient or stroke object.
  • width: the width of the image.
  • height: the height of the image.
  • fit: how the image should adjust itself to a given area, “cover” / “contain” / “stretch”.

The content returned is the image element.

For more codes with these functions see tests.

Reference

Dragon

  • dragon-curve: Generate dragon curve (n: range [0, 16]).
#let dragon-curve(n, step-size: 10, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}

Hilbert

  • hilbert-curve: Generate 2D Hilbert curve. (n: range [1, 8]).
#let hilbert-curve(n, step-size: 10, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
  • peano-curve: Generate 2D Peano curve (n: range [1, 5]).
#let peano-curve(n, step-size: 10, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}

Koch

  • koch-curve: Generate Koch curve (n: range [0, 6]).
#let koch-curve(n, step-size: 10, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
  • koch-snowflake: Generate Koch snowflake (n: range [0, 6]).
#let koch-snowflake(n, step-size: 10, fill-style: none, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}

Sierpiński

  • sierpinski-curve: Generate classic Sierpiński curve (n: range [0, 7]).
#let sierpinski-curve(n, step-size: 10, fill-style: none, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
  • sierpinski-square-curve: Generate Sierpiński square curve (n: range [0, 7]).
#let sierpinski-square-curve(n, step-size: 10, fill-style: none, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
  • sierpinski-arrowhead-curve: Generate Sierpiński arrowhead curve (n: range [0, 8]).
#let sierpinski-arrowhead-curve(n, step-size: 10, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
  • sierpinski-triangle: Generate 2D Sierpiński triangle (n: range [0, 6]).
#let sierpinski-triangle(n, step-size: 10, fill-style: none, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}