#[repr(C)]pub struct Vec2<T> {
pub x: T,
pub y: T,
}Expand description
Vec2 vector.
Fields§
§x: T§y: TImplementations§
Source§impl<T> Vec2<T>
impl<T> Vec2<T>
Sourcepub fn cast<U>(self) -> Vec2<U>where
T: CastTo<U>,
pub fn cast<U>(self) -> Vec2<U>where
T: CastTo<U>,
Casts to a vector of different type with the same dimensions.
Sourcepub fn map<U, F>(self, f: F) -> Vec2<U>where
F: FnMut(T) -> U,
pub fn map<U, F>(self, f: F) -> Vec2<U>where
F: FnMut(T) -> U,
Maps a callable over the components.
Source§impl<T: Scalar> Vec2<T>
Operations on vectors of scalars.
impl<T: Scalar> Vec2<T>
Operations on vectors of scalars.
Sourcepub fn sqr(self) -> Vec2<T>
pub fn sqr(self) -> Vec2<T>
Squares the components.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: -3, y: 4 };
assert_eq!(Vec2(9, 16), this.sqr());
let this = Vec3 { x: 2, y: 3, z: -6 };
assert_eq!(Vec3(4, 9, 36), this.sqr());Sourcepub fn len_sqr(self) -> T
pub fn len_sqr(self) -> T
Calculates the squared length of the vector.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: -3, y: 4 };
assert_eq!(25, this.len_sqr());
let this = Vec3 { x: 2, y: -3, z: 6 };
assert_eq!(49, this.len_sqr());Sourcepub fn len(self) -> Twhere
T: Float,
pub fn len(self) -> Twhere
T: Float,
Calculates the length of the vector.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: -3.0, y: 4.0 };
assert_eq!(5.0, this.len());
let this = Vec3 { x: -2.0, y: 3.0, z: -6.0 };
assert_eq!(7.0, this.len());Sourcepub fn len_hat(self) -> T
pub fn len_hat(self) -> T
Calculates the manhattan length of the vector.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: 3, y: 4 };
assert_eq!(7, this.len_hat());
let this = Vec3 { x: 2, y: -3, z: -6 };
assert_eq!(11, this.len_hat());Sourcepub fn distance_sqr(self, to: Vec2<T>) -> T
pub fn distance_sqr(self, to: Vec2<T>) -> T
Calculates the squared euclidean distance to another vector.
use cvmath::Vec2;
let this = Vec2 { x: 1, y: 1 };
let to = Vec2 { x: 2, y: 2 };
assert_eq!(2, this.distance_sqr(to));Sourcepub fn distance(self, to: Vec2<T>) -> Twhere
T: Float,
pub fn distance(self, to: Vec2<T>) -> Twhere
T: Float,
Calculates the euclidean distance to another vector.
use cvmath::Vec2;
let this = Vec2 { x: 10.0, y: 10.0 };
let to = Vec2 { x: 13.0, y: 14.0 };
assert_eq!(5.0, this.distance(to));Sourcepub fn distance_hat(self, to: Vec2<T>) -> T
pub fn distance_hat(self, to: Vec2<T>) -> T
Calculates the manhattan distance to another vector.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: 1.0, y: 5.0 };
let to = Vec2 { x: 5.0, y: 2.0 };
assert_eq!(7.0, this.distance_hat(to));
let this = Vec3 { x: 1.0, y: 5.0, z: -1.0 };
let to = Vec3 { x: 2.0, y: 3.0, z: 1.0 };
assert_eq!(5.0, this.distance_hat(to));Sourcepub fn norm(self) -> Vec2<T>where
T: Float,
pub fn norm(self) -> Vec2<T>where
T: Float,
Normalizes the vector.
After normalizing the vector has the length 1.0 except the zero vector remains zero.
use cvmath::{Vec2, Vec3};
let this: Vec2<f32> = Vec2 { x: 3.0, y: -4.0 };
assert_eq!(Vec2(0.6, -0.8), this.norm());
let this = Vec3 { x: 0.0, y: 0.0, z: 0.0 };
assert_eq!(this, this.norm());Sourcepub fn norm_len(self) -> (Vec2<T>, T)where
T: Float,
pub fn norm_len(self) -> (Vec2<T>, T)where
T: Float,
Calculates the normalized vector and its length.
After normalizing the vector has the length 1.0 except the zero vector remains zero.
use cvmath::{Vec2, Vec3};
let this: Vec2<f32> = Vec2 { x: 3.0, y: -4.0 };
assert_eq!((Vec2(0.6, -0.8), 5.0), this.norm_len());
let this = Vec3 { x: 0.0, y: 0.0, z: 0.0 };
assert_eq!((this, 0.0), this.norm_len());Sourcepub fn resize(self, len: T) -> Vec2<T>where
T: Float,
pub fn resize(self, len: T) -> Vec2<T>where
T: Float,
Resizes the vector to the given length.
The zero vector remains zero.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: -3.0, y: -4.0 };
assert_eq!(Vec2(-1.5, -2.0), this.resize(2.5));
let this = Vec3 { x: 0.0, y: 0.0, z: 0.0 };
assert_eq!(Vec3(0.0, 0.0, 0.0), this.resize(2.0));Sourcepub fn project_scalar(self, v: Vec2<T>) -> Twhere
T: Float,
pub fn project_scalar(self, v: Vec2<T>) -> Twhere
T: Float,
Calculates the length of self projected onto v.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: 1.0, y: 2.0 };
let v = Vec2 { x: 3.0, y: 4.0 };
assert_eq!(2.2, this.project_scalar(v));
let this = Vec3 { x: 1.0, y: 4.0, z: 0.0 };
let v = Vec3 { x: 4.0, y: 2.0, z: 4.0 };
assert_eq!(2.0, this.project_scalar(v));Sourcepub fn project(self, v: Vec2<T>) -> Vec2<T>where
T: Float,
pub fn project(self, v: Vec2<T>) -> Vec2<T>where
T: Float,
Projection of self onto v.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: -5.0, y: -2.5 };
let v = Vec2 { x: 3.0, y: 4.0 };
assert_eq!(Vec2(-3.0, -4.0), this.project(v));
let this = Vec3 { x: -5.0, y: -2.5, z: 0.0 };
let v = Vec3 { x: 3.0, y: 4.0, z: 0.0 };
assert_eq!(Vec3(-3.0, -4.0, 0.0), this.project(v));Sourcepub fn project_sat(self, v: Vec2<T>) -> Vec2<T>
pub fn project_sat(self, v: Vec2<T>) -> Vec2<T>
Projection of self onto v clamped to v.
use cvmath::Vec2;
let this = Vec2 { x: -5.0, y: -2.5 };
let v = Vec2 { x: 3.0, y: 4.0 };
assert_eq!(Vec2(0.0, 0.0), this.project_sat(v));Sourcepub fn reflect(self, v: Vec2<T>) -> Vec2<T>where
T: Float,
pub fn reflect(self, v: Vec2<T>) -> Vec2<T>where
T: Float,
Reflects self around v.
use cvmath::Vec2;
let this = Vec2 { x: 1.0, y: 3.0 };
let v = Vec2 { x: 4.0, y: 4.0 };
assert_eq!(Vec2(3.0, 1.0), this.reflect(v));Sourcepub fn reverse(self) -> Vec2<T>
pub fn reverse(self) -> Vec2<T>
Reverses the component order.
use cvmath::Vec2;
let this = Vec2 { x: 1, y: 2 };
assert_eq!(Vec2(2, 1), this.reverse());Sourcepub fn polar_angle(self) -> Angle<T>where
T: Float,
pub fn polar_angle(self) -> Angle<T>where
T: Float,
Calculates the polar angle.
use cvmath::Vec2;
let this = Vec2 { x: 1.0, y: 1.0 };
assert_eq!(45.0, this.polar_angle().to_deg());Sourcepub fn signed_angle(self, rhs: Vec2<T>) -> Angle<T>where
T: Float,
pub fn signed_angle(self, rhs: Vec2<T>) -> Angle<T>where
T: Float,
Returns the signed angle between two 2D vectors.
The sign of the angle follows the right-hand rule (counter-clockwise is positive).
use cvmath::Vec2;
let a = Vec2::X;
let b = Vec2::Y;
assert_eq!(90.0, a.signed_angle(b).to_deg());
assert_eq!(-90.0, b.signed_angle(a).to_deg());Sourcepub fn ccw(self) -> Vec2<T>
pub fn ccw(self) -> Vec2<T>
Rotates the vector counter-clockwise by 90°.
The resulting vector is perpendicular to the given vector.
use cvmath::Vec2;
let this = Vec2 { x: 3.0, y: 4.0 };
assert_eq!(Vec2(4.0, -3.0), this.ccw());Sourcepub fn cw(self) -> Vec2<T>
pub fn cw(self) -> Vec2<T>
Rotates the vector clockwise by 90°.
The resulting vector is perpendicular to the given vector.
use cvmath::Vec2;
let this = Vec2 { x: 3.0, y: 4.0 };
assert_eq!(Vec2(-4.0, 3.0), this.cw());Sourcepub fn cross(self, rhs: Vec2<T>) -> T
pub fn cross(self, rhs: Vec2<T>) -> T
Calculates the magnitude of the 3D cross product where the inputs are extended with z = 0.
This result is equal to the area of the parallelogram between the two vectors. This result is equal to twice the area of the triangle between the two vectors.
Furthermore this area is signed; a positive value means the rhs is on the left side of self and a negative value means rhs is on the right side.
use cvmath::Vec2;
let lhs = Vec2 { x: -3, y: -4 };
let rhs = Vec2 { x: -1, y: 2 };
assert_eq!(-10, lhs.cross(rhs));
// Area under the parallelogram defined by (origin, lhs, rhs, lhs + rhs) equals 10
// Area under the triangle defined by (origin, lhs, rhs) equals 5Sourcepub fn hsub(self) -> T
pub fn hsub(self) -> T
Horizontal subtracts the components.
use cvmath::Vec2;
let this = Vec2 { x: 3, y: 4 };
assert_eq!(-1, this.hsub());Sourcepub fn y_intercept(self, x: T) -> Option<T>
pub fn y_intercept(self, x: T) -> Option<T>
Intercepts the vector with x = constant returning the y.
Sourcepub fn x_intercept(self, y: T) -> Option<T>
pub fn x_intercept(self, y: T) -> Option<T>
Intercepts the vector with y = constant returning the x.
Sourcepub fn dot(self, rhs: Vec2<T>) -> T
pub fn dot(self, rhs: Vec2<T>) -> T
Calculates the dot product.
use cvmath::Vec3;
let lhs = Vec3 { x: 1, y: 2, z: 3 };
let rhs = Vec3 { x: 4, y: -5, z: 6 };
assert_eq!(12, Vec3::dot(lhs, rhs));Sourcepub fn cos_angle(self, rhs: Vec2<T>) -> Twhere
T: Float,
pub fn cos_angle(self, rhs: Vec2<T>) -> Twhere
T: Float,
Calculates the cosine of the angle between two vectors.
use cvmath::Vec2;
let lhs = Vec2 { x: 1.0, y: 1.0 };
let rhs = Vec2 { x: 1.0, y: 0.0 };
let sqrt_2_div_2 = 1.0 / 2_f32.sqrt(); // √2 ÷ 2
assert_eq!(sqrt_2_div_2, lhs.cos_angle(rhs));Sourcepub fn angle(self, rhs: Vec2<T>) -> Angle<T>where
T: Float,
pub fn angle(self, rhs: Vec2<T>) -> Angle<T>where
T: Float,
Calculates the angle between two vectors.
use cvmath::Vec2;
let lhs = Vec2 { x: 1.0, y: 1.0 };
let rhs = Vec2 { x: 1.0, y: 0.0 };
assert_eq!(45.0, lhs.angle(rhs).to_deg());Sourcepub fn hadd(self) -> T
pub fn hadd(self) -> T
Horizontal adds all components.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: -2, y: 7 };
assert_eq!(5, this.hadd());
let this = Vec3 { x: 3, y: 4, z: 5 };
assert_eq!(12, this.hadd());Sourcepub fn abs(self) -> Vec2<T>
pub fn abs(self) -> Vec2<T>
Component-wise absolute value.
use cvmath::Vec2;
let this = Vec2 { x: -3, y: 5 };
assert_eq!(Vec2(3, 5), this.abs());Sourcepub fn min(self, rhs: Vec2<T>) -> Vec2<T>
pub fn min(self, rhs: Vec2<T>) -> Vec2<T>
Component-wise minimum value.
use cvmath::Vec2;
let lhs = Vec2 { x: -3, y: 5 };
let rhs = Vec2 { x: 0, y: 2 };
assert_eq!(Vec2(-3, 2), lhs.min(rhs));Sourcepub fn max(self, rhs: Vec2<T>) -> Vec2<T>
pub fn max(self, rhs: Vec2<T>) -> Vec2<T>
Component-wise maximum value.
use cvmath::Vec2;
let lhs = Vec2 { x: -3, y: 5 };
let rhs = Vec2 { x: 0, y: 2 };
assert_eq!(Vec2(0, 5), lhs.max(rhs));Sourcepub fn mul_add(self, vec: Vec2<T>, scale: T) -> Vec2<T>
pub fn mul_add(self, vec: Vec2<T>, scale: T) -> Vec2<T>
Adds the scaled vector.
Equivalent to self + (vec * scale) with less rounding errors.
Sourcepub fn slerp(self, rhs: Vec2<T>, t: T) -> Vec2<T>where
T: Float,
pub fn slerp(self, rhs: Vec2<T>, t: T) -> Vec2<T>where
T: Float,
Spherical interpolation between the vectors with constant velocity.
The result is linear interpolation of the angles between the vectors and their lengths.
This is fairly expensive to calculate requiring trigonometric functions. If constant velocity isn’t required, see the less expensive nlerp.
Sourcepub fn nlerp(self, rhs: Vec2<T>, t: T) -> Vec2<T>where
T: Float,
pub fn nlerp(self, rhs: Vec2<T>, t: T) -> Vec2<T>where
T: Float,
Cheap spherical interpolation between the vectors without constant velocity.
Sourcepub fn exp_decay(self, rhs: Vec2<T>, decay: T, dt: T) -> Vec2<T>where
T: Float,
pub fn exp_decay(self, rhs: Vec2<T>, decay: T, dt: T) -> Vec2<T>where
T: Float,
Exponential decay smoothing.
Also known as lerp smoothing. Useful decay values range from approx 1.0 to 25.0, slow to fast.
use cvmath::Vec2;
struct Entity {
pos: Vec2<f32>,
target: Vec2<f32>,
}
impl Entity {
fn update(&mut self, dt: f32) {
// Smoothly move towards the target.
self.pos = self.pos.exp_decay(self.target, 5.0, dt);
}
}Source§impl<T: PartialOrd> Vec2<T>
impl<T: PartialOrd> Vec2<T>
pub fn spatial_lt(&self, rhs: &Vec2<T>) -> bool
pub fn spatial_le(&self, rhs: &Vec2<T>) -> bool
pub fn spatial_gt(&self, rhs: &Vec2<T>) -> bool
pub fn spatial_ge(&self, rhs: &Vec2<T>) -> bool
Source§impl<T> Vec2<T>
impl<T> Vec2<T>
Sourcepub fn is_infinite(self) -> Bool2where
T: Float,
pub fn is_infinite(self) -> Bool2where
T: Float,
Creates a mask for infinite components.
Sourcepub fn ne(self, rhs: Vec2<T>) -> Bool2where
T: PartialEq,
pub fn ne(self, rhs: Vec2<T>) -> Bool2where
T: PartialEq,
Creates a mask for inequal components.
Sourcepub fn lt(self, rhs: Vec2<T>) -> Bool2where
T: PartialOrd,
pub fn lt(self, rhs: Vec2<T>) -> Bool2where
T: PartialOrd,
Creates a mask for left-hand side components are less than the right-hand side.
Sourcepub fn le(self, rhs: Vec2<T>) -> Bool2where
T: PartialOrd,
pub fn le(self, rhs: Vec2<T>) -> Bool2where
T: PartialOrd,
Creates a mask for left-hand side components are less than or equal the right-hand side.
Sourcepub fn gt(self, rhs: Vec2<T>) -> Bool2where
T: PartialOrd,
pub fn gt(self, rhs: Vec2<T>) -> Bool2where
T: PartialOrd,
Creates a mask for left-hand side components are greater than the right-hand side.
Sourcepub fn ge(self, rhs: Vec2<T>) -> Bool2where
T: PartialOrd,
pub fn ge(self, rhs: Vec2<T>) -> Bool2where
T: PartialOrd,
Creates a mask for left-hand side components are greater than or equal the right-hand side.
Source§impl Vec2<u64>
impl Vec2<u64>
Source§impl Vec2<u32>
impl Vec2<u32>
Source§impl Vec2<u16>
impl Vec2<u16>
Trait Implementations§
Source§impl<T: Copy + AddAssign<T>> AddAssign<Vec2<T>> for Bounds2<T>
impl<T: Copy + AddAssign<T>> AddAssign<Vec2<T>> for Bounds2<T>
Source§fn add_assign(&mut self, rhs: Vec2<T>)
fn add_assign(&mut self, rhs: Vec2<T>)
+= operation. Read moreSource§impl<T: Copy + AddAssign<T>> AddAssign<Vec2<T>> for Rect<T>
impl<T: Copy + AddAssign<T>> AddAssign<Vec2<T>> for Rect<T>
Source§fn add_assign(&mut self, rhs: Vec2<T>)
fn add_assign(&mut self, rhs: Vec2<T>)
+= operation. Read moreSource§impl<U, T: AddAssign<U>> AddAssign<Vec2<U>> for Vec2<T>
impl<U, T: AddAssign<U>> AddAssign<Vec2<U>> for Vec2<T>
Source§fn add_assign(&mut self, rhs: Vec2<U>)
fn add_assign(&mut self, rhs: Vec2<U>)
+= operation. Read moreimpl<T: Copy> Copy for Vec2<T>
Source§impl<U: Scalar, T: DivAssign<U>> DivAssign<U> for Vec2<T>
impl<U: Scalar, T: DivAssign<U>> DivAssign<U> for Vec2<T>
Source§fn div_assign(&mut self, rhs: U)
fn div_assign(&mut self, rhs: U)
/= operation. Read moreSource§impl<U, T: DivAssign<U>> DivAssign<Vec2<U>> for Vec2<T>
impl<U, T: DivAssign<U>> DivAssign<Vec2<U>> for Vec2<T>
Source§fn div_assign(&mut self, rhs: Vec2<U>)
fn div_assign(&mut self, rhs: Vec2<U>)
/= operation. Read moreimpl<T: Eq> Eq for Vec2<T>
Source§impl<T: Float> FromIterator<Vec2<T>> for Bounds2<T>
impl<T: Float> FromIterator<Vec2<T>> for Bounds2<T>
Source§impl<T> Mul<&Vec2<T>> for Transform2<T>
impl<T> Mul<&Vec2<T>> for Transform2<T>
Source§impl<T> Mul<&Vec2<T>> for &Transform2<T>
impl<T> Mul<&Vec2<T>> for &Transform2<T>
Source§impl<T: Copy + Mul<Output = T> + Add<Output = T> + Sub<Output = T>> Mul<Vec2<T>> for Complex<T>
impl<T: Copy + Mul<Output = T> + Add<Output = T> + Sub<Output = T>> Mul<Vec2<T>> for Complex<T>
Source§impl<T> Mul<Vec2<T>> for &Transform2<T>
impl<T> Mul<Vec2<T>> for &Transform2<T>
Source§impl<U: Scalar, T: MulAssign<U>> MulAssign<U> for Vec2<T>
impl<U: Scalar, T: MulAssign<U>> MulAssign<U> for Vec2<T>
Source§fn mul_assign(&mut self, rhs: U)
fn mul_assign(&mut self, rhs: U)
*= operation. Read moreSource§impl<U, T: MulAssign<U>> MulAssign<Vec2<U>> for Vec2<T>
impl<U, T: MulAssign<U>> MulAssign<Vec2<U>> for Vec2<T>
Source§fn mul_assign(&mut self, rhs: Vec2<U>)
fn mul_assign(&mut self, rhs: Vec2<U>)
*= operation. Read moreSource§impl<T: Ord> Ord for Vec2<T>
impl<T: Ord> Ord for Vec2<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialOrd> PartialOrd for Vec2<T>
impl<T: PartialOrd> PartialOrd for Vec2<T>
Source§impl<U: Scalar, T: RemAssign<U>> RemAssign<U> for Vec2<T>
impl<U: Scalar, T: RemAssign<U>> RemAssign<U> for Vec2<T>
Source§fn rem_assign(&mut self, rhs: U)
fn rem_assign(&mut self, rhs: U)
%= operation. Read moreSource§impl<U, T: RemAssign<U>> RemAssign<Vec2<U>> for Vec2<T>
impl<U, T: RemAssign<U>> RemAssign<Vec2<U>> for Vec2<T>
Source§fn rem_assign(&mut self, rhs: Vec2<U>)
fn rem_assign(&mut self, rhs: Vec2<U>)
%= operation. Read moreimpl<T: PartialEq> StructuralPartialEq for Vec2<T>
Source§impl<T: Copy + SubAssign<T>> SubAssign<Vec2<T>> for Bounds2<T>
impl<T: Copy + SubAssign<T>> SubAssign<Vec2<T>> for Bounds2<T>
Source§fn sub_assign(&mut self, rhs: Vec2<T>)
fn sub_assign(&mut self, rhs: Vec2<T>)
-= operation. Read moreSource§impl<T: Copy + SubAssign<T>> SubAssign<Vec2<T>> for Rect<T>
impl<T: Copy + SubAssign<T>> SubAssign<Vec2<T>> for Rect<T>
Source§fn sub_assign(&mut self, rhs: Vec2<T>)
fn sub_assign(&mut self, rhs: Vec2<T>)
-= operation. Read moreSource§impl<U, T: SubAssign<U>> SubAssign<Vec2<U>> for Vec2<T>
impl<U, T: SubAssign<U>> SubAssign<Vec2<U>> for Vec2<T>
Source§fn sub_assign(&mut self, rhs: Vec2<U>)
fn sub_assign(&mut self, rhs: Vec2<U>)
-= operation. Read more