pub struct Offset { /* private fields */ }Expand description
An immutable 2D floating-point offset.
Generally speaking, Offsets can be interpreted in two ways:
-
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
RenderBoxprotocol is typically represented as anOffsetfrom the top left of the parent box. -
As a vector that can be applied to coordinates. For example, when painting a
RenderObject, the parent is passed anOffsetfrom the screen’s origin which it can add to the offsets of its children to find theOffsetfrom 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
impl Offset
Sourcepub const ZERO: Offset
pub const ZERO: Offset
An offset with zero magnitude.
This can be used to represent the origin of a coordinate space.
Sourcepub const INFINITE: Offset
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.
Sourcepub fn from_direction(direction: f64, distance: f64) -> Offset
pub fn from_direction(direction: f64, distance: f64) -> Offset
Sourcepub fn is_infinite(&self) -> bool
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).
Sourcepub fn is_finite(&self) -> bool
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.
Sourcepub fn lt(&self, other: &Offset) -> bool
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.
Sourcepub fn le(&self, other: &Offset) -> bool
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.
Sourcepub fn gt(&self, other: &Offset) -> bool
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.
Sourcepub fn ge(&self, other: &Offset) -> bool
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.
Sourcepub fn distance(&self) -> f64
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.
Sourcepub fn distance_squared(&self) -> f64
pub fn distance_squared(&self) -> f64
The square of the magnitude of the offset.
This is cheaper than computing the distance itself.
Sourcepub fn direction(&self) -> f64
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.
Sourcepub fn scale(&self, scale_x: f64, scale_y: f64) -> Offset
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)Sourcepub fn translate(&self, translate_x: f64, translate_y: f64) -> Offset
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())Sourcepub fn truncating_div(&self, operand: f64) -> Offset
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.
Sourcepub fn lerp(a: Option<Offset>, b: Option<Offset>, t: f64) -> Option<Offset>
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.
impl Add for Offset
Binary addition operator.
Source§impl Add<Offset> for Size
impl Add<Offset> for 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§impl BitAnd<Size> for Offset
Rectangle constructor operator.
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)impl Copy for Offset
Source§impl Div<f64> for Offset
Division operator.
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§impl Mul<f64> for Offset
Multiplication operator.
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§impl Neg for Offset
Unary negation operator.
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§impl Rem<f64> for Offset
Modulo (remainder) operator.
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).
impl StructuralPartialEq for Offset
Source§impl Sub for Offset
Binary subtraction operator.
impl Sub for Offset
Binary subtraction operator.
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.
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.