AffineTransform

public struct AffineTransform : Hashable

An affine transformation matrix is used to rotate, scale, translate, or skew the objects you draw in a graphics context. The AffineTransform type provides functions for creating, concatenating, and applying affine transformations.

Affine transforms are represented by a 3 by 3 matrix:

/ a  b  0 \
| c  d  0 |
\ tx ty 1 /

Because the third column is always (0,0,1), the AffineTransform structure contains values for only the first two columns.

Conceptually, an affine transform multiplies a row vector representing each point (x,y) in your drawing by this matrix, producing a vector that represents the corresponding point (x’,y’):

                      / a  b  0 \
(x y 1) = (x y 1) × | c  d  0 |
                      \ tx ty 1 /

Given the 3 by 3 matrix, the following equations are used to transform a point (x, y) in one coordinate system into a resultant point (x’,y’) in another coordinate system.

x = a * x + c * y + tx
y = b * x + d * y + ty

The matrix thereby “links” two coordinate systems — it specifies how points in one coordinate system map to points in another.

Note that you do not typically need to create affine transforms directly. If you want only to draw an object that is scaled or rotated, for example, it is not necessary to construct an affine transform to do so. The most direct way to manipulate your drawing — whether by movement, scaling, or rotation — is to call the functions translateBy(x:y:), scaleBy(x:y:), or rotate(by:), respectively.

  • a

    The entry at position [1,1] in the matrix.

    Declaration

    Swift

    public var a: Float
  • b

    The entry at position [1,2] in the matrix.

    Declaration

    Swift

    public var b: Float
  • c

    The entry at position [2,1] in the matrix.

    Declaration

    Swift

    public var c: Float
  • d

    The entry at position [2,2] in the matrix.

    Declaration

    Swift

    public var d: Float
  • tx

    The entry at position [3,1] in the matrix.

    Declaration

    Swift

    public var tx: Float
  • ty

    The entry at position [3,2] in the matrix.

    Declaration

    Swift

    public var ty: Float
  • Creates a new affine transformation using the matrix with provided values.

    Declaration

    Swift

    public init(a:  Float, b:  Float,
                c:  Float, d:  Float,
                tx: Float, ty: Float)

    Parameters

    a

    The entry at position [1,1] in the matrix.

    b

    The entry at position [1,2] in the matrix.

    c

    The entry at position [2,1] in the matrix.

    d

    The entry at position [2,2] in the matrix.

    tx

    The entry at position [3,1] in the matrix.

    ty

    The entry at position [3,2] in the matrix.

  • Creates an affine transformation matrix constructed from a rotation value you provide.

    This function creates a AffineTransform structure, which you can use (and reuse, if you want) to rotate a coordinate system. The matrix takes the following form:

    / cos(angle)  sin(angle)  0 \
    | -sin(angle) cos(angle)  0 |
    \ 0           0           1 /
    

    These are the resulting equations used to apply the rotation to a point (x, y):

    x = x * cos(angle) - y * sin(angle)
    y = x * sin(angle) + y * sin(angle)
    

    Declaration

    Swift

    @inlinable
    public init(rotationAngle angle: Float)

    Parameters

    angle

    The angle, in radians, by which this matrix rotates the coordinate system axes.

  • Creates an affine transformation matrix constructed from scaling values you provide.

    This function creates a AffineTransform structure, which you can use (and reuse, if you want) to scale a coordinate system. The matrix takes the following form:

    / sx 0  0 \
    | 0  sy 0 |
    \ 0  0  1 /
    

    These are the resulting equations used to scale the coordinates of a point (x,y):

    x = x * sx
    y = y * sy
    

    Declaration

    Swift

    @inlinable
    public init(scaleX sx: Float, y sy: Float)

    Parameters

    sx

    The factor by which to scale the x-axis of the coordinate system.

    sy

    The factor by which to scale the y-axis of the coordinate system.

  • Returns an affine transformation matrix constructed from translation values you provide.

    This function creates a AffineTransform structure, which you can use (and reuse, if you want) to move a coordinate system. The matrix takes the following form:

    / 1  0  0 \
    | 0  1  0 |
    \ tx ty 1 /
    

    These are the resulting equations used to apply the translation to a point (x,y):

    x = x + tx
    y = y + ty
    

    Declaration

    Swift

    @inlinable
    public init(translationX tx: Float, y ty: Float)

    Parameters

    tx

    The value by which to move the x-axis of the coordinate system.

    ty

    The value by which to move the y-axis of the coordinate system.

  • Checks whether an affine transform is the identity transform.

    Declaration

    Swift

    @inlinable
    public var isIdentity: Bool { get }
  • The identity transform:

    / 1 0 0 \
    | 0 1 0 |
    \ 0 0 1 /
    

    Declaration

    Swift

    public static let identity: AffineTransform
  • Returns an affine transformation matrix constructed by combining two existing affine transforms.

    Concatenation combines two affine transformation matrices by multiplying them together. You might perform several concatenations in order to create a single affine transform that contains the cumulative effects of several transformations.

    Note that matrix operations are not commutative—the order in which you concatenate matrices is important. That is, the result of multiplying matrix t1 by matrix t2 does not necessarily equal the result of multiplying matrix t2 by matrix t1.

    Declaration

    Swift

    @inlinable
    public func concatenating(_ other: AffineTransform) -> AffineTransform

    Parameters

    other

    The affine transform to concatenate to this affine transform.

    Return Value

    A new affine transformation matrix. That is, t’ = self * other.

  • Returns an affine transformation matrix constructed by inverting an existing affine transform.

    Inversion is generally used to provide reverse transformation of points within transformed objects. Given the coordinates (x,y), which have been transformed by a given matrix to new coordinates (x’,y’), transforming the coordinates (x’,y’) by the inverse matrix produces the original coordinates (x,y).

    Declaration

    Swift

    @inlinable
    public func inverted() -> AffineTransform

    Return Value

    A new affine transformation matrix. If the affine transform cannot be inverted, it is returned unchanged.

  • Returns an affine transformation matrix constructed by rotating self.

    You use this function to create a new affine transformation matrix by adding a rotation value to an existing affine transform. The resulting structure represents a new affine transform, which you can use (and reuse, if you want) to rotate a coordinate system.

    Declaration

    Swift

    @inlinable
    public func rotated(byAngle angle: Float) -> AffineTransform

    Parameters

    angle

    The angle, in radians, by which to rotate the affine transform.

    Return Value

    A new affine transformation matrix.

  • Returns an affine transformation matrix constructed by scaling self.

    You use this function to create a new affine transformation matrix by adding scaling values to an existing affine transform. The resulting structure represents a new affine transform, which you can use (and reuse, if you want) to scale a coordinate system.

    Declaration

    Swift

    @inlinable
    public func scaled(byX x: Float, y: Float) -> AffineTransform

    Parameters

    x

    The value by which to scale x values of the affine transform.

    y

    The value by which to scale y values of the affine transform.

    Return Value

    A new affine transformation matrix.

  • Returns an affine transformation matrix constructed by translating an existing affine transform.

    You use this function to create a new affine transform by adding translation values to an existing affine transform. The resulting structure represents a new affine transform, which you can use (and reuse, if you want) to move a coordinate system.

    Declaration

    Swift

    @inlinable
    public func translated(byX tx: Float, y ty: Float) -> AffineTransform

    Parameters

    tx

    The value by which to move x values with the affine transform.

    ty

    The value by which to move y values with the affine transform.

    Return Value

    A new affine transformation matrix.

  • Multiplies two matrices.

    This is analogous to calling lhs.concatenating(rhs).

    Note that matrix operations are not commutative—the order in which you concatenate matrices is important. That is, the result of multiplying matrix lhs by matrix rhs does not necessarily equal the result of multiplying matrix rhs by matrix lhs.

    Declaration

    Swift

    @inlinable
    public static func * (lhs: AffineTransform, rhs: AffineTransform) -> AffineTransform

    Parameters

    lhs

    The left operand.

    rhs

    The right operand.

    Return Value

    A new affine transformation matrix.

  • Declaration

    Swift

    public var debugDescription: String { get }
  • Declaration

    Swift

    public var description: String { get }