Transform2D
Mojo struct 🡭
Transform2D
@memory_only
struct Transform2DAn affine map from continuous data-space coordinates to integer pixel-space coordinates: scale, then rotate, then translate.
pixel = rotate(data * scale, rotation) + translate
scale_y is commonly negative: pixel-space y increases downward while data-space y conventionally increases upward, so a negative scale_y with a matching translate_y flips a vertical axis.
rotation is radians, applied around the origin of the scaled data
space – before translation, not around wherever
translate_x/translate_y puts that origin in pixel space. There is no
pivot parameter; to rotate around another point, shift the data
coordinates or the translation. Defaults to 0.0.
Fields
- scale_x (
Float64) - scale_y (
Float64) - translate_x (
Float64) - translate_y (
Float64) - rotation (
Float64)
Implemented traits
AnyType, Copyable, Deinitable, ImplicitlyCopyable, Movable
Methods
__init__
fn def __init__(out self, scale_x: Float64, scale_y: Float64, translate_x: Float64, translate_y: Float64, rotation: Float64 = 0)Scale, then rotate, then translate – see the struct docstring above for the full pipeline.
Args:
- scale_x (
Float64): Horizontal scale applied before rotation. - scale_y (
Float64): Vertical scale applied before rotation. Commonly negative to flip a data-space y axis that increases upward into pixel-space y, which increases downward. - translate_x (
Float64): Horizontal offset applied after rotation. - translate_y (
Float64): Vertical offset applied after rotation. - rotation (
Float64): Radians, applied around the scaled space’s origin, before translation. - self (
Self)
Returns:
Self
to_point
fn def to_point(self, x: Float64, y: Float64) -> FPointMap a data-space point to sub-pixel canvas space.
The same pipeline to_pixel applies, without the final
rounding – what the anti-aliased primitives want, so a marker at
y = 44.3 is drawn there rather than at 44. to_pixel rounds this
result, so the two cannot drift apart.
Args:
- self (
Self) - x (
Float64): Data-space x. - y (
Float64): Data-space y.
Returns:
FPoint: The transformed point, unrounded.
to_pixel
fn def to_pixel(self, x: Float64, y: Float64) -> PointMap a data-space point to the nearest integer pixel.
Args:
- self (
Self) - x (
Float64): Data-space x. - y (
Float64): Data-space y.
Returns:
Point: The transformed point, rounded to the nearest pixel.
inverse_point
fn def inverse_point(self, px: Float64, py: Float64) -> FPointMap a pixel-space point back to the data-space point to_point would have produced it from – the exact inverse of to_point, for hit-testing or cursor readout against a chart’s own coordinate transform.
Undoes the pipeline in reverse: subtracts the translation, rotates by -rotation, then divides each axis by its own scale.
Returns a point rather than another Transform2D: this
transform’s pipeline is fixed as scale-then-rotate, and the
exact inverse of an anisotropic (scale_x != scale_y), rotated
instance is a rotate-then-scale map, which is a different
matrix in general and does not fit that same shape.
Args:
- self (
Self) - px (
Float64): Pixel-space x. - py (
Float64): Pixel-space y.
Returns:
FPoint: The data-space point.
Raises:
Error: scale_x or scale_y is zero, which collapses
that axis and cannot be undone.