Struct Vec2

Source
pub struct Vec2<T> {
    pub x: T,
    pub y: T,
}
Expand description

A vector in 2D cartesian space.

The Vec2 struct represents a two-dimensional vector with x and y components.

Fields§

§x: T

The cartesian x coordinate.

§y: T

The cartesian y coordinate.

Implementations§

Source§

impl<T> Vec2<T>

Source

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

Construct a Vec2 from cartesian coordinates.

Source

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

Sets the vector’s x component.

Source

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

Sets the vector’s y component.

Source§

impl<T: Copy> Vec2<T>

Source

pub const fn x(&self) -> T

Returns the x component of the vector.

Source

pub const fn y(&self) -> T

Returns the y component of the vector.

Source§

impl<T: Float + Copy + Mul<Output = T>> Vec2<T>

Source

pub fn from_polar(r: T, theta: T) -> Self

Construct a Vec2 from polar coordinates.

Source§

impl<T: Float + Copy> Vec2<T>

Source

pub fn angle(&self) -> T

Returns this vector’s angle in radians relative to the origin (0, 0).

Source

pub fn length(&self) -> T

Returns this vector’s distance (magnitude) from the origin (0, 0).

Source§

impl<T: Float + Copy + Sub<Output = T>> Vec2<T>

Source

pub fn distance(&self, other: Vec2<T>) -> T

Returns the cartesian distance between one vector and another.

This operation is equivalent to (self - other).length().

Source§

impl<T: Float + Copy + Div<Output = T>> Vec2<T>

Source

pub fn unit(&self) -> Self

Returns the unit (normalized) vector.

This function creates a Vec2 with a length of 1.0 while retaining the angle of its original input.

Source§

impl<T: Float + Copy + Add<Output = T> + Sub<Output = T> + Mul<Output = T>> Vec2<T>

Source

pub fn lerp(self, other: Vec2<T>, t: T) -> Vec2<T>

Linearly interpolates between two vectors.

Source

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

Creates a new vector with its coordinates rotated by a given angle in radians.

Source§

impl<T: Float + Copy + Mul<Output = T> + Sub<Output = T>> Vec2<T>

Source

pub fn cross(&self, other: Vec2<T>) -> T

Computes the cross product between this vector and another Vec2.

Source§

impl<T: Float + Copy + Mul<Output = T> + Add<Output = T>> Vec2<T>

Source

pub fn dot(&self, other: Vec2<T>) -> T

Computes the dot product between this vector and another Vec2.

The dot product is the sum of the products of each vector’s components, and represents a measurement of how closely two vectors align with respect to angle.

Source§

impl<T: Float + Copy + Mul<Output = T> + Add<Output = T> + Div<Output = T>> Vec2<T>

Source

pub fn projected(&self, onto: Vec2<T>) -> Self

Projects one Vec2 onto onto another.

Trait Implementations§

Source§

impl<T: Add<Output = T>> Add for Vec2<T>

Source§

fn add(self, other: Vec2<T>) -> Self

Performs vector addition.

Source§

type Output = Vec2<T>

The resulting type after applying the + operator.
Source§

impl<T: AddAssign> AddAssign for Vec2<T>

Source§

fn add_assign(&mut self, other: Vec2<T>)

Performs vector add-assignment.

Source§

impl<T: Clone> Clone for Vec2<T>

Source§

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

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<T: Debug> Debug for Vec2<T>

Source§

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

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

impl<T: Default> Default for Vec2<T>

Source§

fn default() -> Vec2<T>

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

impl<T: Display> Display for Vec2<T>

Source§

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

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

impl<T: Div<Output = T> + Copy> Div<T> for Vec2<T>

Source§

fn div(self, scalar: T) -> Self

Performs scalar division.

Source§

type Output = Vec2<T>

The resulting type after applying the / operator.
Source§

impl<T: DivAssign + Copy> DivAssign<T> for Vec2<T>

Source§

fn div_assign(&mut self, scalar: T)

Performs scalar div-assignment.

Source§

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

Source§

fn from(tuple: (T, T)) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Point2<T>> for Vec2<T>

Source§

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

Converts to this type from the input type.
Source§

impl<T: Mul<Output = T> + Copy> Mul<T> for Vec2<T>

Source§

fn mul(self, scalar: T) -> Self

Performs scalar multiplication.

Source§

type Output = Vec2<T>

The resulting type after applying the * operator.
Source§

impl<T: MulAssign + Copy> MulAssign<T> for Vec2<T>

Source§

fn mul_assign(&mut self, scalar: T)

Performs scalar mul-assignment.

Source§

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

Source§

fn neg(self) -> Self

Negates each component of the vector.

Source§

type Output = Vec2<T>

The resulting type after applying the - operator.
Source§

impl<T: PartialEq> PartialEq for Vec2<T>

Source§

fn eq(&self, other: &Vec2<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: Sub<Output = T>> Sub for Vec2<T>

Source§

fn sub(self, other: Vec2<T>) -> Self

Performs vector subtraction.

Source§

type Output = Vec2<T>

The resulting type after applying the - operator.
Source§

impl<T: SubAssign> SubAssign for Vec2<T>

Source§

fn sub_assign(&mut self, other: Vec2<T>)

Performs vector sub-assignment.

Source§

impl<T: Copy> Copy for Vec2<T>

Source§

impl<T: Eq> Eq for Vec2<T>

Source§

impl<T> StructuralPartialEq for Vec2<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Vec2<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<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§

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.