Struct fixed_vectors::Vector2

source ·
pub struct Vector2<T> {
    pub x: T,
    pub y: T,
}
Expand description

Vector for holding two-dimensional values.

Example

use fixed_vectors::Vector2;
 
let mut vec2 = Vector2::new(1, 2);
vec2 += Vector2::new(1, 2);
 
assert_eq!(vec2.x, 2);
assert_eq!(vec2.y, 4);

Fields§

§x: T§y: T

Implementations§

source§

impl<T> Vector2<T>

source

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

Constructs a new vector with the specified values for each field.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(0, 0);
 
assert_eq!(vec2.x, 0);
assert_eq!(vec2.y, 0);
source

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

Consumes the vector and returns its values as an array.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(0, 0);
let array = vec2.to_array();
 
assert_eq!(array, [0, 0]);
source

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

Consumes the vector and returns a new vector with the given function applied on each field.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(1, 2)
    .map(|i| i * 2);
 
assert_eq!(vec2, Vector2::new(2, 4));
source§

impl<T: Copy> Vector2<T>

source

pub const fn from_value(value: T) -> Self

Constructs a vector using the given value as the value for all of its fields.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::from_value(0);
 
assert_eq!(vec2, Vector2::new(0, 0));
source§

impl<T: Add<Output = T> + Copy> Vector2<T>

source

pub fn add_value(self, value: T) -> Self

Adds the given value to all fields within the vector.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(0, 0).add_value(1);
 
assert_eq!(vec2, Vector2::new(1, 1));
source§

impl<T: Sub<Output = T> + Copy> Vector2<T>

source

pub fn sub_value(self, value: T) -> Self

Subtracts the given value from all fields within the vector.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(0, 0).sub_value(1);
 
assert_eq!(vec2, Vector2::new(-1, -1));
source§

impl<T: Mul<Output = T> + Copy> Vector2<T>

source

pub fn mul_value(self, value: T) -> Self

Multiplies the given value across all fields within the vector.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(1, 2).mul_value(2);
 
assert_eq!(vec2, Vector2::new(2, 4));
source§

impl<T: Div<Output = T> + Copy> Vector2<T>

source

pub fn div_value(self, value: T) -> Self

Divides the given value across all fields within the vector.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(2, 4).div_value(2);
 
assert_eq!(vec2, Vector2::new(1, 2));
source§

impl<T: FloatCore> Vector2<T>

source

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

Returns the dot product of two vectors.

Example
use fixed_vectors::Vector2;
 
let a = Vector2::new(1.0, 2.0);
let b = Vector2::new(2.0, 4.0);
let dot = a.dot(&b);
 
assert_eq!(dot, 10.0);
source

pub fn length_squared(&self) -> T

Returns the squared magnitude of vector.

Example
use fixed_vectors::Vector3;
 
let vec3 = Vector3::new(3.33, 2.04, 1.337);
let lsq = vec3.length_squared();
 
assert!(lsq >= 17.0);
source

pub fn floor(self) -> Self

Applies floor on all fields within the vector, converting each field to the largest integer less than or equal to its value.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(1.6, 2.3).floor();
 
assert_eq!(vec2, Vector2::new(1.0, 2.0));
source

pub fn ceil(self) -> Self

Applies ceil on all fields within the vector, converting each field to the largest integer greater than or equal to its value.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(1.6, 2.3).ceil();
 
assert_eq!(vec2, Vector2::new(2.0, 3.0));
source

pub fn round(self) -> Self

Applies round on all fields within the vector, converting each field’s value to its nearest integer.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(1.6, 2.3).round();
 
assert_eq!(vec2, Vector2::new(2.0, 2.0));
source

pub fn abs(self) -> Self

Applies abs on all fields within the vector, converting each field to their absolute value.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(-2.6, 2.3).abs();
 
assert_eq!(vec2, Vector2::new(2.6, 2.3));
source

pub fn trunc(self) -> Self

Applies trunc on all fields within the vector, converting each field’s value to their integer parts.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(-2.6, 2.3).trunc();
 
assert_eq!(vec2, Vector2::new(-2.0, 2.0));
source

pub fn fract(self) -> Self

Applies fract on all fields within the vector, converting each field’s value to their fractional parts.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(-2.5, 2.25).fract();
 
assert_eq!(vec2, Vector2::new(-0.5, 0.25));
source

pub fn powi(self, n: i32) -> Self

Applies powi on all fields within the vector, raising each field’s value to an integer power.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(2.0, 4.0).powi(2);
 
assert_eq!(vec2, Vector2::new(4.0, 16.0));
source

pub fn lerp(self, to: Self, weight: T) -> Self

Linearly interpolates between two Vectors by a normalized weight.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(1.0, 2.0).lerp(
    Vector2::new(2.0, 3.0), 1.0
);
 
assert_eq!(vec2, Vector2::new(2.0, 3.0));
source§

impl Vector2<f32>

source

pub fn sqrt_short(self) -> Self

Consumes the vector and returns it with all of its fields converted to their square-root as a f32.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(64.0, 25.0).sqrt_short();
 
assert_eq!(vec2, Vector2::new(8.0, 5.0));
source

pub fn length_short(&self) -> f32

Returns the magnitude of the vector as a f32.

Example
use fixed_vectors::Vector3;
 
let vec3 = Vector3::new(1.5, 2.0, 3.33);
let length = vec3.length_short();
 
assert!(length < 4.2);
source

pub fn normalized_short(self) -> Self

Consumes the vector and returns it as normalized vector of f32s.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(14.3, 7.9).normalized_short();
 
assert!(vec2.x < 1.0);
assert!(vec2.y < 1.0);
source§

impl Vector2<f64>

source

pub fn sqrt(self) -> Self

Consumes the vector and returns it with all of its fields converted to their square-root as a f64.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(64.0, 25.0).sqrt();
 
assert_eq!(vec2, Vector2::new(8.0, 5.0));
source

pub fn length(&self) -> f64

Returns the magnitude of the vector as a f64.

Example
use fixed_vectors::Vector3;
 
let vec3 = Vector3::new(1.5, 2.0, 3.33);
let length = vec3.length();
 
assert!(length < 4.2);
source

pub fn normalized(self) -> Self

Consumes the vector and returns it as normalized vector of f64s.

Example
use fixed_vectors::Vector2;
 
let vec2 = Vector2::new(14.3, 7.9).normalized();
 
assert!(vec2.x < 1.0);
assert!(vec2.y < 1.0);

Trait Implementations§

source§

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

§

type Output = Vector2<T>

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl<T: AddAssign> AddAssign<Vector2<T>> for Vector2<T>

source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
source§

impl<T: Clone> Clone for Vector2<T>

source§

fn clone(&self) -> Self

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

source§

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

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

impl<T: Default> Default for Vector2<T>

source§

fn default() -> Self

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

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

§

type Output = Vector2<T>

The resulting type after applying the / operator.
source§

fn div(self, other: Self) -> Self::Output

Performs the / operation. Read more
source§

impl<T: DivAssign> DivAssign<Vector2<T>> for Vector2<T>

source§

fn div_assign(&mut self, other: Self)

Performs the /= operation. Read more
source§

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

§

type Output = Vector2<T>

The resulting type after applying the * operator.
source§

fn mul(self, other: Self) -> Self::Output

Performs the * operation. Read more
source§

impl<T: MulAssign> MulAssign<Vector2<T>> for Vector2<T>

source§

fn mul_assign(&mut self, other: Self)

Performs the *= operation. Read more
source§

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

§

type Output = Vector2<T>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<T: PartialEq> PartialEq<Vector2<T>> for Vector2<T>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Rem<Output = T>> Rem<Vector2<T>> for Vector2<T>

§

type Output = Vector2<T>

The resulting type after applying the % operator.
source§

fn rem(self, other: Self) -> Self::Output

Performs the % operation. Read more
source§

impl<T: RemAssign> RemAssign<Vector2<T>> for Vector2<T>

source§

fn rem_assign(&mut self, other: Self)

Performs the %= operation. Read more
source§

impl<T: Sub<Output = T>> Sub<Vector2<T>> for Vector2<T>

§

type Output = Vector2<T>

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl<T: SubAssign> SubAssign<Vector2<T>> for Vector2<T>

source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
source§

impl<T: Eq> Eq for Vector2<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Vector2<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

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

Mutably borrows from an owned value. 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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, Rhs> NumAssignOps<Rhs> for Twhere T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>,

source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for Twhere T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,