AffineTransform


Known direct subclasses
ImmutableAffineTransform

An affine transformation in the plane.

MutableAffineTransform

An affine transformation in the plane.


An affine transformation in the plane. The transformation can be thought of as a 3x3 matrix:

m00  m10  m20
m01 m11 m21
0 0 1

Applying the transformation can be thought of as a matrix multiplication, with the to-be-transformed point represented as a column vector with an extra 1:

m00  m10  m20   x   m00*x + m10*y + m20
m01 m11 m21 * y = m01*x + m11*y + m21
0 0 1 1 1

Transformations are composed via multiplication. Multiplication is not commutative (i.e. AB != BA), and the left-hand transformation is composed "after" the right hand transformation. E.g., if you have:

val rotate = ImmutableAffineTransform.rotate(Angle.degreesToRadians(45))
val translate = ImmutableAffineTransform.translate(Vec(10, 0))

then rotate * translate first translates 10 units in the positive x-direction, then rotates 45° about the origin.

ImmutableAffineTransform and MutableAffineTransform are the two concrete implementations of this.

Summary

Public companion functions

Unit
multiply(
    lhs: AffineTransform,
    rhs: AffineTransform,
    output: MutableAffineTransform
)

Multiplies the lhs transform by the rhs transform as matrices, and stores the result in output.

android

Public companion properties

ImmutableAffineTransform

Constant representing an identity transformation, which maps a point to itself, i.e. it leaves it unchanged.

android

Public functions

ImmutableParallelogram

Returns an ImmutableParallelogram containing the result of applying the AffineTransform to box.

android
ImmutableParallelogram
applyTransform(parallelogram: Parallelogram)

Returns an ImmutableParallelogram containing the result of applying the AffineTransform to parallelogram.

android
ImmutableVec

Returns an ImmutableVec containing the result of applying the AffineTransform to point.

android
ImmutableSegment

Returns an ImmutableSegment containing the result of applying the AffineTransform to segment.

android
ImmutableTriangle

Returns an ImmutableTriangle containing the result of applying the AffineTransform to triangle.

android
MutableParallelogram
applyTransform(box: Box, outParallelogram: MutableParallelogram)

Apply the AffineTransform to the Box and store the result in the MutableParallelogram.

android
MutableParallelogram
applyTransform(
    parallelogram: Parallelogram,
    outParallelogram: MutableParallelogram
)

Apply the AffineTransform to the Parallelogram and store the result in the MutableParallelogram.

android
MutableVec
applyTransform(point: Vec, outVec: MutableVec)

Apply the AffineTransform to the Vec and store the result in the MutableVec.

android
MutableSegment
applyTransform(segment: Segment, outSegment: MutableSegment)

Apply the AffineTransform to the Segment and store the result in the MutableSegment.

android
MutableTriangle
applyTransform(triangle: Triangle, outTriangle: MutableTriangle)

Apply the AffineTransform to the Triangle and store the result in the MutableTriangle.

android
ImmutableAffineTransform

Returns the inverse of the AffineTransform.

android
MutableAffineTransform

Populates outAffineTransform with the inverse of this AffineTransform.

android
@Size(min = 6) FloatArray
getValues(outArray: @Size(min = 6) FloatArray)

Populates the first 6 elements of outArray with the values of this transform, starting with the top left corner of the matrix and proceeding in row-major order.

android
Boolean
isAlmostEqual(
    other: AffineTransform,
    tolerance: @FloatRange(from = 0.0) Float
)

Compares this AffineTransform with other, and returns true if each component of the transform matrix is within tolerance of the corresponding component of other.

android

Extension functions

Unit

Writes the values from this AffineTransform to out.

android
Matrix

Constructs a Matrix with the values from the AffineTransform.

android
Matrix

Writes the values from this AffineTransform to out.

android
Matrix

Constructs a Matrix with the values from the AffineTransform.

android

Public companion functions

multiply

fun multiply(
    lhs: AffineTransform,
    rhs: AffineTransform,
    output: MutableAffineTransform
): Unit

Multiplies the lhs transform by the rhs transform as matrices, and stores the result in output. Note that, when performing matrix multiplication, the lhs transform is applied after the rhs transform; i.e., after calling this method, output contains a transform equivalent to applying rhs, then lhs.

Public companion properties

IDENTITY

val IDENTITYImmutableAffineTransform

Constant representing an identity transformation, which maps a point to itself, i.e. it leaves it unchanged.

Public functions

applyTransform

fun applyTransform(box: Box): ImmutableParallelogram

Returns an ImmutableParallelogram containing the result of applying the AffineTransform to box.

Note that applying an AffineTransform to a Box results in a Parallelogram. If you need a Box, use Parallelogram.computeBoundingBox to get the minimum bounding box of the result.

Performance-sensitive code should use the applyTransform overload that takes a pre-allocated MutableParallelogram, so that instance can be reused across multiple calls.

applyTransform

fun applyTransform(parallelogram: Parallelogram): ImmutableParallelogram

Returns an ImmutableParallelogram containing the result of applying the AffineTransform to parallelogram.

Performance-sensitive code should use the applyTransform overload that takes a pre-allocated MutableParallelogram, so that instance can be reused across multiple calls.

applyTransform

fun applyTransform(point: Vec): ImmutableVec

Returns an ImmutableVec containing the result of applying the AffineTransform to point.

Note that this treats point as a location, not an offset. If you want to transform an offset, you must also transform the origin and subtract that from the result, e.g.:

val result = MutableVec()
Vec.subtract(
transform.applyTransform(vec),
transform.applyTransform(Vec.ORIGIN),
result
)

Performance-sensitive code should use the applyTransform overload that takes a pre-allocated MutableVec, so that instance can be reused across multiple calls.

applyTransform

fun applyTransform(segment: Segment): ImmutableSegment

Returns an ImmutableSegment containing the result of applying the AffineTransform to segment.

Performance-sensitive code should use the applyTransform overload that takes a pre-allocated MutableSegment, so that instance can be reused across multiple calls.

applyTransform

fun applyTransform(triangle: Triangle): ImmutableTriangle

Returns an ImmutableTriangle containing the result of applying the AffineTransform to triangle.

Performance-sensitive code should use the applyTransform overload that takes a pre-allocated MutableTriangle, so that instance can be reused across multiple calls.

applyTransform

fun applyTransform(box: Box, outParallelogram: MutableParallelogram): MutableParallelogram

Apply the AffineTransform to the Box and store the result in the MutableParallelogram. This is the only Apply function where the input cannot also be the output, as applying an Affine Transform to a Box makes a Parallelogram.

applyTransform

fun applyTransform(
    parallelogram: Parallelogram,
    outParallelogram: MutableParallelogram
): MutableParallelogram

Apply the AffineTransform to the Parallelogram and store the result in the MutableParallelogram. The same MutableParallelogram can be used as both the input and output to avoid additional allocations.

applyTransform

fun applyTransform(point: Vec, outVec: MutableVec): MutableVec

Apply the AffineTransform to the Vec and store the result in the MutableVec. The same MutableVec can be used as both the input and output to avoid additional allocations. Returns outVec.

Note that this treats point as a location, not an offset. If you want to transform an offset, you must also transform the origin and subtract that from the result, e.g.:

Vec.subtract(
transform.applyTransform(vec, scratchPoint1),
transform.applyTransform(Vec.ORIGIN, scratchPoint2),
result
)

applyTransform

fun applyTransform(segment: Segment, outSegment: MutableSegment): MutableSegment

Apply the AffineTransform to the Segment and store the result in the MutableSegment. The same MutableSegment can be used as both the input and output to avoid additional allocations. Returns outSegment.

applyTransform

fun applyTransform(triangle: Triangle, outTriangle: MutableTriangle): MutableTriangle

Apply the AffineTransform to the Triangle and store the result in the MutableTriangle. The same MutableTriangle can be used as both the input and output to avoid additional allocations. Returns outTriangle.

computeInverse

fun computeInverse(): ImmutableAffineTransform

Returns the inverse of the AffineTransform.

Performance-sensitive code should use the computeInverse overload that takes a pre-allocated MutableAffineTransform, so that instance can be reused across multiple calls.

Throws
kotlin.IllegalArgumentException

if the AffineTransform cannot be inverted.

computeInverse

fun computeInverse(outAffineTransform: MutableAffineTransform): MutableAffineTransform

Populates outAffineTransform with the inverse of this AffineTransform. The same MutableAffineTransform instance can be used as the output to avoid additional allocations. Returns outAffineTransform.

Throws
kotlin.IllegalArgumentException

if the AffineTransform cannot be inverted. .

getValues

fun getValues(outArray: @Size(min = 6) FloatArray = FloatArray(6)): @Size(min = 6) FloatArray

Populates the first 6 elements of outArray with the values of this transform, starting with the top left corner of the matrix and proceeding in row-major order.

In performance-sensitive code, prefer to pass in an array that has already been allocated and is being reused, rather than relying on the default behavior of allocating a new instance for each call.

Prefer to apply this transform to an object, such as with applyTransform, rather than accessing the actual numeric values of this transform. This function is useful for when the values are needed in bulk but not to apply a transform, for example for serialization.

To set these values on a transform in the same order that they are retrieved here, use the ImmutableAffineTransform constructor or use MutableAffineTransform.setValues.

isAlmostEqual

fun isAlmostEqual(
    other: AffineTransform,
    tolerance: @FloatRange(from = 0.0) Float
): Boolean

Compares this AffineTransform with other, and returns true if each component of the transform matrix is within tolerance of the corresponding component of other.

Extension functions

populateMatrix

fun AffineTransform.populateMatrix(out: Matrix): Unit

Writes the values from this AffineTransform to out.

Returns the modified Matrix to allow chaining calls.

Returns
Unit

out

toMatrix

fun AffineTransform.toMatrix(): Matrix

Constructs a Matrix with the values from the AffineTransform.

Performance-sensitive code should use the populateMatrix overload that takes a pre-allocated Matrix, so that the instance can be reused across multiple calls.

populateMatrix

fun AffineTransform.populateMatrix(out: Matrix): Matrix

Writes the values from this AffineTransform to out. Note the conversion from a 3x3 AffineTransform to a 4x4 column-major android.compose.ui.graphics.Matrix.

Returns the modified Matrix instance to allow chaining calls.

Returns
Matrix

out

toMatrix

fun AffineTransform.toMatrix(): Matrix

Constructs a Matrix with the values from the AffineTransform.

Performance-sensitive code should still use this because Matrix is an inline value class.