Skip to main content

Vector

Struct Vector 

Source
pub struct Vector {
    pub x: f64,
    pub y: f64,
}
Expand description

A displacement in the plane. Serialized as [x, y].

Fields§

§x: f64

X component.

§y: f64

Y component.

Implementations§

Source§

impl Vector

Source

pub const ZERO: Self

Zero vector.

Source

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

Create a vector.

Source

pub fn from_angle(angle: f64) -> Self

Unit vector at angle radians from +X, counter-clockwise.

Source

pub fn is_finite(self) -> bool

Both components are finite.

Source

pub fn length(self) -> f64

Euclidean length (overflow-safe).

Source

pub fn length_sq(self) -> f64

Squared length.

Source

pub fn dot(self, o: Self) -> f64

Dot product.

Source

pub fn cross(self, o: Self) -> f64

2D cross product (z component of the 3D cross product).

Source

pub fn normalize(self) -> Option<Self>

Unit vector in the same direction, or None for a zero/non-finite vector.

Source

pub const fn perp(self) -> Self

Rotate 90° counter-clockwise.

Source

pub fn angle(self) -> f64

Angle from +X in radians, in (-π, π].

Source

pub fn rotate(self, angle: f64) -> Self

Rotate by angle radians counter-clockwise.

Source

pub const fn to_point(self) -> Point

Point at the tip of this vector from the origin.

Trait Implementations§

Source§

impl Add for Vector

Source§

type Output = Vector

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<Vector> for Point

Source§

type Output = Point

The resulting type after applying the + operator.
Source§

fn add(self, v: Vector) -> Self

Performs the + operation. Read more
Source§

impl AddAssign for Vector

Source§

fn add_assign(&mut self, o: Self)

Performs the += operation. Read more
Source§

impl AddAssign<Vector> for Point

Source§

fn add_assign(&mut self, v: Vector)

Performs the += operation. Read more
Source§

impl Clone for Vector

Source§

fn clone(&self) -> Vector

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Vector

Source§

impl Debug for Vector

Source§

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

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

impl Default for Vector

Source§

fn default() -> Vector

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

impl<'de> Deserialize<'de> for Vector

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Div<f64> for Vector

Source§

type Output = Vector

The resulting type after applying the / operator.
Source§

fn div(self, s: f64) -> Self

Performs the / operation. Read more
Source§

impl From<Vector> for [f64; 2]

Source§

fn from(p: Vector) -> Self

Converts to this type from the input type.
Source§

impl From<[f64; 2]> for Vector

Source§

fn from(v: [f64; 2]) -> Self

Converts to this type from the input type.
Source§

impl Mul<Vector> for f64

Source§

type Output = Vector

The resulting type after applying the * operator.
Source§

fn mul(self, v: Vector) -> Vector

Performs the * operation. Read more
Source§

impl Mul<f64> for Vector

Source§

type Output = Vector

The resulting type after applying the * operator.
Source§

fn mul(self, s: f64) -> Self

Performs the * operation. Read more
Source§

impl Neg for Vector

Source§

type Output = Vector

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl PartialEq for Vector

Source§

fn eq(&self, other: &Vector) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 Serialize for Vector

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Vector

Source§

impl Sub for Vector

Source§

type Output = Vector

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<Vector> for Point

Source§

type Output = Point

The resulting type after applying the - operator.
Source§

fn sub(self, v: Vector) -> Self

Performs the - operation. Read more
Source§

impl SubAssign<Vector> for Point

Source§

fn sub_assign(&mut self, v: Vector)

Performs the -= operation. Read more

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, 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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.