Skip to content

Figure & Decorators

The figure system for collecting and rendering geometry diagrams.


@figure

@figure
def my_diagram():
    # ... create geometry objects ...
    return [list of primitives]

Collect geometry objects from a function's return value into a Figure.

Simple decorator that requires explicit return of primitives.

Example

from geolet import figure, point, segment

@figure
def my_diagram():
    A = point(0, 0, "A", label_dir="SW")
    B = point(4, 0, "B", label_dir="SE")
    C = point(2, 3, "C", label_dir="N")

    return [
        A, B, C,
        segment(A, B),
        segment(B, C),
        segment(C, A),
    ]

# Save as various formats
my_diagram.save_svg(Path("output.svg"))
my_diagram.save_pdf(Path("output.pdf"))
my_diagram.save_asy(Path("output.asy"))

@autofigure

@autofigure
def my_diagram():
    # ... create geometry objects ...
    # No return needed - all Drawable locals collected

Collect geometry objects from a function into a Figure.

Uses frame introspection to auto-collect all primitive locals if no return. Variables with underscore prefix are excluded from auto-collection.

Example

from geolet import autofigure, point, segment

@autofigure
def my_diagram():
    A = point(0, 0, "A", label_dir="SW")
    B = point(4, 0, "B", label_dir="SE")
    C = point(2, 3, "C", label_dir="N")

    segment(A, B)
    segment(B, C)
    segment(C, A)
    # No return needed - all Drawable locals collected

# Save as various formats
my_diagram.save_svg(Path("output.svg"))

Excluding Variables

Variables prefixed with underscore are excluded from auto-collection:

@autofigure
def my_diagram():
    A = point(0, 0, "A")
    B = point(4, 0, "B")

    # This helper point won't be drawn
    _midpoint = point(2, 0, "")

    segment(A, B)

Decorators Comparison

Feature @figure @autofigure
Return statement Required Optional
Auto-collection No Yes
Underscore exclusion N/A Yes (_var excluded)
Use case Explicit control Quick prototyping

Figure class

class Figure:
    primitives: Sequence[Point | Segment | Line | Circle | Angle]

A collection of geometry objects that can be rendered.

Methods

to_asy() -> str

Generate complete Asymptote code.

asy_code = my_diagram.to_asy()
print(asy_code)

render(fmt: Literal["svg", "pdf"]) -> str | bytes

Render to format in-memory.

# Get SVG as string
svg_content = my_diagram.render("svg")

# Get PDF as bytes
pdf_bytes = my_diagram.render("pdf")

save_asy(path: Path) -> None

Write Asymptote code to file.

my_diagram.save_asy(Path("output.asy"))

save_pdf(path: Path) -> None

Render and save as PDF.

my_diagram.save_pdf(Path("output.pdf"))

save_svg(path: Path) -> None

Render and save as SVG.

my_diagram.save_svg(Path("output.svg"))