Skip to content
Edit this page

ictdesigntools.draw.layout

Placing things: grids, stacks, rack units and outlines around groups of circles.

grid

grid(
    count: int,
    columns: int,
    pitch_x: float,
    pitch_y: float,
    x: float = 0.0,
    y: float = 0.0,
) -> list[tuple[float, float]]

count positions filling rows of columns left to right, then top to bottom.

Source code in src/ictdesigntools/draw/layout.py
def grid(count: int, columns: int, pitch_x: float, pitch_y: float, x: float = 0.0,
         y: float = 0.0) -> list[tuple[float, float]]:  # fmt: skip
    """``count`` positions filling rows of ``columns`` left to right, then top to bottom."""
    if columns < 1:
        raise ValueError("columns must be at least 1")
    return [(x + (i % columns) * pitch_x, y + (i // columns) * pitch_y) for i in range(count)]

stack

stack(
    sizes: list[float], gap: float = 0.0, start: float = 0.0
) -> list[float]

Where each of a run of sizes starts when laid end to end with gap between them.

Source code in src/ictdesigntools/draw/layout.py
def stack(sizes: list[float], gap: float = 0.0, start: float = 0.0) -> list[float]:
    """Where each of a run of sizes starts when laid end to end with ``gap`` between them."""
    out, at = [], start
    for s in sizes:
        out.append(at)
        at += s + gap
    return out

centred

centred(
    sizes: list[float],
    gap: float,
    span: float,
    start: float = 0.0,
) -> list[float]

Like stack, with the run centred in span.

Source code in src/ictdesigntools/draw/layout.py
def centred(sizes: list[float], gap: float, span: float, start: float = 0.0) -> list[float]:
    """Like ``stack``, with the run centred in ``span``."""
    used = sum(sizes) + gap * max(len(sizes) - 1, 0)
    return stack(sizes, gap, start + (span - used) / 2)

rack_y

rack_y(
    u: float, height_u: float, rack_u: int, top: float = 0.0
) -> float

The top edge of equipment height_u high whose lowest unit is u (numbered 1 from the bottom, as on the rails) in a rack rack_u high drawn from top down.

Source code in src/ictdesigntools/draw/layout.py
def rack_y(u: float, height_u: float, rack_u: int, top: float = 0.0) -> float:
    """The top edge of equipment ``height_u`` high whose lowest unit is ``u`` (numbered 1 from
    the bottom, as on the rails) in a rack ``rack_u`` high drawn from ``top`` down."""
    if not (1 <= u and u + height_u - 1 <= rack_u):
        raise ValueError(f"U{u:g} to U{u + height_u - 1:g} is outside a {rack_u}U rack.")
    return top + (rack_u - (u + height_u - 1)) * RACK_UNIT_MM

convex_hull

convex_hull(points) -> tuple[tuple[float, float], ...]

The convex outline around points, anticlockwise (Andrew's monotone chain).

Source code in src/ictdesigntools/draw/layout.py
def convex_hull(points) -> tuple[tuple[float, float], ...]:
    """The convex outline around points, anticlockwise (Andrew's monotone chain)."""
    pts = sorted(set(points))
    if len(pts) < 3:
        return tuple(pts)

    def cross(o, a, b):
        return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])

    lower, upper = [], []
    for p in pts:
        while len(lower) >= 2 and cross(lower[-2], lower[-1], p) <= 0:
            lower.pop()
        lower.append(p)
    for p in reversed(pts):
        while len(upper) >= 2 and cross(upper[-2], upper[-1], p) <= 0:
            upper.pop()
        upper.append(p)
    return tuple(lower[:-1] + upper[:-1])

circle_hull

circle_hull(
    circles, steps: int = 72
) -> tuple[tuple[float, float], ...]

The convex outline around circles (cx, cy, r): how a sheath shrinks onto what it holds, whatever their sizes.

Source code in src/ictdesigntools/draw/layout.py
def circle_hull(circles, steps: int = 72) -> tuple[tuple[float, float], ...]:
    """The convex outline around circles (cx, cy, r): how a sheath shrinks onto what it
    holds, whatever their sizes."""
    return convex_hull(
        (round(cx + r * math.cos(a), 9), round(cy + r * math.sin(a), 9))
        for cx, cy, r in circles
        for a in (2 * math.pi * i / steps for i in range(steps))
    )