Struct ultraviolet::vec::Vec2
source · #[repr(C)]pub struct Vec2 {
pub x: f32,
pub y: f32,
}
Expand description
A set of two coordinates which may be interpreted as a vector or point in 2d space.
Generally this distinction between a point and vector is more of a pain than it is worth to distinguish on a type level, however when converting to and from homogeneous coordinates it is quite important.
Fields§
§x: f32
§y: f32
Implementations§
source§impl Vec2
impl Vec2
pub const fn new(x: f32, y: f32) -> Self
pub const fn broadcast(val: f32) -> Self
pub fn unit_x() -> Self
pub fn unit_y() -> Self
sourcepub fn into_homogeneous_point(self) -> Vec3
pub fn into_homogeneous_point(self) -> Vec3
Create a homogeneous 2d point from this vector interpreted as a point, meaning the homogeneous component will start with a value of 1.0.
sourcepub fn into_homogeneous_vector(self) -> Vec3
pub fn into_homogeneous_vector(self) -> Vec3
Create a homogeneous 2d vector from this vector, meaning the homogeneous component will always have a value of 0.0.
sourcepub fn from_homogeneous_point(v: Vec3) -> Self
pub fn from_homogeneous_point(v: Vec3) -> Self
Create a 2d point from a homogeneous 2d point, performing division by the homogeneous component. This should not be used for homogeneous 2d vectors, which will have 0 as their homogeneous component.
sourcepub fn from_homogeneous_vector(v: Vec3) -> Self
pub fn from_homogeneous_vector(v: Vec3) -> Self
Create a 2d vector from homogeneous 2d vector, which simply discards the homogeneous component.
pub fn dot(&self, other: Vec2) -> f32
sourcepub fn wedge(&self, other: Vec2) -> Bivec2
pub fn wedge(&self, other: Vec2) -> Bivec2
The wedge (aka exterior) product of two vectors.
Note: Sometimes called “cross” product in 2D. Such a product is not well defined in 2 dimensions and is really just shorthand notation for a hacky operation that extends the vectors into 3 dimensions, takes the cross product, then returns only the resulting Z component as a pseudoscalar value. This value is will have the same value as the resulting bivector of the wedge product in 2d (a 2d bivector is also a kind of pseudoscalar value), so you may use this product to calculate the same value.
This operation results in a bivector, which represents
the plane parallel to the two vectors, and which has a
‘oriented area’ equal to the parallelogram created by extending
the two vectors, oriented such that the positive direction is the
one which would move self
closer to other
.
sourcepub fn geom(&self, other: Vec2) -> Rotor2
pub fn geom(&self, other: Vec2) -> Rotor2
The geometric product of this and another vector, which is defined as the sum of the dot product and the wedge product.
This operation results in a ‘rotor’, named as such as it may define
a rotation. The rotor which results from the geometric product
will rotate in the plane parallel to the two vectors, by twice the angle between
them and in the opposite direction (i.e. it will rotate in the direction that would
bring other
towards self
, and rotate in that direction by twice the angle between them).
pub fn rotate_by(&mut self, rotor: Rotor2)
pub fn rotated_by(self, rotor: Rotor2) -> Self
pub fn reflected(&self, normal: Vec2) -> Self
pub fn mag_sq(&self) -> f32
pub fn mag(&self) -> f32
pub fn normalize(&mut self)
pub fn normalized(&self) -> Self
pub fn mul_add(&self, mul: Vec2, add: Vec2) -> Self
pub fn abs(&self) -> Self
pub fn clamp(&mut self, min: Self, max: Self)
pub fn clamped(self, min: Self, max: Self) -> Self
pub fn map<F>(&self, f: F) -> Selfwhere F: FnMut(f32) -> f32,
pub fn apply<F>(&mut self, f: F)where F: FnMut(f32) -> f32,
pub fn max_by_component(self, other: Self) -> Self
pub fn min_by_component(self, other: Self) -> Self
pub fn component_max(&self) -> f32
pub fn component_min(&self) -> f32
pub fn zero() -> Self
pub fn one() -> Self
pub fn xyz(&self) -> Vec3
pub fn xyzw(&self) -> Vec4
sourcepub fn layout() -> Layout
pub fn layout() -> Layout
Get the core::alloc::Layout
of Self
sourcepub fn as_array(&self) -> &[f32; 2]
pub fn as_array(&self) -> &[f32; 2]
Interpret self
as a statically-sized array of its base numeric type
sourcepub fn as_mut_array(&mut self) -> &mut [f32; 2]
pub fn as_mut_array(&mut self) -> &mut [f32; 2]
Interpret self
as a statically-sized array of its base numeric type
sourcepub fn as_mut_slice(&mut self) -> &mut [f32]
pub fn as_mut_slice(&mut self) -> &mut [f32]
Interpret self
as a slice of its base numeric type
pub fn as_byte_slice(&self) -> &[u8] ⓘ
pub fn as_mut_byte_slice(&mut self) -> &mut [u8] ⓘ
sourcepub const fn as_ptr(&self) -> *const f32
pub const fn as_ptr(&self) -> *const f32
Returns a constant unsafe pointer to the underlying data in the underlying type. This function is safe because all types here are repr(C) and can be represented as their underlying type.
Safety
It is up to the caller to correctly use this pointer and its bounds.
sourcepub fn as_mut_ptr(&mut self) -> *mut f32
pub fn as_mut_ptr(&mut self) -> *mut f32
Returns a mutable unsafe pointer to the underlying data in the underlying type. This function is safe because all types here are repr(C) and can be represented as their underlying type.
Safety
It is up to the caller to correctly use this pointer and its bounds.
Trait Implementations§
source§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<'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 DivAssign<Vec2> for Vec2
impl DivAssign<Vec2> for Vec2
source§fn div_assign(&mut self, rhs: Vec2)
fn div_assign(&mut self, rhs: Vec2)
/=
operation. Read moresource§impl DivAssign<f32> for Vec2
impl DivAssign<f32> for Vec2
source§fn div_assign(&mut self, rhs: f32)
fn div_assign(&mut self, rhs: f32)
/=
operation. Read moresource§impl Lerp<f32> for Vec2
impl Lerp<f32> for Vec2
source§fn lerp(&self, end: Self, t: f32) -> Self
fn lerp(&self, end: Self, t: f32) -> Self
Linearly interpolate between self
and end
by t
between 0.0 and 1.0.
i.e. (1.0 - t) * self + (t) * end
.
For interpolating Rotor
s with linear interpolation, you almost certainly
want to normalize the returned Rotor
. For example,
let interpolated_rotor = rotor1.lerp(rotor2, 0.5).normalized();
For most cases (especially where performance is the primary concern, like in
animation interpolation for games, this ‘normalized lerp’ or ‘nlerp’ is probably
what you want to use. However, there are situations in which you really want
the interpolation between two Rotor
s to be of constant angular velocity. In this
case, check out Slerp
.
source§impl Mul<Vec2> for Similarity2
impl Mul<Vec2> for Similarity2
source§impl MulAssign<Vec2> for Vec2
impl MulAssign<Vec2> for Vec2
source§fn mul_assign(&mut self, rhs: Vec2)
fn mul_assign(&mut self, rhs: Vec2)
*=
operation. Read moresource§impl MulAssign<f32> for Vec2
impl MulAssign<f32> for Vec2
source§fn mul_assign(&mut self, rhs: f32)
fn mul_assign(&mut self, rhs: f32)
*=
operation. Read moresource§impl PartialEq<Vec2> for Vec2
impl PartialEq<Vec2> for Vec2
source§impl Slerp<f32> for Vec2
impl Slerp<f32> for Vec2
source§fn slerp(&self, end: Self, t: f32) -> Self
fn slerp(&self, end: Self, t: f32) -> Self
Spherical-linear interpolation between self
and end
based on t
from 0.0 to 1.0.
self
and end
should both be normalized or something bad will happen!
The implementation for SIMD types also requires that the two things being interpolated between are not exactly aligned, or else the result is undefined.
Basically, interpolation that maintains a constant angular velocity
from one orientation on a unit hypersphere to another. This is sorta the “high quality” interpolation
for Rotor
s, and it can also be used to interpolate other things, one example being interpolation of
3d normal vectors.
Note that you should often normalize the result returned by this operation, when working with Rotor
s, etc!
source§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 TryFrom<Vec2> for IVec2
impl TryFrom<Vec2> for IVec2
source§fn try_from(v: Vec2) -> Result<Self, Self::Error>
fn try_from(v: Vec2) -> Result<Self, Self::Error>
Tries to convert the source to Self in a lossy way, flooring any float value.
Errors
NaN
- If a float value isNaN
.NotFinite
- If a float value is infinity or negative infinity.PosOverflow
- If a float value would be greater than the the self.component max value.NegOverflow
- If a float value would be less than the self.component min value.
§type Error = FloatConversionError
type Error = FloatConversionError
source§impl TryFrom<Vec2> for UVec2
impl TryFrom<Vec2> for UVec2
source§fn try_from(v: Vec2) -> Result<Self, Self::Error>
fn try_from(v: Vec2) -> Result<Self, Self::Error>
Tries to convert the source to Self in a lossy way, flooring any float value.
Errors
NaN
- If a float value isNaN
.NotFinite
- If a float value is infinity or negative infinity.PosOverflow
- If a float value would be greater than the the self.component max value.NegOverflow
- If a float value would be less than the self.component min value.
§type Error = FloatConversionError
type Error = FloatConversionError
impl Copy for Vec2
impl Pod for Vec2
impl StructuralPartialEq for Vec2
Auto Trait Implementations§
impl RefUnwindSafe for Vec2
impl Send for Vec2
impl Sync for Vec2
impl Unpin for Vec2
impl UnwindSafe for Vec2
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
impl<T> CheckedBitPattern for Twhere T: AnyBitPattern,
§type Bits = T
type Bits = T
Self
must have the same layout as the specified Bits
except for
the possible invalid bit patterns being checked during
is_valid_bit_pattern
.source§fn is_valid_bit_pattern(_bits: &T) -> bool
fn is_valid_bit_pattern(_bits: &T) -> bool
bits
as &Self
.