Skip to main content

Offset

Struct Offset 

Source
pub struct Offset { /* private fields */ }
Expand description

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.

Implementations§

Source§

impl Offset

Source

pub const ZERO: Offset

An offset with zero magnitude.

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

Source

pub const INFINITE: Offset

An offset with infinite x and y components.

See also:

  • is_infinite, which checks whether either component is infinite.
  • is_finite, which checks whether both components are finite.
Source

pub const fn new(dx: f64, dy: f64) -> Offset

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

Source

pub fn from_direction(direction: f64, distance: f64) -> Offset

Creates an offset from its direction and distance.

The direction is in radians clockwise from the positive x-axis.

Pass 1.0 for distance to create a unit vector.

Source

pub const fn dx(&self) -> f64

The x component of the offset.

The y component is given by dy.

Source

pub const fn dy(&self) -> f64

The y component of the offset.

The x component is given by dx.

Source

pub fn is_infinite(&self) -> bool

Returns true if either component is f64::INFINITY, and false if both are finite (or negative infinity, or NaN).

This is different than comparing for equality with an instance that has both components set to f64::INFINITY.

See also:

  • is_finite, which is true if both components are finite (and not NaN).
Source

pub fn is_finite(&self) -> bool

Whether both components are finite (neither infinite nor NaN).

See also:

  • is_infinite, which returns true if either component is equal to positive infinity.
Source

pub fn lt(&self, other: &Offset) -> bool

Less-than operator. Compares an Offset or Size to another Offset or Size, and returns true if both the horizontal and vertical values of the left-hand-side operand are smaller than the horizontal and vertical values of the right-hand-side operand respectively. Returns false otherwise.

This is a partial ordering. It is possible for two values to be neither less, nor greater than, nor equal to, another.

Source

pub fn le(&self, other: &Offset) -> bool

Less-than-or-equal-to operator. Compares an Offset or Size to another Offset or Size, and returns true if both the horizontal and vertical values of the left-hand-side operand are smaller than or equal to the horizontal and vertical values of the right-hand-side operand respectively. Returns false otherwise.

This is a partial ordering. It is possible for two values to be neither less, nor greater than, nor equal to, another.

Source

pub fn gt(&self, other: &Offset) -> bool

Greater-than operator. Compares an Offset or Size to another Offset or Size, and returns true if both the horizontal and vertical values of the left-hand-side operand are bigger than the horizontal and vertical values of the right-hand-side operand respectively. Returns false otherwise.

This is a partial ordering. It is possible for two values to be neither less, nor greater than, nor equal to, another.

Source

pub fn ge(&self, other: &Offset) -> bool

Greater-than-or-equal-to operator. Compares an Offset or Size to another Offset or Size, and returns true if both the horizontal and vertical values of the left-hand-side operand are bigger than or equal to the horizontal and vertical values of the right-hand-side operand respectively. Returns false otherwise.

This is a partial ordering. It is possible for two values to be neither less, nor greater than, nor equal to, another.

Source

pub fn distance(&self) -> f64

The magnitude of the offset.

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

Source

pub fn distance_squared(&self) -> f64

The square of the magnitude of the offset.

This is cheaper than computing the distance itself.

Source

pub fn direction(&self) -> f64

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 right 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.
Source

pub fn scale(&self, scale_x: f64, scale_y: f64) -> Offset

Returns a new offset with the x component scaled by scale_x and the y component scaled by scale_y.

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

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

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

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

pub fn translate(&self, translate_x: f64, translate_y: f64) -> Offset

Returns a new offset with translate_x added to the x component and translate_y added to the y component.

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

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

pub fn truncating_div(&self, operand: f64) -> Offset

Integer (truncating) division operator.

Returns an offset whose coordinates are the coordinates of this offset divided by the scalar operand, rounded towards zero.

Source

pub fn lerp(a: Option<Offset>, b: Option<Offset>, t: f64) -> Option<Offset>

Linearly interpolate between two offsets.

If either offset is null, this function interpolates from Offset::ZERO.

The t argument represents position on the timeline, with 0.0 meaning that the interpolation has not started, returning a (or something equivalent to a), 1.0 meaning that the interpolation has finished, returning b (or something equivalent to b), and values in between meaning that the interpolation is at the relevant point on the timeline between a and b. 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 such as Curves.elasticInOut).

Values for t are usually obtained from an Animation<double>, such as an AnimationController.

Trait Implementations§

Source§

impl Add for 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.

Source§

type Output = Offset

The resulting type after applying the + operator.
Source§

fn add(self, other: Offset) -> Offset

Performs the + operation. Read more
Source§

impl Add<Offset> for Size

Binary addition operator for adding an Offset to a Size.

Returns a Size whose width is the sum of the width of the left-hand-side operand, a Size, and the Offset::dx dimension of the right-hand-side operand, an Offset, and whose height is the sum of the height of the left-hand-side operand and the Offset::dy dimension of the right-hand-side operand.

Source§

type Output = Size

The resulting type after applying the + operator.
Source§

fn add(self, other: Offset) -> Size

Performs the + operation. Read more
Source§

impl BitAnd<Size> for Offset

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.

let my_rect = Offset::ZERO & Size::new(100.0, 100.0);
// same as: Rect::from_ltwh(0.0, 0.0, 100.0, 100.0)
Source§

type Output = Rect

The resulting type after applying the & operator.
Source§

fn bitand(self, other: Size) -> Rect

Performs the & operation. Read more
Source§

impl Clone for Offset

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Offset

Source§

impl Debug for Offset

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div<f64> for 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 (an f64).

See also scale.

Source§

type Output = Offset

The resulting type after applying the / operator.
Source§

fn div(self, operand: f64) -> Offset

Performs the / operation. Read more
Source§

impl From<Offset> for Point

Source§

fn from(offset: Offset) -> Point

Converts to this type from the input type.
Source§

impl From<Point> for Offset

Source§

fn from(point: Point) -> Offset

Converts to this type from the input type.
Source§

impl Mul<f64> for 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 (an f64).

See also scale.

Source§

type Output = Offset

The resulting type after applying the * operator.
Source§

fn mul(self, operand: f64) -> Offset

Performs the * operation. Read more
Source§

impl Neg for 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.

Source§

type Output = Offset

The resulting type after applying the - operator.
Source§

fn neg(self) -> Offset

Performs the unary - operation. Read more
Source§

impl PartialEq for Offset

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Rem<f64> for 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 (an f64).

Source§

type Output = Offset

The resulting type after applying the % operator.
Source§

fn rem(self, operand: f64) -> Offset

Performs the % operation. Read more
Source§

impl StructuralPartialEq for Offset

Source§

impl Sub for 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.

Source§

type Output = Offset

The resulting type after applying the - operator.
Source§

fn sub(self, other: Offset) -> Offset

Performs the - operation. Read more
Source§

impl Sub<Offset> for Size

Subtracting an Offset from a Size returns the Size that is smaller than the Size operand by the difference given by the Offset operand. In other words, the returned Size has a width consisting of the width of the left-hand-side operand minus the Offset::dx dimension of the right-hand-side operand, and a height consisting of the height of the left-hand-side operand minus the Offset::dy dimension of the right-hand-side operand.

Source§

type Output = Size

The resulting type after applying the - operator.
Source§

fn sub(self, other: Offset) -> Size

Performs the - operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,