Struct Vector2

Source
#[repr(C)]
pub struct Vector2<T> { pub x: T, pub y: T, }
Expand description

Vector2.

Fields§

§x: T

The vector’s X component.

§y: T

The vector’s Y component.

Implementations§

Source§

impl<T> Vector2<T>

Source

pub const fn new(x: T, y: T) -> Self

Construct a new Vector2.

Source§

impl<T: Copy> Vector2<T>

Source

pub const fn splat(x: T) -> Self

Construct a new Vector2 with x and y set to the given value.

Source§

impl Vector2<f32>

Source

pub const ZERO: Vec2

Zero unit vector. (0, 0)

Source

pub const RIGHT: Vec2

Right unit vector. (1, 0)

Source

pub const LEFT: Vec2

Left unit vector. (-1, 0)

Source

pub const UP: Vec2

Up unit vector. Y-Down, so points -Y. (0, -1)

Source

pub const DOWN: Vec2

Down unit vector. Y-Down, so points +Y. (0, 1)

Source§

impl Vector2<f64>

Source

pub const ZERO: Vec2

Zero unit vector. (0, 0)

Source

pub const RIGHT: Vec2

Right unit vector. (1, 0)

Source

pub const LEFT: Vec2

Left unit vector. (-1, 0)

Source

pub const UP: Vec2

Up unit vector. Y-Down, so points -Y. (0, -1)

Source

pub const DOWN: Vec2

Down unit vector. Y-Down, so points +Y. (0, 1)

Source§

impl<T: Neg<Output = T>> Vector2<T>

Source

pub fn orthogonal(self) -> Self

Returns a perpendicular vector, rotated 90 degrees counter-clockwise, with the same length.

Source§

impl<T: FloatAlone> Vector2<T>

Source

pub fn from_angle(angle: T) -> Self

Creates a unit Vector2 rotated to the given angle (radians). This is equivalent to Vec2::new(angle.cos(), angle.sin()).

assert_eq!(Vec2::from_angle(0.0), Vec2::RIGHT);
assert_eq!(Vec2::RIGHT.angle(), 0.0);
assert!(Vec2::from_angle(PI / 2.0).approx_eq(Vec2::new(0.0, 1.0)));
Source

pub fn abs(self) -> Self

Returns a new vector with all components in absolute values (i.e. positive).

Source

pub fn angle(&self) -> T

Returns this vector’s angle with respect to the positive X axis, or the Vec2::RIGHT vector, in radians.

assert_eq!(Vec2::RIGHT.angle(), 0.0);
assert_eq!(Vec2::DOWN.angle(), PI / 2.0); // 90 degrees
assert_eq!(Vec2::new(1.0, -1.0).angle(), -PI / 4.0); // -45 degrees
Source

pub fn cross(&self, with: &Self) -> T

Returns the cross product of self and with.

Source

pub fn distance_to(&self, to: &Self) -> T

Returns the distance from self to to.

Source

pub fn dot(&self, with: &Self) -> T

Returns the dot product of self and with.

Source

pub fn length(&self) -> T

Returns the length(magnitude) of self.

assert_eq!(Vec2::splat(10.0).length(), 10.0 * 2.0f32.sqrt());
Source

pub fn length_squared(&self) -> T

Returns the squared length of self. Faster than Self::length.

assert_eq!(Vec2::splat(10.0).length_squared(), 200.0);
Source

pub fn limit_length(self, len: T) -> Self

Returns the vector with a new maximum length.

assert!(Vec2::splat(10.).limit_length(1.0).approx_eq(Vec2::splat(1.0 / 2.0f32.sqrt())));
assert!(Vec2::splat(10.).limit_length(5.0).approx_eq(Vec2::splat(1.0 / 2.0f32.sqrt() * 5.0)));
Source

pub fn normalized(self) -> Self

Returns the result of scaling the vector to unit length. Equivalent to v / v.length().

Note: This function may struggle with denormal values.

assert!(Vec2::RIGHT.normalized().approx_eq(Vec2::RIGHT));
assert!(Vec2::splat(1.0).normalized().approx_eq(Vec2::splat(0.5f32.sqrt())));
Source

pub fn rotated(self, angle: T) -> Self

Rotates this vector by angle radians.

let v = Vec2::new(1.2, 3.4);
assert!(v.rotated(TAU).approx_eq(Vec2::new(1.2, 3.4))); // full circle rotation
assert!(v.rotated(TAU / 4.0).approx_eq(Vec2::new(-3.4, 1.2)));
assert!(v.rotated(TAU / 3.0).approx_eq(Vec2::new(-3.5444863, -0.6607695)));
assert!(v.rotated(TAU / 2.0).approx_eq(v.rotated(TAU / -2.0)));
Source§

impl<T: Rounding> Vector2<T>

Source

pub fn ceil(self) -> Self

Returns a new vector with all components rounded up (towards positive infinity).

Source

pub fn floor(self) -> Self

Returns a new vector with all components rounded down (towards negative infinity).

Trait Implementations§

Source§

impl<T: Copy + add<T, Output = T>> Add<&T> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &T) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Copy + add<T, Output = T>> Add<&Vector2<T>> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Vector2<T>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Copy + add<T, Output = T>> Add<T> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: T) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: add<T, Output = T>> Add for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Vector2<T>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Copy + add_assign<T>> AddAssign<&T> for Vector2<T>

Source§

fn add_assign(&mut self, rhs: &T)

Performs the += operation. Read more
Source§

impl<T: Copy + add_assign<T>> AddAssign<&Vector2<T>> for Vector2<T>

Source§

fn add_assign(&mut self, rhs: &Vector2<T>)

Performs the += operation. Read more
Source§

impl<T: Copy + add_assign<T>> AddAssign<T> for Vector2<T>

Source§

fn add_assign(&mut self, rhs: T)

Performs the += operation. Read more
Source§

impl<T: add_assign<T>> AddAssign for Vector2<T>

Source§

fn add_assign(&mut self, rhs: Vector2<T>)

Performs the += operation. Read more
Source§

impl<T: Clone> Clone for Vector2<T>

Source§

fn clone(&self) -> Vector2<T>

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Vector2<T>

Source§

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

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

impl<T: Default> Default for Vector2<T>

Source§

fn default() -> Vector2<T>

Returns the “default value” for a type. Read more
Source§

impl<T: Copy + div<T, Output = T>> Div<&T> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &T) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: Copy + div<T, Output = T>> Div<&Vector2<T>> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &Vector2<T>) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: Copy + div<T, Output = T>> Div<T> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: T) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: div<T, Output = T>> Div for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Vector2<T>) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: Copy + div_assign<T>> DivAssign<&T> for Vector2<T>

Source§

fn div_assign(&mut self, rhs: &T)

Performs the /= operation. Read more
Source§

impl<T: Copy + div_assign<T>> DivAssign<&Vector2<T>> for Vector2<T>

Source§

fn div_assign(&mut self, rhs: &Vector2<T>)

Performs the /= operation. Read more
Source§

impl<T: Copy + div_assign<T>> DivAssign<T> for Vector2<T>

Source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
Source§

impl<T: div_assign<T>> DivAssign for Vector2<T>

Source§

fn div_assign(&mut self, rhs: Vector2<T>)

Performs the /= operation. Read more
Source§

impl<T> From<[T; 2]> for Vector2<T>

Source§

fn from([x, y]: [T; 2]) -> Self

Converts to this type from the input type.
Source§

impl<T> From<(T, T)> for Vector2<T>

Source§

fn from((x, y): (T, T)) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy> From<T> for Vector2<T>

Source§

fn from(value: T) -> Self

Splats the value.

Source§

impl<T> From<Vector2<T>> for (T, T)

Source§

fn from(value: Vector2<T>) -> Self

Tuplifys the vec, (x, y).

Source§

impl<T: Hash> Hash for Vector2<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Copy + mul<T, Output = T>> Mul<&T> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &T) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: Copy + mul<T, Output = T>> Mul<&Vector2<T>> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Vector2<T>) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: Copy + mul<T, Output = T>> Mul<T> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: T) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: mul<T, Output = T>> Mul for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vector2<T>) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: Copy + mul_assign<T>> MulAssign<&T> for Vector2<T>

Source§

fn mul_assign(&mut self, rhs: &T)

Performs the *= operation. Read more
Source§

impl<T: Copy + mul_assign<T>> MulAssign<&Vector2<T>> for Vector2<T>

Source§

fn mul_assign(&mut self, rhs: &Vector2<T>)

Performs the *= operation. Read more
Source§

impl<T: Copy + mul_assign<T>> MulAssign<T> for Vector2<T>

Source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
Source§

impl<T: mul_assign<T>> MulAssign for Vector2<T>

Source§

fn mul_assign(&mut self, rhs: Vector2<T>)

Performs the *= operation. Read more
Source§

impl<T: Neg<Output = T>> Neg for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<T: Ord> Ord for Vector2<T>

Source§

fn cmp(&self, other: &Vector2<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for Vector2<T>

Source§

fn eq(&self, other: &Vector2<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for Vector2<T>

Source§

fn partial_cmp(&self, other: &Vector2<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Copy + rem<T, Output = T>> Rem<&T> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: &T) -> Self::Output

Performs the % operation. Read more
Source§

impl<T: Copy + rem<T, Output = T>> Rem<&Vector2<T>> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: &Vector2<T>) -> Self::Output

Performs the % operation. Read more
Source§

impl<T: Copy + rem<T, Output = T>> Rem<T> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: T) -> Self::Output

Performs the % operation. Read more
Source§

impl<T: rem<T, Output = T>> Rem for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Vector2<T>) -> Self::Output

Performs the % operation. Read more
Source§

impl<T: Copy + rem_assign<T>> RemAssign<&T> for Vector2<T>

Source§

fn rem_assign(&mut self, rhs: &T)

Performs the %= operation. Read more
Source§

impl<T: Copy + rem_assign<T>> RemAssign<&Vector2<T>> for Vector2<T>

Source§

fn rem_assign(&mut self, rhs: &Vector2<T>)

Performs the %= operation. Read more
Source§

impl<T: Copy + rem_assign<T>> RemAssign<T> for Vector2<T>

Source§

fn rem_assign(&mut self, rhs: T)

Performs the %= operation. Read more
Source§

impl<T: rem_assign<T>> RemAssign for Vector2<T>

Source§

fn rem_assign(&mut self, rhs: Vector2<T>)

Performs the %= operation. Read more
Source§

impl<T: Copy + sub<T, Output = T>> Sub<&T> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &T) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Copy + sub<T, Output = T>> Sub<&Vector2<T>> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Vector2<T>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Copy + sub<T, Output = T>> Sub<T> for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: T) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: sub<T, Output = T>> Sub for Vector2<T>

Source§

type Output = Vector2<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Vector2<T>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Copy + sub_assign<T>> SubAssign<&T> for Vector2<T>

Source§

fn sub_assign(&mut self, rhs: &T)

Performs the -= operation. Read more
Source§

impl<T: Copy + sub_assign<T>> SubAssign<&Vector2<T>> for Vector2<T>

Source§

fn sub_assign(&mut self, rhs: &Vector2<T>)

Performs the -= operation. Read more
Source§

impl<T: Copy + sub_assign<T>> SubAssign<T> for Vector2<T>

Source§

fn sub_assign(&mut self, rhs: T)

Performs the -= operation. Read more
Source§

impl<T: sub_assign<T>> SubAssign for Vector2<T>

Source§

fn sub_assign(&mut self, rhs: Vector2<T>)

Performs the -= operation. Read more
Source§

impl<T: Copy> TryFrom<&[T]> for Vector2<T>

Source§

fn try_from(value: &[T]) -> Result<Self, Self::Error>

If the slice len is 2, constructs a new vec.

Source§

type Error = ()

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

impl<T: Copy> Copy for Vector2<T>

Source§

impl<T: Eq> Eq for Vector2<T>

Source§

impl<T> StructuralPartialEq for Vector2<T>

Auto Trait Implementations§

§

impl<T> Freeze for Vector2<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Vector2<T>
where T: RefUnwindSafe,

§

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

§

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

§

impl<T> Unpin for Vector2<T>
where T: Unpin,

§

impl<T> UnwindSafe for Vector2<T>
where T: UnwindSafe,

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<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> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.