chalk-diagrams / chalk

A declarative drawing API in Python
MIT License
276 stars 13 forks source link

Sierpinski example is incompete #135

Closed pawamoy closed 3 months ago

pawamoy commented 3 months ago

The sierpinski example is incomplete. beside takes a "direction", but I'm not sure what this is. The docs mention "unit_x" but this doesn't really help: https://chalk-diagrams.github.io/api/combinators/?h=beside#beside.

from colour import Color
from chalk import Diagram, triangle
from planar import Vec2

papaya = Color("#ff9700")

def sierpinski(n: int, size: int) -> Diagram:
    if n <= 1:
        return triangle(size)
    else:
        smaller = sierpinski(n - 1, size / 2)
        return smaller.above(smaller.beside(smaller).center_xy())

d = sierpinski(5, 4).fill_color(papaya)

...gives TypeError: beside() missing 1 required positional argument: 'direction'.

Any hint :relaxed:?

danoneata commented 3 months ago

Hey Timothée! Thanks, I've updated the README. But for me it works using unit_x as the second argument to beside:

-       return smaller.above(smaller.beside(smaller).center_xy())
+       return smaller.above(smaller.beside(smaller, unit_x).center_xy())

This second argument to beside specifies the direction on which to place the two objects next to each other. If direction is unit_x(that is, the vector (1, 0)) this means: place the second diagram horizontally next to the first one. The horizontal placement can also be achieved with the | (__or__) operator:

        return smaller.above((smaller | smaller).center_xy())

I hope this is clearer.

pawamoy commented 3 months ago

Ah, right, I suppose unit_x is imported from chalk thanks to the wildcard import. That's what I was missing, thanks :)

And thanks for the update to the readme!