Figure & Decorators¶
The figure system for collecting and rendering geometry diagrams.
@figure¶
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¶
A collection of geometry objects that can be rendered.
Methods¶
to_asy() -> str¶
Generate complete Asymptote 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.
save_pdf(path: Path) -> None¶
Render and save as PDF.
save_svg(path: Path) -> None¶
Render and save as SVG.