Expand description
2D vector
Usually used as either a coordinate or a difference of coordinates, but may have some other uses.
Vectors are partially ordered and support component-wise comparison via
methods like lhs.lt(rhs)
. The PartialOrd
trait is not implemented since
it implements lhs ≤ rhs
as lhs < rhs || lhs == rhs
which is wrong for
vectors (consider for lhs = (0, 1), rhs = (1, 0)
).
Tuple Fields§
§0: f32
§1: f32
Implementations§
source§impl Vec2
impl Vec2
sourcepub const NEG_INFINITY: Vec2 = _
pub const NEG_INFINITY: Vec2 = _
Negative infinity
sourcepub const fn splat(value: f32) -> Self
pub const fn splat(value: f32) -> Self
Constructs a new instance with each element initialized to value
.
Examples found in repository?
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
fn step(&mut self, (decay_mul, decay_sub): (f32, f32)) -> Option<Offset> {
if let Glide::Glide(start, v, rest) = self {
let now = Instant::now();
let dur = (now - *start).as_secs_f32();
let d = *v * dur + *rest;
let delta = Offset::conv_approx(d);
let rest = d - Vec2::conv(delta);
if v.abs().max_comp() >= 1.0 {
let mut v = *v * decay_mul.powf(dur);
v = v - v.abs().min(Vec2::splat(decay_sub * dur)) * v.sign();
*self = Glide::Glide(now, v, rest);
Some(delta)
} else {
*self = Glide::None;
None
}
} else {
None
}
}
sourcepub fn max_comp(self) -> f32
pub fn max_comp(self) -> f32
Take the maximum component
Examples found in repository?
More examples
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
fn step(&mut self, (decay_mul, decay_sub): (f32, f32)) -> Option<Offset> {
if let Glide::Glide(start, v, rest) = self {
let now = Instant::now();
let dur = (now - *start).as_secs_f32();
let d = *v * dur + *rest;
let delta = Offset::conv_approx(d);
let rest = d - Vec2::conv(delta);
if v.abs().max_comp() >= 1.0 {
let mut v = *v * decay_mul.powf(dur);
v = v - v.abs().min(Vec2::splat(decay_sub * dur)) * v.sign();
*self = Glide::Glide(now, v, rest);
Some(delta)
} else {
*self = Glide::None;
None
}
} else {
None
}
}
sourcepub fn min(self, other: Self) -> Self
pub fn min(self, other: Self) -> Self
Return the minimum, componentwise
Examples found in repository?
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
fn step(&mut self, (decay_mul, decay_sub): (f32, f32)) -> Option<Offset> {
if let Glide::Glide(start, v, rest) = self {
let now = Instant::now();
let dur = (now - *start).as_secs_f32();
let d = *v * dur + *rest;
let delta = Offset::conv_approx(d);
let rest = d - Vec2::conv(delta);
if v.abs().max_comp() >= 1.0 {
let mut v = *v * decay_mul.powf(dur);
v = v - v.abs().min(Vec2::splat(decay_sub * dur)) * v.sign();
*self = Glide::Glide(now, v, rest);
Some(delta)
} else {
*self = Glide::None;
None
}
} else {
None
}
}
sourcepub fn abs(self) -> Self
pub fn abs(self) -> Self
Take the absolute value of each component
Examples found in repository?
More examples
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
fn step(&mut self, (decay_mul, decay_sub): (f32, f32)) -> Option<Offset> {
if let Glide::Glide(start, v, rest) = self {
let now = Instant::now();
let dur = (now - *start).as_secs_f32();
let d = *v * dur + *rest;
let delta = Offset::conv_approx(d);
let rest = d - Vec2::conv(delta);
if v.abs().max_comp() >= 1.0 {
let mut v = *v * decay_mul.powf(dur);
v = v - v.abs().min(Vec2::splat(decay_sub * dur)) * v.sign();
*self = Glide::Glide(now, v, rest);
Some(delta)
} else {
*self = Glide::None;
None
}
} else {
None
}
}
sourcepub fn sign(self) -> Self
pub fn sign(self) -> Self
For each component, return ±1
with the same sign as self
.
Examples found in repository?
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
fn step(&mut self, (decay_mul, decay_sub): (f32, f32)) -> Option<Offset> {
if let Glide::Glide(start, v, rest) = self {
let now = Instant::now();
let dur = (now - *start).as_secs_f32();
let d = *v * dur + *rest;
let delta = Offset::conv_approx(d);
let rest = d - Vec2::conv(delta);
if v.abs().max_comp() >= 1.0 {
let mut v = *v * decay_mul.powf(dur);
v = v - v.abs().min(Vec2::splat(decay_sub * dur)) * v.sign();
*self = Glide::Glide(now, v, rest);
Some(delta)
} else {
*self = Glide::None;
None
}
} else {
None
}
}
sourcepub fn complex_mul(self, rhs: Self) -> Self
pub fn complex_mul(self, rhs: Self) -> Self
Multiply two vectors as if they are complex numbers
sourcepub fn complex_div(self, rhs: Self) -> Self
pub fn complex_div(self, rhs: Self) -> Self
Divide by a second vector as if they are complex numbers
sourcepub fn complex_inv(self) -> Self
pub fn complex_inv(self) -> Self
Take the complex reciprocal
sourcepub fn sum_square(self) -> f32
pub fn sum_square(self) -> f32
Return the sum of the square of the terms
sourcepub fn extract<D: Directional>(self, dir: D) -> f32
pub fn extract<D: Directional>(self, dir: D) -> f32
Extract one component, based on a direction
This merely extracts the horizontal or vertical component. It never negates it, even if the axis is reversed.
Trait Implementations§
source§impl AddAssign<Vec2> for Quad
impl AddAssign<Vec2> for Quad
source§fn add_assign(&mut self, rhs: Vec2)
fn add_assign(&mut self, rhs: Vec2)
+=
operation. Read moresource§impl AddAssign<Vec2> for Vec2
impl AddAssign<Vec2> for Vec2
source§fn add_assign(&mut self, rhs: Vec2)
fn add_assign(&mut self, rhs: Vec2)
+=
operation. Read moresource§impl AddAssign<f32> for Vec2
impl AddAssign<f32> for Vec2
source§fn add_assign(&mut self, rhs: f32)
fn add_assign(&mut self, rhs: f32)
+=
operation. Read moresource§impl ConvApprox<DVec2> for Vec2
impl ConvApprox<DVec2> for Vec2
source§impl ConvApprox<Vec2> for Coord
impl ConvApprox<Vec2> for Coord
source§fn try_conv_approx(arg: Vec2) -> Result<Self>
fn try_conv_approx(arg: Vec2) -> Result<Self>
source§fn conv_approx(x: T) -> Self
fn conv_approx(x: T) -> Self
source§impl ConvApprox<Vec2> for Offset
impl ConvApprox<Vec2> for Offset
source§fn try_conv_approx(arg: Vec2) -> Result<Self>
fn try_conv_approx(arg: Vec2) -> Result<Self>
source§fn conv_approx(x: T) -> Self
fn conv_approx(x: T) -> Self
source§impl ConvApprox<Vec2> for Size
impl ConvApprox<Vec2> for Size
source§fn try_conv_approx(arg: Vec2) -> Result<Self>
fn try_conv_approx(arg: Vec2) -> Result<Self>
source§fn conv_approx(x: T) -> Self
fn conv_approx(x: T) -> Self
source§impl ConvFloat<Vec2> for Coord
impl ConvFloat<Vec2> for Coord
source§fn try_conv_trunc(x: Vec2) -> Result<Self>
fn try_conv_trunc(x: Vec2) -> Result<Self>
source§fn conv_trunc(x: T) -> Self
fn conv_trunc(x: T) -> Self
source§fn conv_nearest(x: T) -> Self
fn conv_nearest(x: T) -> Self
source§fn conv_floor(x: T) -> Self
fn conv_floor(x: T) -> Self
source§impl ConvFloat<Vec2> for Offset
impl ConvFloat<Vec2> for Offset
source§fn try_conv_trunc(x: Vec2) -> Result<Self>
fn try_conv_trunc(x: Vec2) -> Result<Self>
source§fn conv_trunc(x: T) -> Self
fn conv_trunc(x: T) -> Self
source§fn conv_nearest(x: T) -> Self
fn conv_nearest(x: T) -> Self
source§fn conv_floor(x: T) -> Self
fn conv_floor(x: T) -> Self
source§impl ConvFloat<Vec2> for Size
impl ConvFloat<Vec2> for Size
source§fn try_conv_trunc(x: Vec2) -> Result<Self>
fn try_conv_trunc(x: Vec2) -> Result<Self>
source§fn conv_trunc(x: T) -> Self
fn conv_trunc(x: T) -> Self
source§fn conv_nearest(x: T) -> Self
fn conv_nearest(x: T) -> Self
source§fn conv_floor(x: T) -> Self
fn conv_floor(x: T) -> Self
source§impl<'de> Deserialize<'de> for Vec2
impl<'de> Deserialize<'de> for Vec2
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl From<LogicalSize> for Vec2
impl From<LogicalSize> for Vec2
source§fn from(LogicalSize: LogicalSize) -> Self
fn from(LogicalSize: LogicalSize) -> Self
source§impl From<Vec2> for LogicalSize
impl From<Vec2> for LogicalSize
source§impl PartialEq<Vec2> for Vec2
impl PartialEq<Vec2> for Vec2
source§impl SubAssign<Vec2> for Quad
impl SubAssign<Vec2> for Quad
source§fn sub_assign(&mut self, rhs: Vec2)
fn sub_assign(&mut self, rhs: Vec2)
-=
operation. Read moresource§impl SubAssign<Vec2> for Vec2
impl SubAssign<Vec2> for Vec2
source§fn sub_assign(&mut self, rhs: Vec2)
fn sub_assign(&mut self, rhs: Vec2)
-=
operation. Read moresource§impl SubAssign<f32> for Vec2
impl SubAssign<f32> for Vec2
source§fn sub_assign(&mut self, rhs: f32)
fn sub_assign(&mut self, rhs: f32)
-=
operation. Read more