Skip to main content

Affine

Struct Affine 

pub struct Affine(/* private fields */);
Expand description

A 2D affine transform.

Implementations§

§

impl Affine

pub const IDENTITY: Affine

The identity transform.

pub const FLIP_Y: Affine

A transform that is flipped on the y-axis. Useful for converting between y-up and y-down spaces.

pub const FLIP_X: Affine

A transform that is flipped on the x-axis.

pub const fn new(c: [f64; 6]) -> Affine

Construct an affine transform from coefficients.

If the coefficients are (a, b, c, d, e, f), then the resulting transformation represents this augmented matrix:

| a c e |
| b d f |
| 0 0 1 |

Note that this convention is transposed from PostScript and Direct2D, but is consistent with the Wikipedia formulation of affine transformation as augmented matrix. The idea is that (A * B) * v == A * (B * v), where * is the Mul trait.

pub const fn scale(s: f64) -> Affine

An affine transform representing uniform scaling.

pub const fn scale_non_uniform(s_x: f64, s_y: f64) -> Affine

An affine transform representing non-uniform scaling with different scale values for x and y

pub fn scale_about(s: f64, center: impl Into<Point>) -> Affine

An affine transform representing a scale of scale about center.

Useful for a view transform that zooms at a specific point, while keeping that point fixed in the result space.

See Affine::scale() for more info.

pub fn rotate(th: f64) -> Affine

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.

pub fn rotate_about(th: f64, center: impl Into<Point>) -> Affine

An affine transform representing a rotation of th radians about center.

See Affine::rotate() for more info.

pub fn translate<V>(p: V) -> Affine
where V: Into<Vec2>,

An affine transform representing translation.

pub const fn skew(skew_x: f64, skew_y: f64) -> Affine

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);

pub fn reflect(point: impl Into<Point>, direction: impl Into<Vec2>) -> Affine

Create an affine transform that represents reflection about the line point + direction * t, t in (-infty, infty)

§Examples
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.));

pub fn pre_rotate(self, th: f64) -> Affine

A rotation by th followed by self.

Equivalent to self * Affine::rotate(th)

pub fn pre_rotate_about(self, th: f64, center: impl Into<Point>) -> Affine

A rotation by th about center followed by self.

Equivalent to self * Affine::rotate_about(th, center)

pub fn pre_scale(self, scale: f64) -> Affine

A scale by scale followed by self.

Equivalent to self * Affine::scale(scale)

pub fn pre_scale_non_uniform(self, scale_x: f64, scale_y: f64) -> Affine

A scale by (scale_x, scale_y) followed by self.

Equivalent to self * Affine::scale_non_uniform(scale_x, scale_y)

pub fn pre_translate(self, trans: Vec2) -> Affine

A translation of trans followed by self.

Equivalent to self * Affine::translate(trans)

pub fn pre_skew(self, skew_x: f64, skew_y: f64) -> Affine

A skew of (skew_x, skew_y) followed by self.

Equivalent to self * Affine::skew(skew_x, skew_y)

pub fn pre_reflect( self, point: impl Into<Point>, direction: impl Into<Vec2>, ) -> Affine

A reflection about the line through point in direction followed by self.

Equivalent to self * Affine::reflect(point, direction)

pub fn then_rotate(self, th: f64) -> Affine

self followed by a rotation of th.

Equivalent to Affine::rotate(th) * self

pub fn then_rotate_about(self, th: f64, center: impl Into<Point>) -> Affine

self followed by a rotation of th about center.

Equivalent to Affine::rotate_about(th, center) * self

pub fn then_scale(self, scale: f64) -> Affine

self followed by a scale of scale.

Equivalent to Affine::scale(scale) * self

pub fn then_scale_non_uniform(self, scale_x: f64, scale_y: f64) -> Affine

self followed by a scale of (scale_x, scale_y).

Equivalent to Affine::scale_non_uniform(scale_x, scale_y) * self

pub fn then_scale_about(self, scale: f64, center: impl Into<Point>) -> Affine

self followed by a scale of scale about center.

Equivalent to Affine::scale_about(scale) * self

pub fn then_skew(self, skew_x: f64, skew_y: f64) -> Affine

self followed by a skew of (skew_x, skew_y).

Equivalent to Affine::skew(skew_x, skew_y) * self

pub fn then_reflect( self, point: impl Into<Point>, direction: impl Into<Vec2>, ) -> Affine

self followed by a reflection about the line through point in direction.

Equivalent to Affine::reflect(point, direction) * self

pub const fn then_translate(self, trans: Vec2) -> Affine

self followed by a translation of trans.

Equivalent to Affine::translate(trans) * self

pub const fn map_unit_square(rect: Rect) -> Affine

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.

pub const fn as_coeffs(self) -> [f64; 6]

Get the coefficients of the transform.

pub const fn determinant(self) -> f64

Compute the determinant of this transform.

§Geometric interpretation

Consider a region transformed by this affine. The transformed region’s area is the area of the original region scaled by the absolute value of the determinant. A negative determinant indicates orientation reversal.

pub const fn nuclear_norm_squared(self) -> f64

Compute the square of the nuclear norm of this transform.

This is the square of the Schatten p-norm with p=1, also known as the “trace norm.”

Returns the squared norm for efficiency; take the square root as necessary.

§Geometric interpretation

Consider a unit circle transformed by this affine. The nuclear norm is the sum of the resulting ellipse’s radii (semi axes). That sum multiplied by π is a first-order approximation of the ellipse’s perimeter.

pub const fn frobenius_norm_squared(self) -> f64

Compute the square of the Frobenius norm of this transform.

This is the square of the Schatten p-norm with p=2.

Returns the squared norm for efficiency; take the square root as necessary.

§Geometric interpretation

Consider a unit circle transformed by this affine. The squared Frobenius norm is twice the mean squared radius of the resulting ellipse. Alternatively, it is equal to the squared distance from the ellipse’s center to a corner of the rectangle spanned by the ellipse’s axes.

pub fn spectral_norm(self) -> f64

Compute the spectral norm of this transform.

This is the Schatten p-norm with p=∞.

§Geometric interpretation

Consider a unit circle transformed by this affine. The spectral norm is the major radius (semi-major axis) of the Ellipse.

pub const fn inverse(self) -> Affine

Compute the inverse transform.

Produces NaN values when the determinant is zero.

pub fn transform_rect_bbox(self, rect: Rect) -> 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.

pub const fn is_finite(&self) -> bool

Is this map finite?

pub const fn is_nan(&self) -> bool

Is this map NaN?

pub const fn translation(self) -> Vec2

Returns the translation part of this affine map ((self.0[4], self.0[5])).

pub const fn with_translation(self, trans: Vec2) -> Affine

Replaces the translation portion of this affine map

The translation can be seen as being applied after the linear part of the map.

Trait Implementations§

§

impl Clone for Affine

§

fn clone(&self) -> Affine

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for Affine

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Default for Affine

§

fn default() -> Affine

Returns the “default value” for a type. Read more
§

impl From<TranslateScale> for Affine

§

fn from(ts: TranslateScale) -> Affine

Converts to this type from the input type.
§

impl Mul<&BezPath> for Affine

§

type Output = BezPath

The resulting type after applying the * operator.
§

fn mul(self, other: &BezPath) -> BezPath

Performs the * operation. Read more
§

impl Mul<Arc> for Affine

§

type Output = Arc

The resulting type after applying the * operator.
§

fn mul(self, arc: Arc) -> <Affine as Mul<Arc>>::Output

Performs the * operation. Read more
§

impl Mul<BezPath> for Affine

§

type Output = BezPath

The resulting type after applying the * operator.
§

fn mul(self, other: BezPath) -> BezPath

Performs the * operation. Read more
§

impl Mul<Circle> for Affine

§

type Output = Ellipse

The resulting type after applying the * operator.
§

fn mul(self, other: Circle) -> <Affine as Mul<Circle>>::Output

Performs the * operation. Read more
§

impl Mul<CubicBez> for Affine

§

type Output = CubicBez

The resulting type after applying the * operator.
§

fn mul(self, c: CubicBez) -> CubicBez

Performs the * operation. Read more
§

impl Mul<Ellipse> for Affine

§

type Output = Ellipse

The resulting type after applying the * operator.
§

fn mul(self, other: Ellipse) -> <Affine as Mul<Ellipse>>::Output

Performs the * operation. Read more
§

impl Mul<Line> for Affine

§

type Output = Line

The resulting type after applying the * operator.
§

fn mul(self, other: Line) -> Line

Performs the * operation. Read more
§

impl Mul<PathEl> for Affine

§

type Output = PathEl

The resulting type after applying the * operator.
§

fn mul(self, other: PathEl) -> PathEl

Performs the * operation. Read more
§

impl Mul<PathSeg> for Affine

§

type Output = PathSeg

The resulting type after applying the * operator.
§

fn mul(self, other: PathSeg) -> PathSeg

Performs the * operation. Read more
§

impl Mul<Point> for Affine

§

type Output = Point

The resulting type after applying the * operator.
§

fn mul(self, other: Point) -> Point

Performs the * operation. Read more
§

impl Mul<QuadBez> for Affine

§

type Output = QuadBez

The resulting type after applying the * operator.
§

fn mul(self, other: QuadBez) -> QuadBez

Performs the * operation. Read more
§

impl Mul for Affine

§

type Output = Affine

The resulting type after applying the * operator.
§

fn mul(self, other: Affine) -> Affine

Performs the * operation. Read more
§

impl MulAssign for Affine

§

fn mul_assign(&mut self, other: Affine)

Performs the *= operation. Read more
§

impl PartialEq for Affine

§

fn eq(&self, other: &Affine) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl Copy for Affine

§

impl StructuralPartialEq for Affine

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> AnyEq for T
where T: Any + PartialEq,

§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> NoneValue for T
where T: Default,

§

type NoneType = T

§

fn null_value() -> T

The none-equivalent value.
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(_simd: S, value: T) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> Fruit for T
where T: Send + Downcast,

§

impl<T> MaybeSendSync for T
where T: Send + Sync + ?Sized,

§

impl<T, Rhs, Output> ScalarMul<Rhs, Output> for T
where T: Mul<Rhs, Output = Output> + MulAssign<Rhs>,

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T

§

impl<T> WasmNotSync for T
where T: Sync,