---
title: "Matrix"
description: ""
type: "class"
---

<div class='type'>Class</div>


<a id='references'></a>

<div class='sourceset sourceset-common'>Common</div>


```kotlin
value class Matrix(
    val values: FloatArray =
        floatArrayOf(1f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1f)
)
```

## Functions

```kotlin
inline operator fun get(row: Int, column: Int) = values[(row * 4) + column]
```

```kotlin
inline operator fun set(row: Int, column: Int, v: Float)
```

```kotlin
fun map(point: Offset): Offset
```


Does the 3D transform on `point` and returns the `x` and `y` values in an `Offset`.


```kotlin
fun map(rect: Rect): Rect
```


Does a 3D transform on `rect` and returns its bounds after the transform.


```kotlin
fun map(rect: MutableRect)
```


Does a 3D transform on `rect`, transforming `rect` with the results.


```kotlin
operator fun timesAssign(m: Matrix)
```


Multiply this matrix by `m` and assign the result to this matrix.


```kotlin
fun invert()
```


Invert `this` Matrix.


```kotlin
fun reset()
```


Resets the `this` to the identity matrix.


```kotlin
fun setFrom(matrix: Matrix)
```


Sets the entire matrix to the matrix in `matrix`.


```kotlin
fun rotateX(degrees: Float)
```


Applies a `degrees` rotation around X to `this`.


```kotlin
fun rotateY(degrees: Float)
```


Applies a `degrees` rotation around Y to `this`.


```kotlin
fun rotateZ(degrees: Float)
```


Applies a `degrees` rotation around Z to `this`.


```kotlin
fun scale(x: Float = 1f, y: Float = 1f, z: Float = 1f)
```


Scale this matrix by `x`, `y`, `z`


```kotlin
fun translate(x: Float = 0f, y: Float = 0f, z: Float = 0f)
```


Translate this matrix by `x`, `y`, `z`


```kotlin
fun resetToPivotedTransform(
        pivotX: Float = 0f,
        pivotY: Float = 0f,
        translationX: Float = 0f,
        translationY: Float = 0f,
        translationZ: Float = 0f,
        rotationX: Float = 0f,
        rotationY: Float = 0f,
        rotationZ: Float = 0f,
        scaleX: Float = 1f,
        scaleY: Float = 1f,
        scaleZ: Float = 1f,
    )
```


Resets this matrix to a "TRS" (translation, rotation, scale) transform around a pivot point.
The transform operations encoded in the matrix are the following, in this specific order:
- A translation by -`pivotX`, -`pivotY`
- A translation by `translationX`, `translationY`, and `translationZ`
- An X rotation by `rotationX`
- A Y rotation by `rotationY`
- A Z rotation by `rotationZ`
- A scale by `scaleX` and `scaleY`
- A translation by `pivotX`, `pivotY`

Calling this method is equivalent to the following code:
```
val m: Matrix ...
m.reset()
m.translate(-pivotX, -pivotY)
m *= Matrix().apply {   translate(translationX, translationY)   rotateX(rotationX)   rotateY(rotationY)   rotateZ(rotationZ)   scale(scaleX, scaleY)
}
m *= Matrix().apply { translate(pivotX, pivotY) }
```


## Companion Object

#### Properties

<div class='sourceset sourceset-common'>Common</div>


```kotlin
const val ScaleX = 0
```


Index of the flattened array that represents the scale factor along the X axis



<div class='sourceset sourceset-common'>Common</div>


```kotlin
const val SkewY = 1
```


Index of the flattened array that represents the skew factor along the Y axis



<div class='sourceset sourceset-common'>Common</div>


```kotlin
const val Perspective0 = 3
```


Index of the flattened array that represents the perspective factor along the X axis



<div class='sourceset sourceset-common'>Common</div>


```kotlin
const val SkewX = 4
```


Index of the flattened array that represents the skew factor along the X axis



<div class='sourceset sourceset-common'>Common</div>


```kotlin
const val ScaleY = 5
```


Index of the flattened array that represents the scale factor along the Y axis



<div class='sourceset sourceset-common'>Common</div>


```kotlin
const val Perspective1 = 7
```


Index of the flattened array that represents the perspective factor along the Y axis



<div class='sourceset sourceset-common'>Common</div>


```kotlin
const val ScaleZ = 10
```


Index of the flattened array that represents the scale factor along the Z axis



<div class='sourceset sourceset-common'>Common</div>


```kotlin
const val TranslateX = 12
```


Index of the flattened array that represents the translation along the X axis



<div class='sourceset sourceset-common'>Common</div>


```kotlin
const val TranslateY = 13
```


Index of the flattened array that represents the translation along the Y axis



<div class='sourceset sourceset-common'>Common</div>


```kotlin
const val TranslateZ = 14
```


Index of the flattened array that represents the translation along the Z axis



<div class='sourceset sourceset-common'>Common</div>


```kotlin
const val Perspective2 = 15
```


Index of the flattened array that represents the perspective factor along the Z axis





