The Android Developer Challenge is back! Submit your idea before December 2.

Offset

data class Offset : OffsetBase
kotlin.Any
   ↳ androidx.ui.engine.geometry.Offset

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 RenderObject, 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.

Creates an offset. The first argument sets dx, the horizontal component, and the second sets dy, the vertical component.

Summary

Public constructors

<init>(dx: Float, dy: Float)

An immutable 2D floating-point offset.

Public methods

infix Rect
and(other: Size)

Rectangle constructor operator.

operator Offset
div(operand: Float)

Division operator.

Boolean
equals(other: Any?)

Float

The angle of this offset as radians clockwise from the positive x-axis, in the range -pi to pi, assuming positive values of the x-axis go to the left and positive values of the y-axis go down.

Float

The magnitude of the offset.

Float

The square of the magnitude of the offset.

Int

operator Offset
minus(other: Offset)

Binary subtraction operator.

operator Offset
plus(other: Offset)

Binary addition operator.

operator Offset
rem(operand: Float)

Modulo (remainder) operator.

Offset
scale(scaleX: Float, scaleY: Float)

Returns a new offset with the x component scaled by scaleX and the y component scaled by scaleY.

operator Offset
times(operand: Float)

Multiplication operator.

String

Offset
translate(translateX: Float, translateY: Float)

Returns a new offset with translateX added to the x component and translateY added to the y component.

Offset
truncDiv(operand: Float)

Integer (truncating) division operator.

operator Offset

Unary negation operator.

Companion functions

Boolean
isValid(offset: Offset)

Offset?
lerp(start: Offset, stop: Offset, fraction: Float)

Linearly interpolate between two offsets.

Inherited functions

Extension functions

From androidx.ui.engine.geometry
PxPosition

Properties

Float

Float

Companion properties

Offset

An offset with infinite x and y components.

Offset

An offset with zero magnitude.

Public constructors

<init>

Offset(
    dx: Float,
    dy: Float)

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 RenderObject, 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.

Creates an offset. The first argument sets dx, the horizontal component, and the second sets dy, the vertical component.

Public methods

and

infix fun and(other: Size
): Rect

Rectangle constructor operator.

Combines an Offset and a Size to form a Rect whose top-left coordinate is the point given by adding this offset, the left-hand-side operand, to the origin, and whose size is the right-hand-side operand.

Rect myRect = Offset.zero & const Size(100.0, 100.0);
// same as: new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0)

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).

See also scale.

equals

fun equals(other: Any?
): Boolean

getDirection

fun getDirection(): Float

The angle of this offset as radians clockwise from the positive x-axis, in the range -pi to pi, assuming positive values of the x-axis go to the left and positive values of the y-axis go down.

Zero means that dy is zero and dx is zero or positive.

Values from zero to pi/2 indicate positive values of dx and dy, the bottom-right quadrant.

Values from pi/2 to pi indicate negative values of dx and positive values of dy, the bottom-left quadrant.

Values from zero to -pi/2 indicate positive values of dx and negative values of dy, the top-right quadrant.

Values from -pi/2 to -pi indicate negative values of dx and dy, the top-left quadrant.

When dy is zero and dx is negative, the direction is pi.

When dx is zero, direction is pi/2 if dy is positive and -pi/2 if dy is negative.

See also:

  • distance, to compute the magnitude of the vector.
  • Canvas.rotate, which uses the same convention for its angle.

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.

hashCode

fun hashCode(): Int

minus

operator fun minus(other: Offset
): Offset

Binary subtraction operator.

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

See also translate.

plus

operator fun plus(other: Offset
): Offset

Binary addition operator.

Returns an offset whose dx value is the sum of the dx values of the two operands, and whose dy value is the sum of the dy values of the two operands.

See also translate.

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).

scale

fun scale(
    scaleX: Float,
    scaleY: Float
): Offset

Returns a new offset with the x component scaled by scaleX and the y component scaled by scaleY.

If the two scale arguments are the same, consider using the * operator instead:

Offset a = const Offset(10.0, 10.0);
Offset b = a * 2.0; // same as: a.scale(2.0, 2.0)

If the two arguments are -1, consider using the unary - operator instead:

Offset a = const Offset(10.0, 10.0);
Offset b = -a; // same as: a.scale(-1.0, -1.0)

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).

See also scale.

toString

fun toString(): String

translate

fun translate(
    translateX: Float,
    translateY: Float
): Offset

Returns a new offset with translateX added to the x component and translateY added to the y component.

If the arguments come from another Offset, consider using the + or - operators instead:

Offset a = const Offset(10.0, 10.0);
Offset b = const Offset(10.0, 10.0);
Offset c = a + b; // same as: a.translate(b.dx, b.dy)
Offset d = a - b; // same as: a.translate(-b.dx, -b.dy)

truncDiv

fun truncDiv(operand: Float
): Offset

Integer (truncating) 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), rounded towards zero.

unaryMinus

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.

Properties

dx

val dx: Float

dy

val dy: Float

Companion functions

isValid

fun isValid(offset: Offset
): Boolean

lerp

fun lerp(
    start: Offset,
    stop: Offset,
    fraction: Float
): Offset?

Linearly interpolate between two offsets.

The fraction argument represents position on the timeline, with 0.0 meaning that the interpolation has not started, returning start (or something equivalent to start), 1.0 meaning that the interpolation has finished, returning stop (or something equivalent to stop), and values in between meaning that the interpolation is at the relevant point on the timeline between start and stop. The interpolation can be extrapolated beyond 0.0 and 1.0, so negative values and values greater than 1.0 are valid (and can easily be generated by curves).

Values for fraction are usually obtained from an Animation, such as an AnimationController.

Companion properties

infinite

val infinite: Offset

An offset with infinite x and y components.

See also:

  • isInfinite, which checks whether either component is infinite.
  • isFinite, which checks whether both components are finite.

zero

val zero: Offset

An offset with zero magnitude.

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