AffineTransform Struct

A transformation of the plane that keeps straight lines straight and parallel lines parallel: a combination of translations, scalings, rotations and skews.

public readonly struct AffineTransform : IEquatable<AffineTransform>

Remarks

A point (x, y) becomes (x M11 + y M21 + OffsetX, x M12 + y M22 + OffsetY). The default value is the identity. The coordinate system is the one of PointD: Y grows downward, so a positive angle turns clockwise on the image.

Angles are in degrees. Their sine and cosine are computed with additions, multiplications and divisions only, so a transformation is the same on every platform; a rotation by a multiple of 90 degrees is exact.

Implements

Constructors

NameDescription
AffineTransform(double, double, double, double, double, double)Initializes a new instance of the AffineTransform struct from its coefficients.

AffineTransform(double, double, double, double, double, double)

AffineTransform.cs

Initializes a new instance of the AffineTransform struct from its coefficients.

public AffineTransform(double m11, double m12, double m21, double m22, double offsetX, double offsetY)

Parameters

m11 double
The factor of x in the transformed x.
m12 double
The factor of x in the transformed y.
m21 double
The factor of y in the transformed x.
m22 double
The factor of y in the transformed y.
offsetX double
The horizontal translation.
offsetY double
The vertical translation.

Exceptions

ArgumentException
A coefficient is not a finite number.

Properties

NameDescription
DeterminantGets the factor by which the transformation multiplies areas; it is negative when the transformation mirrors, and zero when it flattens the plane to a line or a point.
IdentityGets the transformation that changes nothing.
IsIdentityGets a value indicating whether the transformation changes nothing.
M11Gets the factor of x in the transformed x.
M12Gets the factor of x in the transformed y.
M21Gets the factor of y in the transformed x.
M22Gets the factor of y in the transformed y.
OffsetXGets the horizontal translation.
OffsetYGets the vertical translation.

Gets the factor by which the transformation multiplies areas; it is negative when the transformation mirrors, and zero when it flattens the plane to a line or a point.

public double Determinant { get; }

Property value

double

Gets the transformation that changes nothing.

public static AffineTransform Identity { get; }

Property value

AffineTransform

Gets a value indicating whether the transformation changes nothing.

public bool IsIdentity { get; }

Property value

bool

Gets the factor of x in the transformed x.

public double M11 { get; }

Property value

double

Gets the factor of x in the transformed y.

public double M12 { get; }

Property value

double

Gets the factor of y in the transformed x.

public double M21 { get; }

Property value

double

Gets the factor of y in the transformed y.

public double M22 { get; }

Property value

double

Gets the horizontal translation.

public double OffsetX { get; }

Property value

double

Gets the vertical translation.

public double OffsetY { get; }

Property value

double

Methods

NameDescription
CreateRotation(double)Creates a rotation around the origin.
CreateRotation(double, PointD)Creates a rotation around a point, which stays where it is.
CreateScale(double, double)Creates a scaling around the origin.
CreateScale(double, double, PointD)Creates a scaling around a point, which stays where it is.
CreateSkew(double, double)Creates a skew: a point moves horizontally in proportion to its y, and vertically in proportion to its x.
CreateTranslation(double, double)Creates a translation.
Equals(AffineTransform)Indicates whether the current object is equal to another object of the same type.
Equals(object?)Indicates whether this instance and a specified object are equal.
GetHashCode()Returns the hash code for this instance.
Multiply(AffineTransform, AffineTransform)Gets the transformation that applies a first one, then a second one.
Then(AffineTransform)Gets the transformation that applies this one first, then another one.
ToString()Returns the coefficients formatted as [M11 M12 M21 M22 OffsetX OffsetY].
Transform(PointD)Transforms a point.
TransformBounds(RectangleD)Gets the smallest rectangle, aligned with the axes, that contains a transformed rectangle.
TransformVector(PointD)Transforms a vector: the difference of two points, which a translation does not change.
TryInvert(AffineTransform)Gets the transformation that undoes this one.

CreateRotation overloads

CreateRotation(double)

AffineTransform.cs

Creates a rotation around the origin.

public static AffineTransform CreateRotation(double degrees)
Parameters
degrees double
The angle, in degrees. A positive angle turns clockwise on the image.
Returns

AffineTransform The transformation.

Exceptions
ArgumentException
degrees is not a finite number.

CreateRotation(double, PointD)

AffineTransform.cs

Creates a rotation around a point, which stays where it is.

public static AffineTransform CreateRotation(double degrees, PointD center)
Parameters
degrees double
The angle, in degrees. A positive angle turns clockwise on the image.
center PointD
The point that does not move.
Returns

AffineTransform The transformation.

Exceptions
ArgumentException
degrees or a coordinate is not a finite number.

CreateScale overloads

CreateScale(double, double)

AffineTransform.cs

Creates a scaling around the origin.

public static AffineTransform CreateScale(double scaleX, double scaleY)
Parameters
scaleX double
The horizontal factor.
scaleY double
The vertical factor.
Returns

AffineTransform The transformation.

Exceptions
ArgumentException
A factor is not a finite number.

CreateScale(double, double, PointD)

AffineTransform.cs

Creates a scaling around a point, which stays where it is.

public static AffineTransform CreateScale(double scaleX, double scaleY, PointD center)
Parameters
scaleX double
The horizontal factor.
scaleY double
The vertical factor.
center PointD
The point that does not move.
Returns

AffineTransform The transformation.

Exceptions
ArgumentException
A factor or a coordinate is not a finite number.

CreateSkew(double, double)

AffineTransform.cs

Creates a skew: a point moves horizontally in proportion to its y, and vertically in proportion to its x.

public static AffineTransform CreateSkew(double degreesX, double degreesY)

Parameters

degreesX double
The angle the vertical lines lean by, in degrees. Must be between -90 and 90, excluded.
degreesY double
The angle the horizontal lines lean by, in degrees. Must be between -90 and 90, excluded.

Returns

AffineTransform The transformation.

Exceptions

ArgumentOutOfRangeException
An angle is not between -90 and 90 degrees, excluded.

CreateTranslation(double, double)

AffineTransform.cs

Creates a translation.

public static AffineTransform CreateTranslation(double dx, double dy)

Parameters

dx double
The horizontal distance.
dy double
The vertical distance.

Returns

AffineTransform The transformation.

Exceptions

ArgumentException
A distance is not a finite number.

Equals overloads

Equals(AffineTransform)

AffineTransform.cs

Indicates whether the current object is equal to another object of the same type.

public bool Equals(AffineTransform other)
Parameters
other AffineTransform
An object to compare with this object.
Returns

bool true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

AffineTransform.cs

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)
Parameters
obj object?
The object to compare with the current instance.
Returns

bool true if obj and this instance are the same type and represent the same value; otherwise, false.

GetHashCode()

AffineTransform.cs

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int A 32-bit signed integer that is the hash code for this instance.

Multiply(AffineTransform, AffineTransform)

AffineTransform.cs

Gets the transformation that applies a first one, then a second one.

public static AffineTransform Multiply(AffineTransform first, AffineTransform second)

Parameters

first AffineTransform
The transformation applied first.
second AffineTransform
The transformation applied to the result of the first one.

Returns

AffineTransform The combined transformation.

Exceptions

ArgumentException
A coefficient of the result is not a finite number.

Then(AffineTransform)

AffineTransform.cs

Gets the transformation that applies this one first, then another one.

Parameters

next AffineTransform
The transformation applied after this one.

Returns

AffineTransform The combined transformation.

Exceptions

ArgumentException
A coefficient of the result is not a finite number.

Returns the coefficients formatted as [M11 M12 M21 M22 OffsetX OffsetY].

public override string ToString()

Returns

string A string representation of the transformation.

Transform(PointD)

AffineTransform.cs

Transforms a point.

public PointD Transform(PointD point)

Parameters

point PointD
The point.

Returns

PointD The transformed point.

TransformBounds(RectangleD)

AffineTransform.cs

Gets the smallest rectangle, aligned with the axes, that contains a transformed rectangle.

public RectangleD TransformBounds(RectangleD rectangle)

Parameters

rectangle RectangleD
The rectangle.

Returns

RectangleD The bounds of the transformed rectangle.

Exceptions

ArgumentOutOfRangeException
The size of the result is not a finite number.

Remarks

The result contains the four transformed corners: it is where a rotated, skewed or flipped rectangle lands, such as the box of a text or an image drawn in DrawingCanvas.Transform. A rotation that is not a multiple of a quarter of a turn makes it larger than the rectangle. A rectangle without width or height is a segment or a point, and gives the bounds of that.

TransformVector(PointD)

AffineTransform.cs

Transforms a vector: the difference of two points, which a translation does not change.

public PointD TransformVector(PointD vector)

Parameters

vector PointD
The vector.

Returns

PointD The transformed vector.

TryInvert(AffineTransform)

AffineTransform.cs

Gets the transformation that undoes this one.

public bool TryInvert(out AffineTransform inverse)

Parameters

inverse AffineTransform
The inverse transformation, or the identity when there is none.

Returns

bool false when the transformation flattens the plane to a line or a point: nothing undoes it.

Operators

NameDescription
operator ==(AffineTransform, AffineTransform)Compares two transformations for equality.
operator !=(AffineTransform, AffineTransform)Compares two transformations for inequality.
operator *(AffineTransform, AffineTransform)Gets the transformation that applies a first one, then a second one.

operator ==(AffineTransform, AffineTransform)

AffineTransform.cs

Compares two transformations for equality.

public static bool operator ==(AffineTransform left, AffineTransform right)

Parameters

left AffineTransform
The first transformation.
right AffineTransform
The second transformation.

Returns

bool true if every coefficient is equal; otherwise false.

operator !=(AffineTransform, AffineTransform)

AffineTransform.cs

Compares two transformations for inequality.

public static bool operator !=(AffineTransform left, AffineTransform right)

Parameters

left AffineTransform
The first transformation.
right AffineTransform
The second transformation.

Returns

bool true if a coefficient differs; otherwise false.

operator *(AffineTransform, AffineTransform)

AffineTransform.cs

Gets the transformation that applies a first one, then a second one.

public static AffineTransform operator *(AffineTransform left, AffineTransform right)

Parameters

left AffineTransform
The transformation applied first.
right AffineTransform
The transformation applied to the result of the first one.

Returns

AffineTransform The combined transformation.

Exceptions

ArgumentException
A coefficient of the result is not a finite number.