Affine¶
- class kurbopy.Affine(a, b, c, d, e, f)¶
A 2D affine transform.
- classmethod FLIP_X()¶
A transform that is flipped on the x-axis.
- classmethod FLIP_Y()¶
A transform that is flipped on the y-axis. Useful for converting between y-up and y-down spaces.
- classmethod IDENTITY()¶
The identity transform.
- as_coeffs()¶
Get the coefficients of the transform.
- determinant()¶
Compute the determinant of this transform.
- inverse()¶
Compute the inverse transform.
Produces NaN values when the determinant is zero.
- is_finite()¶
Is this value finite?
- is_nan()¶
Is this value NaN?
- classmethod map_unit_square(rect)¶
Creates an affine transformation that takes the unit square to the given rectangle.
Useful when you want to draw into the unit square but have your output fill any rectangle. In this case push the Affine onto the transform stack.
- pre_rotate(th)¶
A rotation by th followed by self.
Equivalent to self * Affine::rotate(th)
- pre_rotate_about(th, center)¶
A rotation by th about center followed by self.
Equivalent to self * Affine::rotate_about(th)
- pre_scale(scale)¶
A scale by scale followed by self.
Equivalent to self * Affine::scale(scale)
- pre_scale_non_uniform(scale_x, scale_y)¶
A scale by (scale_x, scale_y) followed by self.
Equivalent to self * Affine::scale_non_uniform(scale_x, scale_y)
- pre_translate(trans)¶
A translation of trans followed by self.
Equivalent to self * Affine::translate(trans)
- classmethod reflect(point, direction)¶
Create an affine transform that represents reflection about the line point + direction * t, t in (-infty, infty)
# Examples
` # use kurbo::{Point, Vec2, Affine}; # fn assert_near(p0: Point, p1: Point) { # assert!((p1 - p0).hypot() < 1e-9, "{p0:?} != {p1:?}"); # } let point = Point::new(1., 0.); let vec = Vec2::new(1., 1.); let map = Affine::reflect(point, vec); assert_near(map * Point::new(1., 0.), Point::new(1., 0.)); assert_near(map * Point::new(2., 1.), Point::new(2., 1.)); assert_near(map * Point::new(2., 2.), Point::new(3., 1.)); `
- classmethod rotate(th)¶
An affine transform representing rotation.
The convention for rotation is that a positive angle rotates a positive X direction into positive Y. Thus, in a Y-down coordinate system (as is common for graphics), it is a clockwise rotation, and in Y-up (traditional for math), it is anti-clockwise.
The angle, th, is expressed in radians.
- classmethod rotate_about(th, center)¶
An affine transform representing a rotation of th radians about center.
See [Affine::rotate()] for more info.
- classmethod scale(s)¶
An affine transform representing uniform scaling.
- classmethod scale_non_uniform(sx, sy)¶
An affine transform representing non-uniform scaling with different scale values for x and y
- classmethod skew(skew_x, skew_y)¶
An affine transformation representing a skew.
The skew_x and skew_y parameters represent skew factors for the horizontal and vertical directions, respectively.
This is commonly used to generate a faux oblique transform for font rendering. In this case, you can slant the glyph 20 degrees clockwise in the horizontal direction (assuming a Y-up coordinate system):
` let oblique_transform = kurbo::Affine::skew(20f64.to_radians().tan(), 0.0); `
- then_rotate(th)¶
self followed by a rotation of th.
Equivalent to Affine::rotate(th) * self
- then_rotate_about(th, center)¶
self followed by a rotation of th about center.
Equivalent to Affine::rotate_about(th, center) * self
- then_scale(scale)¶
self followed by a scale of scale.
Equivalent to Affine::scale(scale) * self
- then_scale_non_uniform(scale_x, scale_y)¶
self followed by a scale of (scale_x, scale_y).
Equivalent to Affine::scale_non_uniform(scale_x, scale_y) * self
- then_translate(trans)¶
self followed by a translation of trans.
Equivalent to Affine::translate(trans) * self
- transform_rect_bbox(rect)¶
Compute the bounding box of a transformed rectangle.
Returns the minimal Rect that encloses the given Rect after affine transformation. If the transform is axis-aligned, then this bounding box is “tight”, in other words the returned Rect is the transformed rectangle.
The returned rectangle always has non-negative width and height.
- classmethod translate(p)¶
An affine transform representing translation.
- translation()¶
Returns the translation part of this affine map ((self.0[4], self.0[5])).
- with_translation(trans)¶
Replaces the translation portion of this affine map
The translation can be seen as being applied after the linear part of the map.