Vector2

Struct Vector2 

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

2 Dimensional vector.

Fields§

§x: F§y: F

Implementations§

Source§

impl<F: Float> Vector2<F>

Source

pub const ZERO: Self

Source

pub const ONE: Self

Source

pub const X: Self

Source

pub const Y: Self

Source

pub const XY: Self

Source§

impl<F: Float> Vector2<F>

Source

pub const fn to_complex(self) -> Complex<F>

Converts a vector to a complex number with real = x, imag = y.

Source

pub const fn from_complex(complex: Complex<F>) -> Self

Converts a complex number to a vector with x = real, y = imag.

Source

pub const fn extend(self, z: F) -> Vector3<F>

Extends the vector with z component to create a Vector3.

Source

pub fn rotate_by(&mut self, angle: F)

Rotates angle around origin by some angle angle in radians counter-clockwise.

Source

pub fn rotated_by(self, angle: F) -> Self

Returns a rotated copy of a vector. See Self::rotate_by.

Source

pub fn rotate_by_clockwise(&mut self, angle: F)

Rotates angle around origin by some angle angle in radians clockwise.

Source

pub fn rotated_by_clockwise(self, angle: F) -> Self

Returns a rotated copy of a vector. See Self::rotate_by_clockwise.

Source

pub fn max_element(&self) -> F

Returns maximum element of the vector.

Source

pub fn min_element(&self) -> F

Returns minumum element of the vector.

Source

pub fn max_index(&self) -> usize

Returns index of the maximum element. Index is in 0..=1 range.

Source

pub fn min_index(&self) -> usize

Returns index of the minumum element. Index is in 0..=1 range.

Source

pub fn reflect(&self, axis: Self) -> Self

Source§

impl<F: Float> Vector2<F>

Source

pub fn normalize(&mut self)

Normalizes vector, preserving directing and making its magnitude equal to 1.

Source

pub fn normalized(&self) -> Self

Returns normalized copy of the vector. See Self::normalize.

Source

pub fn to_array(&self) -> [F; 2]

Converts the vector to an array.

Source

pub fn from_array(array: [F; 2]) -> Self

Converts array to a vector.

Source

pub fn as_array(&self) -> &[F; 2]

Converts the vector to an array slice.

Source

pub fn as_array_mut<'a>(&'a mut self) -> &'a [F; 2]

Converts the vector to a mutable array slice.

Source

pub fn dot(&self, other: Self) -> F

Computes the dot(scalar) product between two vectors. Dot product for two normalized vector is equal to the cosine of the angle between them.

Source

pub fn dot_normalized(&self, other: Self) -> F

Computes the dot product between two vectors normalizing them beforehand.

Source

pub fn angle_to(&self, other: Self) -> F

Returns angle in radians between two vectors. Output range is: [0, pi].

Source

pub fn arc_angle_to(&self, other: Self) -> F

Returns angle in radians between two vectors that goes along circle arc counter-clockwise. Output range is: [0, 2pi].

let a = vector!(1, 1);
let b = vector!(-1, 1);
assert_eq!(a.arc_angle_to(b).to_degrees(), 90.0);
assert_eq!(b.arc_angle_to(a).to_degrees(), 270.0);
/*  (B). -- ~~~ -- .(A)
    .-~\    90.0   / ~-.
   /     \        /     \
  /        \    /        \
 |          \  /          |
 |           ()           |
 |                        |
  \         270.0        /
   \                    /
    `-.              .-'
        ~- . ___ . -~    */
Source

pub fn project_onto(&mut self, axis: Self)

Projects a vector onto another vector. Axis and the resulting vector are collinear.

Source

pub fn projected_onto(&self, axis: Self) -> Self

Returns the projected copy of the vector onto another vector. See Self::project_onto.

Source

pub fn distance_to(&self, other: Self) -> F

Computes the distance between two vectors.

Source

pub fn sqr_distance_to(&self, other: Self) -> F

Computes the squared distance between two vectors.

Source

pub fn lerp(self, end: Self, t: F) -> Self

Linearly interpolates between two vectors.

Source

pub fn inv_lerp(self, end: Self, v: Self) -> F

Inverse linear interpolation between two vectors.

Source

pub fn nlerp(self, end: Self, t: F) -> Self

Returns normalized copy of the linear interpolation between vectors.

Source

pub fn slerp(self, end: Self, t: F) -> Self

Spherically interpolates between two vectors.

Source

pub fn product(&self) -> F

Computes the product of all elements in the vector.

Source

pub fn sum(&self) -> F

Computes the sum of all elements in the vector.

Source§

impl<F: Float> Vector2<F>

Source

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

Creates new from individual components.

Source

pub const fn same(val: F) -> Self

Creates new with all components equal to val.

Source

pub const fn into_parts(self) -> (F, F)

Splits into components.

Source

pub const fn from_parts(parts: (F, F)) -> Self

Creates new from components.

Source

pub fn scale(&mut self, factor: F)

Multiplies all components by factor.

Source

pub fn scaled(self, factor: F) -> Self

Returns the scaled copy. See Self::scale.

Source

pub fn sqr_magnitude(&self) -> F

Returns squared magnitude.

Source

pub fn magnitude(&self) -> F

Returns magnitude.

Source

pub fn max(&self, other: Self) -> Self

Returns maximum component.

Source

pub fn min(&self, other: Self) -> Self

Returns minimum component.

Source

pub fn abs(self) -> Self

Returns a copy where all components are posivive.

Source

pub fn is_zero(&self) -> bool

Checks is zero with regard to EPSILON.

Trait Implementations§

Source§

impl<F: Float> Add for Vector2<F>

Source§

type Output = Vector2<F>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<F: Float> AddAssign for Vector2<F>

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<F: Clone + Float> Clone for Vector2<F>

Source§

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

Returns a duplicate 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<F: Debug + Float> Debug for Vector2<F>

Source§

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

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

impl<F: Default + Float> Default for Vector2<F>

Source§

fn default() -> Vector2<F>

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

impl<F: Float> Div<F> for Vector2<F>

Source§

type Output = Vector2<F>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: F) -> Self

Performs the / operation. Read more
Source§

impl<F: Float> DivAssign<F> for Vector2<F>

Source§

fn div_assign(&mut self, rhs: F)

Performs the /= operation. Read more
Source§

impl<F: Float> From<(F, F)> for Vector2<F>

Source§

fn from(val: (F, F)) -> Self

Converts to this type from the input type.
Source§

impl<F: Float> From<Complex<F>> for Vector2<F>

Source§

fn from(val: Complex<F>) -> Self

Converts to this type from the input type.
Source§

impl<F: Float> From<Vector2<F>> for (F, F)

Source§

fn from(val: Vector2<F>) -> Self

Converts to this type from the input type.
Source§

impl<F: Float> From<Vector2<F>> for Complex<F>

Source§

fn from(val: Vector2<F>) -> Self

Converts to this type from the input type.
Source§

impl<F: Float> Index<usize> for Vector2<F>

Source§

type Output = F

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<F: Float> IndexMut<usize> for Vector2<F>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<F: Float> Mul<Complex<F>> for Vector2<F>

Source§

type Output = Vector2<F>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Complex<F>) -> Self::Output

Performs the * operation. Read more
Source§

impl<F: Float> Mul<F> for Vector2<F>

Source§

type Output = Vector2<F>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: F) -> Self

Performs the * operation. Read more
Source§

impl<F: Float> Mul<Vector2<F>> for Matrix2<F>

Source§

type Output = Vector2<F>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<F: Float> MulAssign<F> for Vector2<F>

Source§

fn mul_assign(&mut self, rhs: F)

Performs the *= operation. Read more
Source§

impl<F: Float> Neg for Vector2<F>

Source§

type Output = Vector2<F>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl<F: PartialEq + Float> PartialEq for Vector2<F>

Source§

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

Source§

fn partial_cmp(&self, other: &Vector2<F>) -> 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<F: Float> Sub for Vector2<F>

Source§

type Output = Vector2<F>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<F: Float> SubAssign for Vector2<F>

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<F: Float> Zeroable for Vector2<F>

Source§

fn zeroed() -> Self

Source§

impl<F: Copy + Float> Copy for Vector2<F>

Source§

impl<F: Float> Pod for Vector2<F>

Source§

impl<F: Float> StructuralPartialEq for Vector2<F>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<F> UnwindSafe for Vector2<F>
where F: 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> CheckedBitPattern for T
where T: AnyBitPattern,

Source§

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

If this function returns true, then it must be valid to reinterpret bits as &Self.
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<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.
Source§

impl<T> AnyBitPattern for T
where T: Pod,

Source§

impl<T> NoUninit for T
where T: Pod,