# Vector2

Artifact: [androidx.xr.runtime:runtime](https://developer.android.com/jetpack/androidx/releases/xr-runtime) [View Source](https://cs.android.com/search?q=file:androidx/xr/runtime/math/Vector2.kt+class:androidx.xr.runtime.math.Vector2) Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

*** ** * ** ***


```
public final class Vector2
```

<br />

*** ** * ** ***

Position in the 2D plane.

## Summary

| ### Public fields |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#Down()` Vector with y set to negative one and all other components set to zero. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#Left()` Vector with x set to negative one and all other components set to zero. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#One()` Vector with all components set to one. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#Right()` Vector with x set to one and all other components set to zero. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#Up()` Vector with y set to one and all other components set to zero. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#Zero()` Vector with all components set to zero. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#Vector2(androidx.xr.runtime.math.Vector2)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 other)` Creates a new vector with the same values as the `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#(androidx.xr.runtime.math.Vector2)` vector. |
| `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#Vector2(kotlin.Float,kotlin.Float)(float x, float y)` |

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#abs(androidx.xr.runtime.math.Vector2)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 vector)` Returns the absolute values of each component of the vector. |
| `static final float` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#angularDistance(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 vector1, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 vector2)` Returns the angle between this `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#angularDistance(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)` and `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#angularDistance(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#clamp(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 min, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 max)` Returns a new vector with the values clamped between `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#clamp(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)` and `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#clamp(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)` vectors. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#copy(kotlin.Float,kotlin.Float)(float x, float y)` Returns a copy of the vector. |
| `final float` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#cross(androidx.xr.runtime.math.Vector2)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 other)` Returns the cross product of this vector and the `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#cross(androidx.xr.runtime.math.Vector2)` vector. |
| `static final float` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#distance(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 vector1, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 vector2)` Returns the distance between `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#distance(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)` and `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#distance(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#div(kotlin.Float)(float c)` Returns a new vector with this vector divided by a scalar amount. |
| `final float` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#dot(androidx.xr.runtime.math.Vector2)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 other)` Returns the dot product of this vector and the `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#dot(androidx.xr.runtime.math.Vector2)` vector. |
| `boolean` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#equals(kotlin.Any)(https://developer.android.com/reference/java/lang/Object.html other)` Returns true if this vector is equal to the `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#equals(kotlin.Any)`. |
| `final float` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#getLength()()` The length of the vector. |
| `final float` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#getLengthSquared()()` The squared length of the vector. |
| `final float` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#getX()()` the x component of the vector |
| `final float` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#getY()()` the y component of the vector |
| `int` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#hashCode()()` |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#inverse()()` Returns the component-wise multiplicative inverse of this vector. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#lerp(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2,kotlin.Float)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 start, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 end, float ratio)` Returns a new vector linearly interpolated between `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#lerp(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2,kotlin.Float)` and `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#lerp(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2,kotlin.Float)` by `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#lerp(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2,kotlin.Float)`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#minus(androidx.xr.runtime.math.Vector2)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 other)` Returns a new vector with the difference of this vector and the `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#minus(androidx.xr.runtime.math.Vector2)` vector. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#plus(androidx.xr.runtime.math.Vector2)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 other)` Returns a new vector with the sum of this vector and the `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#plus(androidx.xr.runtime.math.Vector2)` vector. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#scale(androidx.xr.runtime.math.Vector2)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 other)` Returns a new vector by component-wise multiplying this vector by `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#scale(androidx.xr.runtime.math.Vector2)`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#times(kotlin.Float)(float c)` Returns a new vector multiplied by a scalar amount. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#toNormalized()()` Returns a normalized version of this vector. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#toString()()` |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2` | `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#unaryMinus()()` Negates the values of this vector. |

## Public fields

### Down

```
public static final @NonNull Vector2 Down
```

Vector with y set to negative one and all other components set to zero.

### Left

```
public static final @NonNull Vector2 Left
```

Vector with x set to negative one and all other components set to zero.

### One

```
public static final @NonNull Vector2 One
```

Vector with all components set to one.

### Right

```
public static final @NonNull Vector2 Right
```

Vector with x set to one and all other components set to zero.

### Up

```
public static final @NonNull Vector2 Up
```

Vector with y set to one and all other components set to zero.

### Zero

```
public static final @NonNull Vector2 Zero
```

Vector with all components set to zero.

## Public constructors

### Vector2

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public Vector2(@NonNull Vector2 other)
```

Creates a new vector with the same values as the `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#(androidx.xr.runtime.math.Vector2)` vector.

### Vector2

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public Vector2(float x, float y)
```

## Public methods

### abs

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public static final @NonNull Vector2 abs(@NonNull Vector2 vector)
```

Returns the absolute values of each component of the vector.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 vector` | the vector to get the absolute values of |

### angularDistance

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public static final float angularDistance(@NonNull Vector2 vector1, @NonNull Vector2 vector2)
```

Returns the angle between this `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#angularDistance(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)` and `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#angularDistance(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 vector1` | the first vector |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 vector2` | the second vector |

### clamp

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final @NonNull Vector2 clamp(@NonNull Vector2 min, @NonNull Vector2 max)
```

Returns a new vector with the values clamped between `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#clamp(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)` and `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#clamp(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)` vectors.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 min` | the minimum clamp values |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 max` | the maximum clamp values |

### copy

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final @NonNull Vector2 copy(float x, float y)
```

Returns a copy of the vector.

| Parameters |
|---|---|
| `float x` | the new x value for the copied vector |
| `float y` | the new y value for the copied vector |

### cross

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final float cross(@NonNull Vector2 other)
```

Returns the cross product of this vector and the `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#cross(androidx.xr.runtime.math.Vector2)` vector.

### distance

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public static final float distance(@NonNull Vector2 vector1, @NonNull Vector2 vector2)
```

Returns the distance between `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#distance(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)` and `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#distance(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 vector1` | the first vector |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 vector2` | the second vector |

### div

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final @NonNull Vector2 div(float c)
```

Returns a new vector with this vector divided by a scalar amount.

| Parameters |
|---|---|
| `float c` | the scalar to divide by |

### dot

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final float dot(@NonNull Vector2 other)
```

Returns the dot product of this vector and the `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#dot(androidx.xr.runtime.math.Vector2)` vector.

### equals

```
public boolean equals(Object other)
```

Returns true if this vector is equal to the `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#equals(kotlin.Any)`.

### getLength

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final float getLength()
```

The length of the vector.

### getLengthSquared

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final float getLengthSquared()
```

The squared length of the vector.

### getX

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final float getX()
```

the x component of the vector

### getY

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final float getY()
```

the y component of the vector

### hashCode

```
public int hashCode()
```

### inverse

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final @NonNull Vector2 inverse()
```

Returns the component-wise multiplicative inverse of this vector.

### lerp

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public static final @NonNull Vector2 lerp(@NonNull Vector2 start, @NonNull Vector2 end, float ratio)
```

Returns a new vector linearly interpolated between `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#lerp(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2,kotlin.Float)` and `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#lerp(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2,kotlin.Float)` by `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#lerp(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2,kotlin.Float)`.

If `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2.Companion#lerp(androidx.xr.runtime.math.Vector2,androidx.xr.runtime.math.Vector2,kotlin.Float)` is outside of the range `[0, 1]`, the returned vector will be extrapolated.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 start` | the starting vector |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/math/Vector2 end` | the ending vector |
| `float ratio` | the interpolation ratio |

### minus

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final @NonNull Vector2 minus(@NonNull Vector2 other)
```

Returns a new vector with the difference of this vector and the `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#minus(androidx.xr.runtime.math.Vector2)` vector.

### plus

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final @NonNull Vector2 plus(@NonNull Vector2 other)
```

Returns a new vector with the sum of this vector and the `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#plus(androidx.xr.runtime.math.Vector2)` vector.

### scale

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final @NonNull Vector2 scale(@NonNull Vector2 other)
```

Returns a new vector by component-wise multiplying this vector by `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#scale(androidx.xr.runtime.math.Vector2)`.

### times

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final @NonNull Vector2 times(float c)
```

Returns a new vector multiplied by a scalar amount.

| Parameters |
|---|---|
| `float c` | the scalar to multiply by |

### toNormalized

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final @NonNull Vector2 toNormalized()
```

Returns a normalized version of this vector. A zero-length vector has no direction to normalize and returns `https://developer.android.com/reference/androidx/xr/runtime/math/Vector2#Zero()` rather than a vector of NaN components.

### toString

```
public @NonNull String toString()
```

### unaryMinus

Added in [1.0.0-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

```
public final @NonNull Vector2 unaryMinus()
```

Negates the values of this vector.