# Morph

Artifact: [androidx.graphics:graphics-shapes](https://developer.android.com/jetpack/androidx/releases/graphics) [View Source](https://cs.android.com/search?q=file:androidx/graphics/shapes/Morph.kt+class:androidx.graphics.shapes.Morph) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

*** ** * ** ***

Common/AllAndroid/JVM
- Cmn

  ```
  class Morph
  ```

*** ** * ** ***

This class is used to animate between start and end polygons objects.

Morphing between arbitrary objects can be problematic because it can be difficult to determine how the points of a given shape map to the points of some other shape. `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph` simplifies the problem by only operating on `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/RoundedPolygon` objects, which are known to have similar, contiguous structures. For one thing, the shape of a polygon is contiguous from start to end (compared to an arbitrary Path object, which could have one or more `moveTo` operations in the shape). Also, all edges of a polygon shape are represented by `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Cubic` objects, thus the start and end shapes use similar operations. Two Polygon shapes then only differ in the quantity and placement of their curves. The morph works by determining how to map the curves of the two shapes together (based on proximity and other information, such as distance to polygon vertices and concavity), and splitting curves when the shapes do not have the same number of curves or when the curve placement within the shapes is very different.

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#Morph(androidx.graphics.shapes.RoundedPolygon,androidx.graphics.shapes.RoundedPolygon)(start: https://developer.android.com/reference/kotlin/androidx/graphics/shapes/RoundedPolygon, end: https://developer.android.com/reference/kotlin/androidx/graphics/shapes/RoundedPolygon)` | Cmn |

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Cubic>` | `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#asCubics(kotlin.Float)(progress: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Returns a representation of the morph object at a given `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#asCubics(kotlin.Float)` value as a list of Cubics. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html` | `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#calculateBounds(kotlin.FloatArray,kotlin.Boolean)(bounds: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html, approximate: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Calculates the axis-aligned bounds of the object. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html` | `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#calculateMaxBounds(kotlin.FloatArray)(bounds: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html)` Like `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#calculateBounds(kotlin.FloatArray,kotlin.Boolean)`, this function calculates the axis-aligned bounds of the object and returns that rectangle. | Cmn |
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#forEachCubic(kotlin.Float,androidx.graphics.shapes.MutableCubic,kotlin.Function1)( progress: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html, mutableCubic: https://developer.android.com/reference/kotlin/androidx/graphics/shapes/MutableCubic, callback: (https://developer.android.com/reference/kotlin/androidx/graphics/shapes/MutableCubic) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Returns a representation of the morph object at a given `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#forEachCubic(kotlin.Float,androidx.graphics.shapes.MutableCubic,kotlin.Function1)` value, iterating over the cubics and calling the callback. | Cmn |

| ### Extension functions |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/Path` | `@https://developer.android.com/reference/kotlin/androidx/compose/material3/ExperimentalMaterial3ExpressiveApi https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph.https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#(androidx.graphics.shapes.Morph).toPath(kotlin.Float,androidx.compose.ui.graphics.Path,kotlin.Int)(progress: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html, path: https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/Path, startAngle: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns a `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/Path` for this `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph`. | Cmn |
| `https://developer.android.com/reference/android/graphics/Path.html` | `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph.https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#(androidx.graphics.shapes.Morph).toPath(kotlin.Float,android.graphics.Path)(progress: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html, path: https://developer.android.com/reference/android/graphics/Path.html)` | android |

## Public constructors

### Morph

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
Morph(start: RoundedPolygon, end: RoundedPolygon)
```

## Public functions

### asCubics

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
fun asCubics(progress: Float): List<Cubic>
```

Returns a representation of the morph object at a given `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#asCubics(kotlin.Float)` value as a list of Cubics. Note that this function causes a new list to be created and populated, so there is some overhead.

| Parameters |
|---|---|
| `progress: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | a value from 0 to 1 that determines the morph's current shape, between the start and end shapes provided at construction time. A value of 0 results in the start shape, a value of 1 results in the end shape, and any value in between results in a shape which is a linear interpolation between those two shapes. The range is generally 0..1 and values outside could result in undefined shapes, but values close to (but outside) the range can be used to get an exaggerated effect (e.g., for a bounce or overshoot animation). |

### calculateBounds

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
fun calculateBounds(
    bounds: FloatArray = FloatArray(4),
    approximate: Boolean = true
): FloatArray
```

Calculates the axis-aligned bounds of the object.

| Parameters |
|---|---|
| `bounds: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html = FloatArray(4)` | a buffer to hold the results. If not supplied, a temporary buffer will be created. |
| `approximate: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html = true` | when true, uses a faster calculation to create the bounding box based on the min/max values of all anchor and control points that make up the shape. Default value is true. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html` | The axis-aligned bounding box for this object, where the rectangles left, top, right, and bottom values will be stored in entries 0, 1, 2, and 3, in that order. |

### calculateMaxBounds

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
fun calculateMaxBounds(bounds: FloatArray = FloatArray(4)): FloatArray
```

Like `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#calculateBounds(kotlin.FloatArray,kotlin.Boolean)`, this function calculates the axis-aligned bounds of the object and returns that rectangle. But this function determines the max dimension of the shape (by calculating the distance from its center to the start and midpoint of each curve) and returns a square which can be used to hold the object in any rotation. This function can be used, for example, to calculate the max size of a UI element meant to hold this shape in any rotation.

| Parameters |
|---|---|
| `bounds: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html = FloatArray(4)` | a buffer to hold the results. If not supplied, a temporary buffer will be created. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html` | The axis-aligned max bounding box for this object, where the rectangles left, top, right, and bottom values will be stored in entries 0, 1, 2, and 3, in that order. |

### forEachCubic

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
inline fun forEachCubic(
    progress: Float,
    mutableCubic: MutableCubic = MutableCubic(),
    callback: (MutableCubic) -> Unit
): Unit
```

Returns a representation of the morph object at a given `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#forEachCubic(kotlin.Float,androidx.graphics.shapes.MutableCubic,kotlin.Function1)` value, iterating over the cubics and calling the callback. This function is faster than `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph#asCubics(kotlin.Float)`, since it doesn't allocate new `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Cubic` instances, but to do this it reuses the same `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/MutableCubic` instance during iteration.

| Parameters |
|---|---|
| `progress: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | a value from 0 to 1 that determines the morph's current shape, between the start and end shapes provided at construction time. A value of 0 results in the start shape, a value of 1 results in the end shape, and any value in between results in a shape which is a linear interpolation between those two shapes. The range is generally 0..1 and values outside could result in undefined shapes, but values close to (but outside) the range can be used to get an exaggerated effect (e.g., for a bounce or overshoot animation). |
| `mutableCubic: https://developer.android.com/reference/kotlin/androidx/graphics/shapes/MutableCubic = MutableCubic()` | An instance of `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/MutableCubic` that will be used to set each cubic in time. |
| `callback: (https://developer.android.com/reference/kotlin/androidx/graphics/shapes/MutableCubic) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | The function to be called for each Cubic |

## Extension functions

### Morph.toPath

Cmn Artifact: [androidx.compose.material3:material3](https://developer.android.com/jetpack/androidx/releases/compose-material3) [View Source](https://cs.android.com/search?q=file:androidx/compose/material3/MaterialShapes.kt+function:toPath)

```
@ExperimentalMaterial3ExpressiveApi
fun Morph.toPath(progress: Float, path: Path = Path(), startAngle: Int = 0): Path
```

Returns a `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/Path` for this `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph`.

| Parameters |
|---|---|
| `progress: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | the `https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph`'s progress |
| `path: https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/Path = Path()` | a `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/Path` to rewind and set with the new path data. In case provided, this Path would be the returned one. |
| `startAngle: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = 0` | the angle (in degrees) from which to begin drawing the generated path. By default, it is set to 0 degrees, meaning the `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/Path` begins drawing at the 3 o'clock position. |

### Morph.toPath

android Artifact: [androidx.graphics:graphics-shapes](https://developer.android.com/jetpack/androidx/releases/graphics) [View Source](https://cs.android.com/search?q=file:androidx/graphics/shapes/Shapes.android.kt+function:toPath)

```
fun Morph.toPath(progress: Float, path: Path = Path()): Path
```