An immutable 2D floating-point offset.

Generally speaking, Offsets can be interpreted in two ways:

  1. As representing a point in Cartesian space a specified distance from a separately-maintained origin. For example, the top-left position of children in the RenderBox protocol is typically represented as an Offset from the top left of the parent box.

  2. As a vector that can be applied to coordinates. For example, when painting a widget, the parent is passed an Offset from the screen's origin which it can add to the offsets of its children to find the Offset from the screen's origin to each of the children.

Because a particular Offset can be interpreted as one sense at one time then as the other sense at a later time, the same class is used for both senses.

See also:

  • Size, which represents a vector describing the size of a rectangle.

To create an Offset, call the top-level function that accepts an x/y pair of coordinates:

val offset = Offset(x, y)

The primary constructor of Offset is intended to be used with the packedValue property to allow storing offsets in arrays or collections of primitives without boxing.

Summary

Nested types

Public companion properties

Offset

An offset with infinite x and y components.

Cmn
Offset

Represents an unspecified Offset value, usually a replacement for null when a primitive value is desired.

Cmn
Offset

An offset with zero magnitude.

Cmn

Public constructors

Offset(packedValue: Long)
Cmn

Public functions

inline operator Float
Cmn
inline operator Float
Cmn
Offset
copy(x: Float, y: Float)

Returns a copy of this Offset instance optionally overriding the x or y parameter

Cmn
operator Offset
div(operand: Float)

Division operator.

Cmn
Float

The magnitude of the offset.

Cmn
Float

The square of the magnitude of the offset.

Cmn
inline Boolean

Returns:

Cmn
operator Offset
minus(other: Offset)

Binary subtraction operator.

Cmn
operator Offset
plus(other: Offset)

Binary addition operator.

Cmn
operator Offset
rem(operand: Float)

Modulo (remainder) operator.

Cmn
operator Offset
times(operand: Float)

Multiplication operator.

Cmn
open String
Cmn
inline operator Offset

Unary negation operator.

Cmn

Public properties

Long

Long value encoding the x and y components of the Offset.

Cmn
Float
Cmn
Float
Cmn

Extension functions

operator Offset
Cmn
operator Offset
Cmn
IntOffset

Round a Offset down to the nearest Int coordinates.

Cmn
inline Offset
Offset.takeOrElse(block: () -> Offset)

If this Offset isSpecified then this is returned, otherwise block is executed and its result is returned.

Cmn

Extension properties

Boolean

True if both x and y values of the Offset are finite.

Cmn
Boolean

false when this is Offset.Unspecified.

Cmn
Boolean

true when this is Offset.Unspecified.

Cmn

Public companion properties

Infinite

val InfiniteOffset

An offset with infinite x and y components.

See also isFinite to check whether both components are finite.

Unspecified

val UnspecifiedOffset

Represents an unspecified Offset value, usually a replacement for null when a primitive value is desired.

Zero

val ZeroOffset

An offset with zero magnitude.

This can be used to represent the origin of a coordinate space.

Public constructors

Offset

Offset(packedValue: Long)
Parameters
packedValue: Long

Long value encoding the x and y components of the Offset. Encoded values can be obtained by using the packedValue property of existing Offset instances.

Public functions

component1

inline operator fun component1(): Float

component2

inline operator fun component2(): Float

copy

fun copy(
    x: Float = unpackFloat1(packedValue),
    y: Float = unpackFloat2(packedValue)
): Offset

Returns a copy of this Offset instance optionally overriding the x or y parameter

div

operator fun div(operand: Float): Offset

Division operator.

Returns an offset whose coordinates are the coordinates of the left-hand-side operand (an Offset) divided by the scalar right-hand-side operand (a Float).

getDistance

fun getDistance(): Float

The magnitude of the offset.

If you need this value to compare it to another Offset's distance, consider using getDistanceSquared instead, since it is cheaper to compute.

getDistanceSquared

fun getDistanceSquared(): Float

The square of the magnitude of the offset.

This is cheaper than computing the getDistance itself.

isValid

inline fun isValid(): Boolean

Returns:

  • False if x or y is a NaN

  • True if x or y is infinite

  • True otherwise

minus

operator fun minus(other: Offset): Offset

Binary subtraction operator.

Returns an offset whose x value is the left-hand-side operand's x minus the right-hand-side operand's x and whose y value is the left-hand-side operand's y minus the right-hand-side operand's y.

plus

operator fun plus(other: Offset): Offset

Binary addition operator.

Returns an offset whose x value is the sum of the x values of the two operands, and whose y value is the sum of the y values of the two operands.

rem

operator fun rem(operand: Float): Offset

Modulo (remainder) operator.

Returns an offset whose coordinates are the remainder of dividing the coordinates of the left-hand-side operand (an Offset) by the scalar right-hand-side operand (a Float).

times

operator fun times(operand: Float): Offset

Multiplication operator.

Returns an offset whose coordinates are the coordinates of the left-hand-side operand (an Offset) multiplied by the scalar right-hand-side operand (a Float).

toString

open fun toString(): String

unaryMinus

inline operator fun unaryMinus(): Offset

Unary negation operator.

Returns an offset with the coordinates negated.

If the Offset represents an arrow on a plane, this operator returns the same arrow but pointing in the reverse direction.

Public properties

packedValue

val packedValueLong

Long value encoding the x and y components of the Offset. Encoded values can be obtained by using the packedValue property of existing Offset instances.

x

val xFloat

y

val yFloat

Extension functions

operator fun Offset.minus(offset: IntOffset): Offset
operator fun Offset.plus(offset: IntOffset): Offset
fun Offset.round(): IntOffset

Round a Offset down to the nearest Int coordinates.

takeOrElse

inline fun Offset.takeOrElse(block: () -> Offset): Offset

If this Offset isSpecified then this is returned, otherwise block is executed and its result is returned.

Extension properties

val Offset.isFiniteBoolean

True if both x and y values of the Offset are finite. NaN values are not considered finite.

isSpecified

val Offset.isSpecifiedBoolean

false when this is Offset.Unspecified.

isUnspecified

val Offset.isUnspecifiedBoolean

true when this is Offset.Unspecified.