# VelocityTracker1D

Artifact: [androidx.compose.ui:ui](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/input/pointer/util/VelocityTracker.kt+class:androidx.compose.ui.input.pointer.util.VelocityTracker1D) Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.4.0) Common/All
- Cmn

  ```
  class VelocityTracker1D
  ```

*** ** * ** ***

A velocity tracker calculating velocity in 1 dimension.

Add displacement data points using `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#addDataPoint(kotlin.Long,kotlin.Float)`, and obtain velocity using `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#calculateVelocity()`.

Note: for calculating touch-related or other 2 dimensional/planar velocities, please use `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker`, which handles velocity tracking across both X and Y dimensions at once.

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#VelocityTracker1D(kotlin.Boolean)(isDataDifferential: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Constructor to create a new velocity tracker. | Cmn |

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#addDataPoint(kotlin.Long,kotlin.Float)(timeMillis: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, dataPoint: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Adds a data point for velocity calculation at a given time, `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#addDataPoint(kotlin.Long,kotlin.Float)`. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#calculateVelocity()()` Computes the estimated velocity at the time of the last provided data point. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#calculateVelocity(kotlin.Float)(maximumVelocity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Computes the estimated velocity at the time of the last provided data point. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#resetTracking()()` Clears data points added by `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#addDataPoint(kotlin.Long,kotlin.Float)`. | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#isDataDifferential()` | Cmn |

## Public constructors

### VelocityTracker1D

Cmn Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.4.0)

```
VelocityTracker1D(isDataDifferential: Boolean)
```

Constructor to create a new velocity tracker. It allows to specify whether or not the tracker should consider the data ponits provided via `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#addDataPoint(kotlin.Long,kotlin.Float)` as differential or non-differential.

Differential data ponits represent change in displacement. For instance, differential data points of 2, -1, 5 represent: the object moved by "2" units, then by "-1" units, then by "5" units. An example use case for differential data points is when tracking velocity for an object whose displacements (or change in positions) over time are known.

Non-differential data ponits represent position of the object whose velocity is tracked. For instance, non-differential data points of 2, -1, 5 represent: the object was at position "2", then at position "-1", then at position "5". An example use case for non-differential data points is when tracking velocity for an object whose positions on a geometrical axis over different instances of time are known.

| Parameters |
|---|---|
| `isDataDifferential: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `true` if the data ponits provided to the constructed tracker are differential. `false` otherwise. |

## Public functions

### addDataPoint

Cmn Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.4.0)

```
fun addDataPoint(timeMillis: Long, dataPoint: Float): Unit
```

Adds a data point for velocity calculation at a given time, `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#addDataPoint(kotlin.Long,kotlin.Float)`. The data ponit represents an amount of a change in position (for differential data points), or an absolute position (for non-differential data points). Whether or not the tracker handles differential data points is decided by `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#isDataDifferential()`, which is set once and finally during the construction of the tracker.

Use the same units for the data points provided. For example, having some data points in `cm` and some in `m` will result in incorrect velocity calculations, as this method (and the tracker) has no knowledge of the units used.

### calculateVelocity

Cmn Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.4.0)

```
fun calculateVelocity(): Float
```

Computes the estimated velocity at the time of the last provided data point.

The units of velocity will be `units/second`, where `units` is the units of the data points provided via `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#addDataPoint(kotlin.Long,kotlin.Float)`.

This can be expensive. Only call this when you need the velocity.

### calculateVelocity

Cmn Added in [1.6.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.6.0)

```
fun calculateVelocity(maximumVelocity: Float): Float
```

Computes the estimated velocity at the time of the last provided data point.

The method allows specifying the maximum absolute value for the calculated velocity. If the absolute value of the calculated velocity exceeds the specified maximum, the return value will be clamped down to the maximum. For example, if the absolute maximum velocity is specified as "20", a calculated velocity of "25" will be returned as "20", and a velocity of "-30" will be returned as "-20".

| Parameters |
|---|---|
| `maximumVelocity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | the absolute value of the maximum velocity to be returned in units/second, where `units` is the units of the positions provided to this VelocityTracker. |

### resetTracking

Cmn Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.4.0)

```
fun resetTracking(): Unit
```

Clears data points added by `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/util/VelocityTracker1D#addDataPoint(kotlin.Long,kotlin.Float)`.

## Public properties

### isDataDifferential

Cmn Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.4.0)

```
val isDataDifferential: Boolean
```