Vec2

Struct Vec2 

Source
pub struct Vec2<T> {
    pub x: T,
    pub y: T,
}

Fields§

§x: T§y: T

Implementations§

Source§

impl<T: Zero> Vec2<T>

Source

pub fn zero() -> Self

Source

pub fn is_zero(&self) -> bool

Source§

impl<T> Vec2<T>

Source

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

Create a new vec2 with x and y coordinates.

§Examples
use reda_geometry::base::Vec2;
let a = Vec2::new(2, 3);
assert_eq!(a.x, 2);
assert_eq!(a.y, 3);
Source§

impl<T: Copy + Zero + PartialOrd + Sub<Output = T>> Vec2<T>

Source

pub fn norm1(&self) -> T

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

§Examples
use reda_geometry::base::Vec2;
let a = Vec2::new(-2, 3);
assert_eq!(a.norm1(), 5);
Source§

impl<T: Copy + Zero + PartialOrd + Mul<Output = T> + Sub<Output = T>> Vec2<T>

Source

pub fn orientation_of(&self, other: Self) -> Orientation

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

§Examples
use reda_geometry::base::Vec2;
use reda_geometry::base::Orientation;

let a = Vec2::new(1, 0);
let b = Vec2::new(1, 1);
let c = Vec2::new(1, -1);
let d = Vec2::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);
Source§

impl<T: Copy + Mul<Output = T> + Sub<Output = T>> Vec2<T>

Source

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

Calculate cross product.

§Examples
use reda_geometry::base::Vec2;

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

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

impl<T: Copy + Mul<Output = T> + Add<Output = T>> Vec2<T>

Source

pub fn norm2_squared(&self) -> T

Get squared 2-norm of vec2.

§Examples
use reda_geometry::base::Vec2;
let a = Vec2::new(2, 3);
assert_eq!(a.norm2_squared(), 2*2+3*3);
Source

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

Calculate scalar product.

§Examples
use reda_geometry::base::Vec2;

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

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

impl<T> Vec2<T>
where T: Copy + Float + Mul<Output = T> + Add<Output = T>,

Source

pub fn norm2(&self) -> T

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

§Examples
use reda_geometry::base::Vec2;
let a = Vec2::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);
Source

pub fn normalized(&self) -> Self

Return a vec2 with the same direction but length 1.

§Panics

Panics if the vec2 has length 0.

Source§

impl<T: Copy + NumCast> Vec2<T>

Source

pub fn length<F: Float>(&self) -> F

Calculate length of vec2.

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

§Examples
use reda_geometry::base::Vec2;
let a = Vec2::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);
Source§

impl<T> Vec2<T>
where T: Copy + NumCast,

Source

pub fn to_float<F>(&self) -> Vec2<F>
where F: Float + NumCast,

Convert vec2 into a vec2 with floating point data type.

Trait Implementations§

Source§

impl<T> Add for Vec2<T>
where T: Add<Output = 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 for Vec2<T>
where T: AddAssign,

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 for Vec2<T>
where T: Debug,

Source§

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

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

impl<T: Default> Default for Vec2<T>

Source§

fn default() -> Vec2<T>

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

impl<T> Display for Vec2<T>
where T: Display + Copy,

Source§

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

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

impl<T, D> Div<D> for Vec2<T>
where T: Copy + Div<D, Output = T>, D: Copy,

Scalar division.

Source§

type Output = Vec2<T>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<T, D> DivAssign<D> for Vec2<T>
where T: Copy + DivAssign<D>, D: Copy,

Assigning scalar division.

Source§

fn div_assign(&mut self, rhs: D)

Performs the /= operation. Read more
Source§

impl<'a, T: Copy> From<&'a (T, T)> for Vec2<T>

Source§

fn from(coords: &'a (T, T)) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy> From<&Point<T>> for Vec2<T>

Source§

fn from(p: &Point<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy> From<&Vec2<T>> for (T, T)

Source§

fn from(v: &Vec2<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy> From<&Vec2<T>> for Point<T>

Source§

fn from(v: &Vec2<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T: Copy> From<&'a Vec2<T>> for Vec2<T>

Source§

fn from(v: &'a Vec2<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy> From<[T; 2]> for Vec2<T>

Source§

fn from(coords: [T; 2]) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy> From<(T, T)> for Vec2<T>

Source§

fn from(coords: (T, T)) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Point<T>> for Vec2<T>

Source§

fn from(p: Point<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy> From<Vec2<T>> for (T, T)

Source§

fn from(v: Vec2<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Vec2<T>> for Point<T>

Source§

fn from(v: Vec2<T>) -> Self

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, M> Mul<M> for Vec2<T>
where T: Mul<M, Output = T>, M: Copy,

Scalar multiplication.

Source§

type Output = Vec2<T>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<T, M> MulAssign<M> for Vec2<T>
where T: Copy + MulAssign<M>, M: Copy,

In-place scalar multiplication.

Source§

fn mul_assign(&mut self, rhs: M)

Performs the *= operation. Read more
Source§

impl<T> Neg for Vec2<T>
where T: Neg<Output = 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> Sub<Vec2<T>> for Point<T>
where T: Copy + Sub<Output = T>,

Source§

type Output = Point<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Vec2<T>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T> Sub for Vec2<T>
where T: Sub<Output = T>,

Vec2 subtraction.

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 for Vec2<T>
where T: SubAssign,

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<T> Sum for Vec2<T>
where T: Zero + Add<Output = T>,

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

Source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.