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
| Name | Description |
|---|---|
| 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.csInitializes 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
m11double- The factor of x in the transformed x.
m12double- The factor of x in the transformed y.
m21double- The factor of y in the transformed x.
m22double- The factor of y in the transformed y.
offsetXdouble- The horizontal translation.
offsetYdouble- The vertical translation.
Exceptions
ArgumentException- A coefficient is not a finite number.
Properties
| Name | Description |
|---|---|
| Determinant | 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. |
| Identity | Gets the transformation that changes nothing. |
| IsIdentity | Gets a value indicating whether the transformation changes nothing. |
| M11 | Gets the factor of x in the transformed x. |
| M12 | Gets the factor of x in the transformed y. |
| M21 | Gets the factor of y in the transformed x. |
| M22 | Gets the factor of y in the transformed y. |
| OffsetX | Gets the horizontal translation. |
| OffsetY | Gets the vertical translation. |
Determinant
AffineTransform.csIdentity
AffineTransform.csGets the transformation that changes nothing.
public static AffineTransform Identity { get; }Property value
IsIdentity
AffineTransform.csOffsetX
AffineTransform.csOffsetY
AffineTransform.csMethods
| Name | Description |
|---|---|
| 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.csCreates a rotation around the origin.
public static AffineTransform CreateRotation(double degrees)Parameters
degreesdouble- The angle, in degrees. A positive angle turns clockwise on the image.
Returns
AffineTransform The transformation.
Exceptions
ArgumentExceptiondegreesis not a finite number.
CreateRotation(double, PointD)
AffineTransform.csCreates a rotation around a point, which stays where it is.
public static AffineTransform CreateRotation(double degrees, PointD center)Parameters
degreesdouble- The angle, in degrees. A positive angle turns clockwise on the image.
centerPointD- The point that does not move.
Returns
AffineTransform The transformation.
Exceptions
ArgumentExceptiondegreesor a coordinate is not a finite number.
CreateScale overloads
CreateScale(double, double)
AffineTransform.csCreates a scaling around the origin.
public static AffineTransform CreateScale(double scaleX, double scaleY)Parameters
Returns
AffineTransform The transformation.
Exceptions
ArgumentException- A factor is not a finite number.
CreateScale(double, double, PointD)
AffineTransform.csCreates a scaling around a point, which stays where it is.
public static AffineTransform CreateScale(double scaleX, double scaleY, PointD center)Parameters
scaleXdouble- The horizontal factor.
scaleYdouble- The vertical factor.
centerPointD- 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.csCreates 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
degreesXdouble- The angle the vertical lines lean by, in degrees. Must be between -90 and 90, excluded.
degreesYdouble- 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.csCreates a translation.
public static AffineTransform CreateTranslation(double dx, double dy)Parameters
Returns
AffineTransform The transformation.
Exceptions
ArgumentException- A distance is not a finite number.
Equals overloads
Equals(AffineTransform)
AffineTransform.csIndicates whether the current object is equal to another object of the same type.
public bool Equals(AffineTransform other)Parameters
otherAffineTransform- 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.csGetHashCode()
AffineTransform.csMultiply(AffineTransform, AffineTransform)
AffineTransform.csGets the transformation that applies a first one, then a second one.
public static AffineTransform Multiply(AffineTransform first, AffineTransform second)Parameters
firstAffineTransform- The transformation applied first.
secondAffineTransform- 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.csGets the transformation that applies this one first, then another one.
public AffineTransform Then(AffineTransform next)Parameters
nextAffineTransform- The transformation applied after this one.
Returns
AffineTransform The combined transformation.
Exceptions
ArgumentException- A coefficient of the result is not a finite number.
ToString()
AffineTransform.csTransform(PointD)
AffineTransform.csTransformBounds(RectangleD)
AffineTransform.csGets the smallest rectangle, aligned with the axes, that contains a transformed rectangle.
public RectangleD TransformBounds(RectangleD rectangle)Parameters
rectangleRectangleD- 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.csTryInvert(AffineTransform)
AffineTransform.csGets the transformation that undoes this one.
public bool TryInvert(out AffineTransform inverse)Parameters
inverseAffineTransform- 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
| Name | Description |
|---|---|
| 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.csCompares two transformations for equality.
public static bool operator ==(AffineTransform left, AffineTransform right)Parameters
leftAffineTransform- The first transformation.
rightAffineTransform- The second transformation.
Returns
bool true if every coefficient is equal; otherwise false.
operator !=(AffineTransform, AffineTransform)
AffineTransform.csCompares two transformations for inequality.
public static bool operator !=(AffineTransform left, AffineTransform right)Parameters
leftAffineTransform- The first transformation.
rightAffineTransform- The second transformation.
Returns
bool true if a coefficient differs; otherwise false.
operator *(AffineTransform, AffineTransform)
AffineTransform.csGets the transformation that applies a first one, then a second one.
public static AffineTransform operator *(AffineTransform left, AffineTransform right)Parameters
leftAffineTransform- The transformation applied first.
rightAffineTransform- 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.