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

Vector defines a two dimensional vector with x and y components in the Euclidean plane.

Fields

x: T

x coordinate.

y: T

y coordinate.

Implementations

Create a new vector with x and y coordinates.

Examples
use iron_shapes::vector::Vector;
let a = Vector::new(2, 3);
assert_eq!(a.x, 2);
assert_eq!(a.y, 3);

Get 1-norm of vector, i.e. the sum of the absolute values of its components.

Examples
use iron_shapes::vector::Vector;
let a = Vector::new(-2, 3);
assert_eq!(a.norm1(), 5);

Check if other is oriented clockwise or counter-clockwise respective to self.

Examples
use iron_shapes::vector::Vector;
use iron_shapes::types::Orientation;

let a = Vector::new(1, 0);
let b = Vector::new(1, 1);
let c = Vector::new(1, -1);
let d = Vector::new(2, 0);

assert_eq!(a.orientation_of(b), Orientation::CounterClockWise);
assert_eq!(a.orientation_of(c), Orientation::ClockWise);
assert_eq!(a.orientation_of(d), Orientation::Straight);

Get squared 2-norm of vector.

Examples
use iron_shapes::vector::Vector;
let a = Vector::new(2, 3);
assert_eq!(a.norm2_squared(), 2*2+3*3);

Calculate scalar product.

Examples
use iron_shapes::vector::Vector;

let a = Vector::new(1, 2);
let b = Vector::new(3, 4);

assert_eq!(a.dot(b), 1*3 + 2*4);

Calculate cross product.

Examples
use iron_shapes::vector::Vector;

let a = Vector::new(2, 0);
let b = Vector::new(0, 2);

assert_eq!(a.cross_prod(b), 4);
assert_eq!(b.cross_prod(a), -4);

Convert vector into a vector with floating point data type.

Get 2-norm of vector (length of vector).

Examples
use iron_shapes::vector::Vector;
let a = Vector::new(2.0, 3.0);
let norm2 = a.norm2();
let norm2_sq = norm2 * norm2;
let expected = a.norm2_squared();
assert!(norm2_sq < expected + 1e-12);
assert!(norm2_sq > expected - 1e-12);

Return a vector with the same direction but length 1.

Panics

Panics if the vector has length 0.

Return the normal vector onto this vector. The normal has length 1.

Panics

Panics if the vector has length 0.

Calculate length of vector.

Similar to Vector::norm2 but does potentially return another data type for the length.

Examples
use iron_shapes::vector::Vector;
let a = Vector::new(2.0, 3.0);
let length: f64 = a.length();
let norm2_sq = length * length;
let expected = a.norm2_squared();
assert!(norm2_sq < expected + 1e-12);
assert!(norm2_sq > expected - 1e-12);

Trait Implementations

Vector addition.

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. 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

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

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

Scalar division.

The resulting type after applying the / operator.

Performs the / operation. Read more

Assigning scalar division.

Performs the /= operation. Read more

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

Performs the conversion.

Performs the conversion.

Performs the conversion.

Point wise transformation.

Scalar multiplication.

The resulting type after applying the * operator.

Performs the * operation. Read more

In-place scalar multiplication.

Performs the *= 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 !=.

Serialize this value into the given Serde serializer. Read more

Vector subtraction.

The resulting type after applying the - operator.

Performs the - operation. Read more

Subtract a vector.

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Compute the sum of all vectors in the iterator. If the iterator is empty, (0, 0) is returned.

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

Try to cast to vector of target data type.

Conversion from float to int can fail and will return None. Float values like infinity or non-a-number have no integer representation.

Examples
use iron_shapes::vector::Vector;
use iron_shapes::traits::TryCastCoord;

let v_int = Vector::new(1,2);
let maybe_v_float: Option<Vector<f64>> = v_int.try_cast();

assert_eq!(maybe_v_float, Some(Vector::new(1.0, 2.0)));

// Conversion from float to int can fail.

let w_float = Vector::new(42.0, 0. / 0.);
let maybe_w_int: Option<Vector<i32>> = w_float.try_cast();

assert_eq!(maybe_w_int, None);

Output type of the cast. This is likely the same geometrical type just with other coordinate types. Read more

Cast to target data type. Read more

Get zero-vector.

Examples
use iron_shapes::vector::{Vector, Zero};

let a = Vector::zero();
let b = Vector::new(0, 0);

assert_eq!(a, b);

Check if this is the zero-vector.

Examples
use iron_shapes::vector::{Vector, Zero};

assert!(Vector::<usize>::zero().is_zero());

Sets self to the additive identity element of Self, 0.

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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Return the geometrical object mirrored at the x axis.

Return the geometrical object mirrored at the y axis.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Rotate the geometrical shape by a multiple of 90 degrees.

Scale the geometrical shape. Scaling center is the origin (0, 0).

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)

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

Converts the given value to a String. Read more

Translate the geometrical object by a vector v.

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.