alloy::math::vec

Struct Vector2

source
#[repr(C)]
pub struct Vector2 { pub x: f32, pub y: f32, }
Expand description

An owning representation of a 2-component Euclidean vector.

Like Vec2, the Vector2 object represents a Euclidean vector in 2D. Unlike the Vec2, this is an owning representation that stores the actual content of the vector.

Fields§

§x: f32

The X-component of the vector.

§y: f32

The Y-component of the vector.

Implementations§

source§

impl Vector2

source

pub const ZERO: Vector2 = _

A constant for a Null vector, which has magnitude 0 and exists at the origin.

source

pub const UNIT_X: Vector2 = _

A constant for a unit vector in the positive X-direction.

source

pub const UNIT_Y: Vector2 = _

A constant for a unit vector in the positive Y-direction.

source

pub const NEG_UNIT_X: Vector2 = _

A constant for a unit vector in the negative X-direction.

source

pub const NEG_UNIT_Y: Vector2 = _

A constant for a unit vector in the negative Y-direction.

source

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

Constructs this vector from an x and y coordinate.

§Parameters
  • x - the x-component
  • y - the y-component
source

pub const fn uniform(v: f32) -> Self

Constructs this vector with a uniform value v.

§Parameters
  • v - the value to uniformly apply
source

pub const fn from_array(array: &[f32; 2]) -> Self

Constructs this vector from a 2-component f32 array.

§Parameters
  • array - an array containing 2 f32 values.
source

pub const fn from_slice(slice: &[f32]) -> Result<Self, VecError>

Constructs this vector from a slice of floats.

This will return VecError if slice.len() is not equal to 2.

§Parameters
  • slice - the slice to read from
source

pub const unsafe fn from_slice_unchecked(slice: &[f32]) -> Self

Constructs this vector from a slice of floats.

§Parameters
  • slice - the slice to read from
§Safety

slice.len() must be greater or equal to 2, otherwise this will access an out-of-bounds entry and panic.

source

pub const unsafe fn from_ptr(ptr: *const f32) -> Self

Constructs this vector from a pointer to floating point values.

§Parameters
  • ptr - the pointer to the start of a contiguous sequence of floats
§Safety

This function requires that ptr be non-null and point to the start of a contiguous sequence of 2 f32 values.

source

pub const fn from_vec2(other: &Vec2) -> Self

Constructs this vector from a Vec2

§Parameters
  • other - the other vector
source

pub fn from_vec2i(other: &Vec2i) -> Self

Constructs this vector from a Vec2i

§Parameters
  • other - the other vector
source

pub fn from_vec2u(other: &Vec2u) -> Self

Constructs this vector from a Vec2u

§Parameters
  • other - the other vector
source

pub const fn as_vec2(&self) -> &Vec2

Returns this vector as a Vec2.

source

pub fn as_mut_vec2(&mut self) -> &mut Vec2

Returns this vector as a mutable Vec2.

source

pub const fn as_slice(&self) -> &[f32]

Returns this vector as a slice of f32.

source

pub fn as_mut_slice(&mut self) -> &mut [f32]

Returns this vector as a mutable slice of f32.

Methods from Deref<Target = Vec2>§

source

pub fn as_slice(&self) -> &[f32]

Returns this Vec2 as a slice of f32.

source

pub fn as_mut_slice(&mut self) -> &mut [f32]

Returns this Vec2 as a mutable slice of f32.

source

pub fn as_ptr(&self) -> *const f32

Returns a raw pointer to f32 of the vector’s buffer.

source

pub fn as_mut_ptr(&mut self) -> *mut f32

Returns a raw pointer to f32 of the vector’s buffer.

source

pub fn iter(&self) -> impl Iterator<Item = &f32>

Returns an iterator over the vector.

source

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut f32>

Returns a mutable iterator over the vector.

source

pub fn x(&self) -> f32

Returns the X-coordinate of this 2-component vector.

source

pub fn y(&self) -> f32

Returns the Y-coordinate of this 2-component vector.

source

pub fn x_mut(&mut self) -> &mut f32

Returns a mutable reference to the X-coordinate of this 2-component vector.

source

pub fn y_mut(&mut self) -> &mut f32

Returns a mutable reference to the Y-coordinate of this 2-component vector.

source

pub fn set_x(&mut self, x: f32)

Sets the x-component

§Parameters
  • x - the X-component
source

pub fn set_y(&mut self, y: f32)

Sets the y-component

§Parameters
  • y - the Y-component
source

pub fn set(&mut self, other: &Vec2)

Sets all the components of this vector the values from other.

§Parameters
  • other - the other Vec2 to set.
source

pub fn square_magnitude(&self) -> f32

Computes the square magnitude of this two-component vector.

source

pub fn magnitude(&self) -> f32

Computes the magnitude of this vector.

Where possible, consider using Vec2::square_magnitude as this will avoid the need to compute the square-root.

source

pub fn is_normalized(&self) -> bool

Queries whether this vector is normalized (e.g. has a magnitude of 1).

source

pub fn normalize(&mut self)

Normalizes this vector.

source

pub fn normalized(&self) -> Vector2

Returns a normalized version of this vector.

source

pub fn rotate<A: Angle>(&mut self, angle: A)

Rotates this vector around the origin by angle A.

§Parameters
  • angle - the angle to rotate
source

pub fn rotated<A: Angle>(&self, angle: A) -> Vector2

Returns a vector rotated by the specified angle

§Parameters
  • angle - the angle to rotate
source

pub fn is_finite(&self) -> bool

Returns whether all components of this Vec are finite.

§Example

Basic use:


let finite = Vector2::UNIT_X;
let not_finite = Vector2::new(f32::INFINITY, 3.0);

assert!(finite.is_finite());
assert!(!not_finite.is_finite());
source

pub fn is_infinite(&self) -> bool

Returns whether any component of this vec are infinite.

This is the inverse operation of is_finite.

§Example

Basic use:


let finite = Vector2::UNIT_X;
let not_finite = Vector2::new(f32::INFINITY, 3.0);

assert!(!finite.is_infinite());
assert!(not_finite.is_infinite());
source

pub fn is_nan(&self) -> bool

Returns whether any component of this vec are nan.

§Example

Basic use:


let not_nan = Vector2::UNIT_X;
let nan = Vector2::new(f32::NAN, 3.0);

assert!(!not_nan.is_nan());
assert!(nan.is_nan());
source

pub fn abs(&self) -> Vector2

Computes the absolute value of self

§Example

Basic use:


let vec = Vector2::new(-3.0, 4.0);
let abs = vec.abs();

assert_eq!(abs, Vector2::new(3.0, 4.0));
source

pub fn min(&self, rhs: &Vec2) -> Vector2

Computes the minimum of each vector.

§Parameters
  • lhs - the left vector
  • rhs - the right vector
source

pub fn max(&self, rhs: &Vec2) -> Vector2

Computes the maximum of each vector.

§Parameters
  • lhs - the left vector
  • rhs - the right vector

Trait Implementations§

source§

impl Add<&Vec2> for &Vector2

source§

type Output = Vector2

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl Add<&Vec2> for Vector2

source§

type Output = Vector2

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl Add<&Vector2> for &Vec2

source§

type Output = Vector2

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl Add<&Vector2> for Vector2

source§

type Output = Vector2

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl Add<Vector2> for &Vec2

source§

type Output = Vector2

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl Add<Vector2> for &Vector2

source§

type Output = Vector2

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl Add for &Vector2

source§

type Output = Vector2

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl Add for Vector2

source§

type Output = Vector2

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl AddAssign<&Vec2> for Vector2

source§

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

Performs the += operation. Read more
source§

impl AddAssign<&Vector2> for Vector2

source§

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

Performs the += operation. Read more
source§

impl AddAssign for Vector2

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl AsMut<Vec2> for Vector2

source§

fn as_mut(&mut self) -> &mut Vec2

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl AsRef<Vec2> for Vector2

source§

fn as_ref(&self) -> &Vec2

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Borrow<Vec2> for Vector2

source§

fn borrow(&self) -> &Vec2

Immutably borrows from an owned value. Read more
source§

impl BorrowMut<Vec2> for Vector2

source§

fn borrow_mut(&mut self) -> &mut Vec2

Mutably borrows from an owned value. Read more
source§

impl Clone for Vector2

source§

fn clone(&self) -> Vector2

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 Debug for Vector2

source§

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

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

impl Default for Vector2

source§

fn default() -> Vector2

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

impl Deref for Vector2

source§

type Target = Vec2

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl DerefMut for Vector2

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl Display for Vector2

source§

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

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

impl Div<f32> for &Vector2

source§

type Output = Vector2

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl Div<f32> for Vector2

source§

type Output = Vector2

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl DivAssign<f32> for Vector2

source§

fn div_assign(&mut self, rhs: f32)

Performs the /= operation. Read more
source§

impl From<&Vec2> for Vector2

source§

fn from(value: &Vec2) -> Self

Converts to this type from the input type.
source§

impl From<&Vec2i> for Vector2

source§

fn from(value: &Vec2i) -> Self

Converts to this type from the input type.
source§

impl From<&Vec2u> for Vector2

source§

fn from(value: &Vec2u) -> Self

Converts to this type from the input type.
source§

impl From<Vector2> for Depth

source§

fn from(vector: Vector2) -> Self

Converts to this type from the input type.
source§

impl From<Vector2> for Horizontal

source§

fn from(vector: Vector2) -> Self

Converts to this type from the input type.
source§

impl From<Vector2> for Vertical

source§

fn from(vector: Vector2) -> Self

Converts to this type from the input type.
source§

impl Mul<&Vector2> for f32

source§

type Output = Vector2

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl Mul<Vector2> for f32

source§

type Output = Vector2

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl Mul<f32> for &Vector2

source§

type Output = Vector2

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl Mul<f32> for Vector2

source§

type Output = Vector2

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl MulAssign<f32> for Vector2

source§

fn mul_assign(&mut self, rhs: f32)

Performs the *= operation. Read more
source§

impl PartialEq for Vector2

source§

fn eq(&self, other: &Vector2) -> 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 PartialOrd for Vector2

source§

fn partial_cmp(&self, other: &Vector2) -> 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 Rem<f32> for &Vector2

source§

type Output = Vector2

The resulting type after applying the % operator.
source§

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

Performs the % operation. Read more
source§

impl Rem<f32> for Vector2

source§

type Output = Vector2

The resulting type after applying the % operator.
source§

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

Performs the % operation. Read more
source§

impl RemAssign<f32> for Vector2

source§

fn rem_assign(&mut self, rhs: f32)

Performs the %= operation. Read more
source§

impl Sub<&Vec2> for &Vector2

source§

type Output = Vector2

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl Sub<&Vec2> for Vector2

source§

type Output = Vector2

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl Sub<&Vector2> for &Vec2

source§

type Output = Vector2

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl Sub<&Vector2> for Vector2

source§

type Output = Vector2

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl Sub<Vector2> for &Vec2

source§

type Output = Vector2

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl Sub<Vector2> for &Vector2

source§

type Output = Vector2

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl Sub for &Vector2

source§

type Output = Vector2

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl Sub for Vector2

source§

type Output = Vector2

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl SubAssign<&Vec2> for Vector2

source§

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

Performs the -= operation. Read more
source§

impl SubAssign<&Vector2> for Vector2

source§

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

Performs the -= operation. Read more
source§

impl SubAssign for Vector2

source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
source§

impl Copy for Vector2

source§

impl StructuralPartialEq for Vector2

Auto Trait Implementations§

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.