Struct libreda_db::prelude::Vector
source · [−]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
sourceimpl<T> Vector<T> where
T: Zero + PartialOrd<T> + Mul<T, Output = T> + Sub<T, Output = T> + Copy,
impl<T> Vector<T> where
T: Zero + PartialOrd<T> + Mul<T, Output = T> + Sub<T, Output = T> + Copy,
sourcepub fn orientation_of(&self, other: Vector<T>) -> Orientation
pub fn orientation_of(&self, other: Vector<T>) -> Orientation
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);
sourceimpl<T> Vector<T> where
T: Mul<T, Output = T> + Add<T, Output = T> + Copy,
impl<T> Vector<T> where
T: Mul<T, Output = T> + Add<T, Output = T> + Copy,
sourcepub fn norm2_squared(&self) -> T
pub fn norm2_squared(&self) -> T
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);
sourceimpl<T> Vector<T> where
T: Mul<T, Output = T> + Sub<T, Output = T> + Copy,
impl<T> Vector<T> where
T: Mul<T, Output = T> + Sub<T, Output = T> + Copy,
sourcepub fn cross_prod(&self, other: Vector<T>) -> T
pub fn cross_prod(&self, other: Vector<T>) -> T
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);
sourceimpl<T> Vector<T> where
T: CoordinateType + NumCast,
impl<T> Vector<T> where
T: CoordinateType + NumCast,
sourcepub fn cast_to_float<F>(&self) -> Vector<F> where
F: CoordinateType + Float + NumCast,
pub fn cast_to_float<F>(&self) -> Vector<F> where
F: CoordinateType + Float + NumCast,
Convert vector into a vector with floating point data type.
sourceimpl<T> Vector<T> where
T: Float<Output = T, Output = T> + Mul<T> + Add<T> + Copy,
impl<T> Vector<T> where
T: Float<Output = T, Output = T> + Mul<T> + Add<T> + Copy,
sourcepub fn norm2(&self) -> T
pub fn norm2(&self) -> T
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);
sourcepub fn normalized(&self) -> Vector<T>
pub fn normalized(&self) -> Vector<T>
sourceimpl<T> Vector<T> where
T: Copy + NumCast,
impl<T> Vector<T> where
T: Copy + NumCast,
sourcepub fn length<F>(&self) -> F where
F: Float,
pub fn length<F>(&self) -> F where
F: Float,
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
sourceimpl<T> AddAssign<Vector<T>> for Vector<T> where
T: AddAssign<T>,
impl<T> AddAssign<Vector<T>> for Vector<T> where
T: AddAssign<T>,
sourcefn add_assign(&mut self, rhs: Vector<T>)
fn add_assign(&mut self, rhs: Vector<T>)
Performs the +=
operation. Read more
sourceimpl<'de, T> Deserialize<'de> for Vector<T> where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Vector<T> where
T: Deserialize<'de>,
sourcefn deserialize<__D>(
__deserializer: __D
) -> Result<Vector<T>, <__D as Deserializer<'de>>::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D
) -> Result<Vector<T>, <__D as Deserializer<'de>>::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<T, D> DivAssign<D> for Vector<T> where
T: Copy + DivAssign<D>,
D: Copy,
impl<T, D> DivAssign<D> for Vector<T> where
T: Copy + DivAssign<D>,
D: Copy,
Assigning scalar division.
sourcefn div_assign(&mut self, rhs: D)
fn div_assign(&mut self, rhs: D)
Performs the /=
operation. Read more
sourceimpl<T> MapPointwise<T> for Vector<T> where
T: CoordinateType,
impl<T> MapPointwise<T> for Vector<T> where
T: CoordinateType,
sourceimpl<T, M> Mul<M> for Vector<T> where
T: CoordinateType<Output = T> + Mul<M>,
M: Copy,
impl<T, M> Mul<M> for Vector<T> where
T: CoordinateType<Output = T> + Mul<M>,
M: Copy,
Scalar multiplication.
sourceimpl<T, M> MulAssign<M> for Vector<T> where
T: Copy + MulAssign<M>,
M: Copy,
impl<T, M> MulAssign<M> for Vector<T> where
T: Copy + MulAssign<M>,
M: Copy,
In-place scalar multiplication.
sourcefn mul_assign(&mut self, rhs: M)
fn mul_assign(&mut self, rhs: M)
Performs the *=
operation. Read more
sourceimpl<T> Serialize for Vector<T> where
T: Serialize,
impl<T> Serialize for Vector<T> where
T: Serialize,
sourcefn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error> where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error> where
__S: Serializer,
Serialize this value into the given Serde serializer. Read more
sourceimpl<T> SubAssign<Vector<T>> for Vector<T> where
T: SubAssign<T>,
impl<T> SubAssign<Vector<T>> for Vector<T> where
T: SubAssign<T>,
sourcefn sub_assign(&mut self, rhs: Vector<T>)
fn sub_assign(&mut self, rhs: Vector<T>)
Performs the -=
operation. Read more
sourceimpl<T> Sum<Vector<T>> for Vector<T> where
T: Zero<Output = T> + Add<T>,
impl<T> Sum<Vector<T>> for Vector<T> where
T: Zero<Output = T> + Add<T>,
Compute the sum of all vectors in the iterator. If the iterator is empty, (0, 0) is returned.
sourceimpl<T, Dst> TryCastCoord<T, Dst> for Vector<T> where
T: Copy + NumCast,
Dst: Copy + NumCast,
impl<T, Dst> TryCastCoord<T, Dst> for Vector<T> where
T: Copy + NumCast,
Dst: Copy + NumCast,
sourcefn try_cast(&self) -> Option<<Vector<T> as TryCastCoord<T, Dst>>::Output>
fn try_cast(&self) -> Option<<Vector<T> as TryCastCoord<T, Dst>>::Output>
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);
sourceimpl<T> Zero for Vector<T> where
T: Zero,
impl<T> Zero for Vector<T> where
T: Zero,
sourcefn zero() -> Vector<T>
fn zero() -> Vector<T>
Get zero-vector.
Examples
use iron_shapes::vector::{Vector, Zero};
let a = Vector::zero();
let b = Vector::new(0, 0);
assert_eq!(a, b);
impl<T> Copy for Vector<T> where
T: Copy,
impl<T> Eq for Vector<T> where
T: Eq,
impl<T> StructuralEq for Vector<T>
impl<T> StructuralPartialEq for Vector<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for Vector<T> where
T: RefUnwindSafe,
impl<T> Send for Vector<T> where
T: Send,
impl<T> Sync for Vector<T> where
T: Sync,
impl<T> Unpin for Vector<T> where
T: Unpin,
impl<T> UnwindSafe for Vector<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<S, T> RotateOrtho<T> for S where
T: Copy + Zero + Sub<T, Output = T>,
S: MapPointwise<T>,
impl<S, T> RotateOrtho<T> for S where
T: Copy + Zero + Sub<T, Output = T>,
S: MapPointwise<T>,
sourcefn rotate_ortho(&self, a: Angle) -> S
fn rotate_ortho(&self, a: Angle) -> S
Rotate the geometrical shape by a multiple of 90 degrees.