Struct perpendicular::Vector[][src]

pub struct Vector<T, const DIM: usize> { /* fields omitted */ }

Implementations

Create a new 4D vector

let v = Vector::new4(1, 2, 3, 4);

assert_eq!(v.dimensions(), 4);

Create a new 2D vector

let v = Vector::new3(1, 2, 3);

assert_eq!(v.dimensions(), 3);

Create a new 2D vector

let v = Vector::new2(1, 2);

assert_eq!(v.dimensions(), 2);

Create a new Vector


let v = Vector::new([1, 2]);

Try to create a vector from the elements provided (in the form of any type which implements IntoIterator). Returns None when the number of items in the iterator do no much the dimension of the desired vector.


assert_eq!(Vector::try_new(vec![1, 2]), Some(Vector::new2(1, 2)));
assert_eq!(Vector::try_new(vec![1, 2, 3]), Option::<Vector<_, 2>>::None);
assert_eq!(Vector::try_new(vec![1]), Option::<Vector<_, 2>>::None);

Like [try_new], but the iterator provided may be longer than the desired vector (extra elements are consumed). However, it may not be shorter then the desired vector.


assert_eq!(Vector::try_new_overflow(vec![1, 2]), Some(Vector::new2(1, 2)));
assert_eq!(Vector::try_new_overflow(vec![1, 2, 3]), Some(Vector::new2(1, 2)));
assert_eq!(Vector::try_new_overflow(vec![1, 2, 3]), Some(Vector::new3(1, 2, 3)));
assert_eq!(Vector::try_new_overflow(vec![1]), Option::<Vector<_, 2>>::None);

Get the number of dimensions this vector has

let v = Vector::new([1, 2, 3, 4]);

assert_eq!(v.dimensions(), 4);

get a reference to the nth item in the vector


let mut v = Vector::new2(1, 2);
assert_eq!(v.get(0), Some(&1));
assert_eq!(v.get(1), Some(&2));
assert_eq!(v.get(2), None);

get a mutable reference to the nth item in the vector


let mut v = Vector::new2(1, 2);
assert_eq!(v.get_mut(0), Some(&mut 1));
assert_eq!(v.get_mut(1), Some(&mut 2));
assert_eq!(v.get_mut(2), None);

Create an iterator over references to items in the vector


let v = Vector::new2(1, 2);
let mut i = v.iter();
assert_eq!(i.next(), Some(&1));
assert_eq!(i.next(), Some(&2));
assert_eq!(i.next(), None);

Create an iterator over mutable references to items in the vector


let mut v = Vector::new2(1, 2);
let mut i = v.iter_mut();
assert_eq!(i.next(), Some(&mut 1));
assert_eq!(i.next(), Some(&mut 2));
assert_eq!(i.next(), None);

let mut v = Vector::new2(1, 2);
{
    let mut i = v.iter_mut();
    *i.next().unwrap() = 4;
}
assert_eq!(v.get(0), Some(&4))

Scale a vector by a scalar, multiplying each element by n.


let v = Vector::new([1, 2, 3]);

assert_eq!(v.scale(2), Vector::new([2, 4, 6]));
assert_eq!(v.scale(3), Vector::new([3, 6, 9]));

Unscale a vector by a scalar. This divides every element by n.


let v = Vector::new([4, 8, 16]);

assert_eq!(v.unscale(2), Vector::new([2, 4, 8]));
assert_eq!(v.unscale(4), Vector::new([1, 2, 4]));

Create a vector filled with the zero value of T (according to num)


let v = Vector::new((0, 0, 0));
assert_eq!(Vector::zero(), v);

Create a vector filled with the one value of T (according to num)


let v = Vector::new((1, 1, 1));
assert_eq!(Vector::one(), v);

Calculate the magnitude of this vector


let mut v = Vector::new2(3, 4);

assert_eq!(v.magnitude(), 5.0)

Create a new vector with the same direction but another magnitude

let mut v = Vector::new2(3, 4);

assert_eq!(v.with_magnitude(10), Vector::new((6.0, 8.0)))

Normalizes the vector. Sets the magnitude to 1.

let mut v = Vector::new2(3, 4);

assert_eq!(v.normalize(), Vector::new((3.0/5.0, 4.0/5.0)))

Limit the magnitude of a vector. If the magnitude is less than the limit nothing changes (except all values are cast to floats). If the magnitude is larger than the limit, the magnitude is set to this limit.


assert_eq!(Vector::new2(3, 4).limit(10), Vector::new((3.0, 4.0)));
assert_eq!(Vector::new2(9, 12).limit(10), Vector::new((6.0, 8.0)));

Calculates the angle between two vectors (in radians)


let mut v1 = Vector::new2(0, 1);
let mut v2 = Vector::new2(1, 0);

assert_eq!(v1.angle(&v2).to_degrees(), 90.0)

Calculate the distance from this vector to another vector


let mut v1 = Vector::new2(0, 0);
let mut v2 = Vector::new2(3, 4);

assert_eq!(v1.distance(&v2), 5.0)

Calculate the dot product of this vector


let v1 = Vector::new((1, 2));
let v2 = Vector::new((&2, &1));

assert_eq!(v1.dot(&v2), 4)

Find if the angle between two vectors is 90 degrees


let v1 = Vector::new((0, 1));
let v2 = Vector::new((1, 0));
let v3 = Vector::new((1, 1));

assert!(v1.perpendicular(&v2));
assert!(!v1.perpendicular(&v3));

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Turn a vector into a tuple Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The returned type after indexing.

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

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Map an operation over every element of the vector Read more

Map an operation over every element of the vector Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the - operator.

Performs the unary - operation. Read more

The resulting type after applying the - operator.

Performs the unary - operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

The resulting type after applying the % operator.

Performs the % operation. Read more

The resulting type after applying the % operator.

Performs the % operation. Read more

The resulting type after applying the % operator.

Performs the % operation. Read more

The resulting type after applying the % operator.

Performs the % operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.