vec2

Struct vec2 

Source
#[repr(C)]
pub struct vec2<T>(pub T, pub T);
Expand description

2 dimensional vector.

Tuple Fields§

§0: T§1: T

Implementations§

Source§

impl<T> vec2<T>

Source

pub fn extend(self, z: T) -> vec3<T>

Extend into a 3-d vector.

§Examples
assert_eq!(vec2(1, 2).extend(3), vec3(1, 2, 3));
Source

pub fn map<U, F: Fn(T) -> U>(self, f: F) -> vec2<U>

Map every component (coordinate)

Source

pub fn zip<U>(self, v: vec2<U>) -> vec2<(T, U)>

Zip two vectors together

Source§

impl<T: Clone> vec2<T>

Source

pub fn splat(value: T) -> Self

Construct a vector with all components set to specified value

Source§

impl<T: UNum> vec2<T>

Source

pub const ZERO: Self

A zero 2-d vector

Source

pub const UNIT_X: Self

A unit X

Source

pub const UNIT_Y: Self

A unit Y

Source§

impl<T: Num> vec2<T>

Source

pub fn dot(a: Self, b: Self) -> T

Calculate dot product of two vectors.

§Examples
assert_eq!(vec2::dot(vec2(1, 2), vec2(3, 4)), 11);
Source

pub fn skew(a: Self, b: Self) -> T

Calculate skew product of two vectors.

§Examples
assert_eq!(vec2::skew(vec2(1, 2), vec2(3, 4)), -2);
Source§

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

Source

pub fn rotate_90(self) -> Self

Rotate a vector by 90 degrees counter clockwise.

§Examples
let v = vec2(3.0, 4.0);
assert_eq!(v.rotate_90(), vec2(-4.0, 3.0));
Source§

impl<T: Float> vec2<T>

Source

pub fn normalize(self) -> Self

Normalize a vector.

§Examples
let v: vec2<f64> = vec2(1.0, 2.0);
assert!((v.normalize().len() - 1.0).abs() < 1e-5);
Source

pub fn normalize_or_zero(self) -> Self

Normalizes a vector unless its length its approximately 0. Can be used to avoid division by 0.

§Examples
let v = vec2(1.0, 2.0);
assert_eq!(v.normalize_or_zero(), v.normalize());
let v = vec2(1e-10, 1e-10);
assert_eq!(v.normalize_or_zero(), vec2::ZERO);
Source

pub fn len(self) -> T

Calculate length of a vector.

§Examples
let v = vec2(3.0, 4.0);
assert_eq!(v.len(), 5.0);
Source

pub fn len_sqr(self) -> T

Calculate squared length of this vector

Source

pub fn rotate(self, angle: Angle<T>) -> Self

Rotate a vector by a given angle.

§Examples
let v = vec2(1.0, 2.0);
assert!((v.rotate(Angle::from_radians(std::f32::consts::FRAC_PI_2)) - vec2(-2.0, 1.0)).len() < 1e-5);
Source

pub fn clamp_len(self, len_range: impl FixedRangeBounds<T>) -> Self

Clamp vector’s length. Note that the range must be inclusive.

§Examples
let v = vec2(1.0, 2.0);
assert_eq!(v.clamp_len(..=1.0), v.normalize());
Source

pub fn clamp_coordinates( self, x_range: impl FixedRangeBounds<T>, y_range: impl FixedRangeBounds<T>, ) -> Self

Clamp vector in range. Note the range must be inclusive.

§Examples
let v = vec2(1.0, 2.0);
assert_eq!(v.clamp_coordinates(.., 0.0..=1.0), vec2(1.0, 1.0));
Source

pub fn clamp_aabb(self, aabb: Aabb2<T>) -> Self

Clamp vector by aabb corners.

§Examples
let v = vec2(0.5, 2.0);
let min = vec2(0.0, 0.0);
let max = vec2(1.0, 1.0);
let aabb = Aabb2::from_corners(min, max);
assert_eq!(v.clamp_aabb(aabb), vec2(0.5, 1.0));
Source

pub fn arg(self) -> Angle<T>

Get an angle between the positive direction of the x-axis.

§Examples
let v = vec2(0.0, 1.0);
assert_eq!(v.arg().as_radians(), std::f32::consts::FRAC_PI_2);
Source

pub fn transform(self, transform: mat3<T>) -> Self

Apply transformation matrix

Source

pub fn aspect(self) -> T

Calculate aspect ratio (x / y)

Trait Implementations§

Source§

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

Source§

type Output = vec2<T>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<T: AddAssign> AddAssign for vec2<T>

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
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> Deref for vec2<T>

Source§

type Target = XY<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &XY<T>

Dereferences the value.
Source§

impl<T> DerefMut for vec2<T>

Source§

fn deref_mut(&mut self) -> &mut XY<T>

Mutably dereferences the value.
Source§

impl<'de, T> Deserialize<'de> for vec2<T>
where T: Deserialize<'de>,

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<T: Display> Display for vec2<T>

Source§

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

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

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

Source§

type Output = vec2<T>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<T: Div<Output = T>> Div for vec2<T>

Source§

type Output = vec2<T>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

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

Source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
Source§

impl<T: DivAssign> DivAssign for vec2<T>

Source§

fn div_assign(&mut self, rhs: Self)

Performs the /= operation. Read more
Source§

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

Source§

fn from(v: [T; 2]) -> vec2<T>

Converts to this type from the input type.
Source§

impl<T: Hash> Hash for vec2<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<Output = T>> Mul<T> for vec2<T>

Source§

type Output = vec2<T>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<vec2<f32>> for f32

Source§

type Output = vec2<f32>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: vec2<f32>) -> vec2<f32>

Performs the * operation. Read more
Source§

impl Mul<vec2<f64>> for f64

Source§

type Output = vec2<f64>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: vec2<f64>) -> vec2<f64>

Performs the * operation. Read more
Source§

impl Mul<vec2<i16>> for i16

Source§

type Output = vec2<i16>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: vec2<i16>) -> vec2<i16>

Performs the * operation. Read more
Source§

impl Mul<vec2<i32>> for i32

Source§

type Output = vec2<i32>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: vec2<i32>) -> vec2<i32>

Performs the * operation. Read more
Source§

impl Mul<vec2<i64>> for i64

Source§

type Output = vec2<i64>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: vec2<i64>) -> vec2<i64>

Performs the * operation. Read more
Source§

impl Mul<vec2<i8>> for i8

Source§

type Output = vec2<i8>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: vec2<i8>) -> vec2<i8>

Performs the * operation. Read more
Source§

impl Mul<vec2<isize>> for isize

Source§

type Output = vec2<isize>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: vec2<isize>) -> vec2<isize>

Performs the * operation. Read more
Source§

impl Mul<vec2<u16>> for u16

Source§

type Output = vec2<u16>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: vec2<u16>) -> vec2<u16>

Performs the * operation. Read more
Source§

impl Mul<vec2<u32>> for u32

Source§

type Output = vec2<u32>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: vec2<u32>) -> vec2<u32>

Performs the * operation. Read more
Source§

impl Mul<vec2<u64>> for u64

Source§

type Output = vec2<u64>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: vec2<u64>) -> vec2<u64>

Performs the * operation. Read more
Source§

impl Mul<vec2<u8>> for u8

Source§

type Output = vec2<u8>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: vec2<u8>) -> vec2<u8>

Performs the * operation. Read more
Source§

impl Mul<vec2<usize>> for usize

Source§

type Output = vec2<usize>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: vec2<usize>) -> vec2<usize>

Performs the * operation. Read more
Source§

impl<T: Mul<Output = T>> Mul for vec2<T>

Source§

type Output = vec2<T>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

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

Source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
Source§

impl<T: MulAssign> MulAssign for vec2<T>

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

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

Source§

type Output = vec2<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
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> Serialize for vec2<T>
where T: Serialize,

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<T: Sub<Output = T>> Sub for vec2<T>

Source§

type Output = vec2<T>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<T: SubAssign> SubAssign for vec2<T>

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,