Skip to content
Edit this page

ictdesigntools.draw.transform

Affine transforms for drawings, in the SVG convention: x to the right, y down, and a point mapped as (a*x + c*y + e, b*x + d*y + f).

Transform dataclass

Transform(
    a: float = 1.0,
    b: float = 0.0,
    c: float = 0.0,
    d: float = 1.0,
    e: float = 0.0,
    f: float = 0.0,
)

scale_factor property

scale_factor: float

How much lengths grow, for a similarity (uniform scale with rotation or mirror).

is_similarity property

is_similarity: bool

True when circles stay circles: no shear or uneven scale.

rotation property

rotation: float

The page rotation of the x axis, in degrees counter-clockwise as seen on the page.

rotate staticmethod

rotate(
    degrees: float, cx: float = 0.0, cy: float = 0.0
) -> Transform

Counter-clockwise as seen on the page (y down), about (cx, cy).

Source code in src/ictdesigntools/draw/transform.py
@staticmethod
def rotate(degrees: float, cx: float = 0.0, cy: float = 0.0) -> Transform:
    """Counter-clockwise as seen on the page (y down), about (cx, cy)."""
    t = math.radians(-degrees)
    cos, sin = math.cos(t), math.sin(t)
    r = Transform(cos, sin, -sin, cos)
    if cx or cy:
        return Transform.translate(cx, cy) @ r @ Transform.translate(-cx, -cy)
    return r

mirror_x staticmethod

mirror_x() -> Transform

Mirror left to right (about the y axis).

Source code in src/ictdesigntools/draw/transform.py
@staticmethod
def mirror_x() -> Transform:
    """Mirror left to right (about the y axis)."""
    return Transform(-1.0, 0.0, 0.0, 1.0)

then

then(other: Transform) -> Transform

This transform followed by other: Transform.rotate(90).then(translate(5)).

Source code in src/ictdesigntools/draw/transform.py
def then(self, other: Transform) -> Transform:
    """This transform followed by ``other``: ``Transform.rotate(90).then(translate(5))``."""
    return other @ self

vector

vector(x: float, y: float) -> tuple[float, float]

A direction, without the translation.

Source code in src/ictdesigntools/draw/transform.py
def vector(self, x: float, y: float) -> tuple[float, float]:
    """A direction, without the translation."""
    return self.a * x + self.c * y, self.b * x + self.d * y

svg

svg() -> str

The SVG transform attribute value.

Source code in src/ictdesigntools/draw/transform.py
def svg(self) -> str:
    """The SVG ``transform`` attribute value."""
    if self.a == 1 and self.b == 0 and self.c == 0 and self.d == 1:
        return f"translate({_f(self.e)} {_f(self.f)})"
    return "matrix(" + " ".join(_f(v) for v in (self.a, self.b, self.c, self.d, self.e,
                                                 self.f)) + ")"  # fmt: skip