Struct Vector2D

Source
pub struct Vector2D<T, Kind> {
    pub x: T,
    pub y: T,
    /* private fields */
}
Expand description

A generic vector with an X and Y component.

Fields§

§x: T§y: T

Implementations§

Source§

impl<T> Vector2D<T, Point>

Source

pub fn offset<P: ToPoint2D<T>>(self, point: P) -> Offset2D<T>
where T: Sub<Output = T>,

Returns the offset between self and point.

Order matters here, so if you are trying to get the offset needed for point A to get to point B, you would do a.offset(b).

§Examples
let a = point!(10, 40);
let b = point!(5, 60);

assert_eq!(a.offset(b), offset!(-5, 20));
Source

pub fn with_size<S: ToSize2D<T>>(self, size: S) -> Bounds2D<T>
where T: Num + Copy,

Returns a new Bounds2D using self as position, and size as the size.

§Examples
let bounds = point!(20u32, 30).with_size(size!(50; 2));

assert_eq!(bounds, bounds!(20, 30, 50, 50));
Source§

impl<T, K> Vector2D<T, K>

Source

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

Returns a new Vector2D with x and y components.

In most cases you should not call this directly, but rather use the macros to get the specialized variants.

§Examples
// Avoid doing this
let point: Vector2D<_, Point> = Vector2D::new(20, 40);

// This is better, but not great
let point: Point2D<_> = Vector2D::new(20, 40);

// This is acceptable, but...
let point = Point2D::new(20, 40);

// ...this is the preferred way
let point = point!(20, 40);
Source

pub fn splat(value: T) -> Self
where T: Copy,

Returns a new Vector2D where both components are set to value.

Prefer using the splat syntax with the specialized macros instead of calling this directly.

§Examples
// This is acceptable, but...
let offset = Offset2D::splat(5);

// ...this is the preferred way
let offset = offset!(5; 2);

assert_eq!(offset, offset!(5, 5));
Source

pub fn cast<C>(self) -> Vector2D<C, K>
where C: Copy + 'static, T: AsPrimitive<C>,

Casts self into a new Vector2D where components are the (usually inferred) input type.

Trait Implementations§

Source§

impl<T> Add<Vector2D<T, Offset>> for Bounds2D<T>
where T: Num + Copy,

Source§

type Output = Bounds2D<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Offset2D<T>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Clone, Kind: Clone> Clone for Vector2D<T, Kind>

Source§

fn clone(&self) -> Vector2D<T, Kind>

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, Kind: Debug> Debug for Vector2D<T, Kind>

Source§

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

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

impl<T: Default, Kind: Default> Default for Vector2D<T, Kind>

Source§

fn default() -> Vector2D<T, Kind>

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

impl<T, K> From<(T, T)> for Vector2D<T, K>

Source§

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

Converts to this type from the input type.
Source§

impl<T, K> From<Vector2D<T, K>> for (T, T)

Source§

fn from(vector: Vector2D<T, K>) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash, Kind: Hash> Hash for Vector2D<T, Kind>

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: PartialEq, Kind: PartialEq> PartialEq for Vector2D<T, Kind>

Source§

fn eq(&self, other: &Vector2D<T, Kind>) -> 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<Vector2D<T, Offset>> for Bounds2D<T>
where T: Num + Copy,

Source§

type Output = Bounds2D<T>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<T, K> ToVector2D<T, K> for Vector2D<T, K>

Makes it so that Vector2D itself can be used for interfaces expecting it.

Source§

fn to_vector(self) -> Vector2D<T, K>

Converts this type into a Vector2D.
Source§

impl<T, K, ToVector> Vector<T, ToVector> for Vector2D<T, K>
where ToVector: ToVector2D<T, K>,

Source§

fn dot(&self, rhs: ToVector) -> T
where T: Copy + Mul<Output = T> + Add<Output = T>,

Returns the dot product of self and rhs.

§Examples
let a = offset!(5, 10);
let b = offset!(10, 5);

assert_eq!(a.dot(b), 100);
Source§

fn cross(&self, rhs: ToVector) -> T
where T: Copy + Mul<Output = T> + Sub<Output = T>,

Returns the normal of the cross product between self and rhs.

§Examples
let a = offset!(0, 0);
let b = offset!(20, 20);

assert_eq!(a.cross(b), 0);
Source§

fn distance(&self, rhs: ToVector) -> T
where T: Signed + Copy + Add<Output = T> + Sub<Output = T>,

Returns the absolute distance between self and rhs.

§Examples
let a = offset!(10, 10);
let b = offset!(0, 0);

assert_eq!(a.distance(b), 20);
Source§

impl<T: Copy, Kind: Copy> Copy for Vector2D<T, Kind>

Source§

impl<T, Kind> StructuralPartialEq for Vector2D<T, Kind>

Auto Trait Implementations§

§

impl<T, Kind> Freeze for Vector2D<T, Kind>
where T: Freeze,

§

impl<T, Kind> RefUnwindSafe for Vector2D<T, Kind>
where T: RefUnwindSafe, Kind: RefUnwindSafe,

§

impl<T, Kind> Send for Vector2D<T, Kind>
where T: Send, Kind: Send,

§

impl<T, Kind> Sync for Vector2D<T, Kind>
where T: Sync, Kind: Sync,

§

impl<T, Kind> Unpin for Vector2D<T, Kind>
where T: Unpin, Kind: Unpin,

§

impl<T, Kind> UnwindSafe for Vector2D<T, Kind>
where T: UnwindSafe, Kind: 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> 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, 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.
Source§

impl<T, V> ToOffset2D<T> for V
where V: ToVector2D<T, Offset>,

Source§

impl<T, V> ToPoint2D<T> for V
where V: ToVector2D<T, Point>,