Vec2

Struct Vec2 

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

A 2D vector with generic components, suitable for mathematical operations in 2D space.

Vec2<T> represents a 2D vector with components x and y, where T is a numeric type. It supports various arithmetic operations such as addition, subtraction, multiplication, division, and negation. For T = f32, additional methods like clamp are available.

The generic type T must implement certain traits depending on the operations used:

  • For basic instantiation: T: Default.
  • For arithmetic operations: T: Add, T: Sub, T: Mul, T: Div.
  • For negation: T: Neg.

§Examples

use robomath::{Vec2, vec2};

// Create a Vec2 with f32 components
let v1 = vec2(1.0, 2.0);
let v2 = vec2(3.0, 4.0);

// Perform arithmetic operations
let sum = v1 + v2;
assert_eq!(sum, vec2(4.0, 6.0));

// Scalar multiplication
let scaled = 2.0 * v1;
assert_eq!(scaled, vec2(2.0, 4.0));

// Clamp components (only available for f32)
let clamped = v1.clamp(0.0, 1.5);
assert_eq!(clamped, vec2(1.0, 1.5));

Fields§

§x: T§y: T

Implementations§

Source§

impl Vec2<f32>

Source

pub fn clamp(&self, min: f32, max: f32) -> Vec2<f32>

Clamps the components of the vector to be within the specified range.

Each component (x, y) is clamped to the interval [min, max]. If a component is less than min, it is set to min. If it is greater than max, it is set to max.

§Arguments
  • min - The minimum value for each component.
  • max - The maximum value for each component.
§Returns

A new Vec2<f32> with components clamped to the specified range.

§Examples
use robomath::vec2;

let v = vec2(-1.0, 5.0);
let clamped = v.clamp(0.0, 2.0);
assert_eq!(clamped, vec2(0.0, 2.0));

Trait Implementations§

Source§

impl<T: Add<Output = T> + Default> Add for Vec2<T>

Source§

fn add(self, rhs: Vec2<T>) -> Vec2<T>

Adds two Vec2s component-wise.

§Arguments
  • rhs - The vector to add to self.
§Returns

A new Vec2<T> where each component is the sum of the corresponding components.

§Examples
use robomath::vec2;

let v1 = vec2(1.0, 2.0);
let v2 = vec2(3.0, 4.0);
let result = v1 + v2;
assert_eq!(result, vec2(4.0, 6.0));
Source§

type Output = Vec2<T>

The resulting type after applying the + operator.
Source§

impl<T: Clone + Default> 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 + Default> Debug for Vec2<T>

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() -> Self

Provides a default Vec2 where each component is T::default().

For numeric types, this typically means zero.

§Examples
use robomath::Vec2;

let v: Vec2<f32> = Vec2::default();
assert_eq!(v, Vec2 { x: 0.0, y: 0.0 });

let v_int: Vec2<i32> = Vec2::default();
assert_eq!(v_int, Vec2 { x: 0, y: 0 });
Source§

impl<T: Div<Output = T> + Copy + Default> Div<T> for Vec2<T>

Source§

fn div(self, rhs: T) -> Vec2<T>

Divides each component of the Vec2 by a scalar.

§Arguments
  • rhs - The scalar to divide by.
§Returns

A new Vec2<T> with each component divided by the scalar.

§Panics

Panics if rhs is zero and T does not handle division by zero gracefully (e.g., for integers). For T = f32, division by zero results in infinity or NaN as per IEEE 754.

§Examples
use robomath::vec2;

let v = vec2(4.0, 6.0);
let result = v / 2.0;
assert_eq!(result, vec2(2.0, 3.0));
Source§

type Output = Vec2<T>

The resulting type after applying the / operator.
Source§

impl Mul<Vec2<f32>> for f32

Source§

fn mul(self, rhs: Vec2<f32>) -> Vec2<f32>

Scales a Vec2<f32> by a scalar value.

Each component of the vector is multiplied by the scalar.

§Arguments
  • rhs - The vector to scale.
§Returns

A new Vec2<f32> with each component scaled by the scalar.

§Examples
use robomath::vec2;

let v = vec2(1.0, 2.0);
let scaled = 2.0 * v;
assert_eq!(scaled, vec2(2.0, 4.0));
Source§

type Output = Vec2<f32>

The resulting type after applying the * operator.
Source§

impl<T: Mul<Output = T> + Default> Mul for Vec2<T>

Source§

fn mul(self, rhs: Vec2<T>) -> Vec2<T>

Multiplies two Vec2s component-wise (element-wise multiplication).

§Arguments
  • rhs - The vector to multiply with self.
§Returns

A new Vec2<T> where each component is the product of the corresponding components.

§Examples
use robomath::vec2;

let v1 = vec2(2.0, 3.0);
let v2 = vec2(5.0, 6.0);
let result = v1 * v2;
assert_eq!(result, vec2(10.0, 18.0));
Source§

type Output = Vec2<T>

The resulting type after applying the * operator.
Source§

impl<T: Neg<Output = T> + Default> Neg for Vec2<T>

Source§

fn neg(self) -> Self::Output

Negates each component of the Vec2.

§Returns

A new Vec2<T> with each component negated.

§Examples
use robomath::vec2;

let v = vec2(1.0, -2.0);
let neg = -v;
assert_eq!(neg, vec2(-1.0, 2.0));
Source§

type Output = Vec2<T>

The resulting type after applying the - operator.
Source§

impl<T: PartialEq + Default> 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: PartialOrd + Default> PartialOrd for Vec2<T>

Source§

fn partial_cmp(&self, other: &Vec2<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Sub<Output = T> + Default> Sub for Vec2<T>

Source§

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

Subtracts two Vec2s component-wise.

§Arguments
  • rhs - The vector to subtract from self.
§Returns

A new Vec2<T> where each component is the difference of the corresponding components.

§Examples
use robomath::vec2;

let v1 = vec2(5.0, 7.0);
let v2 = vec2(1.0, 2.0);
let result = v1 - v2;
assert_eq!(result, vec2(4.0, 5.0));
Source§

type Output = Vec2<T>

The resulting type after applying the - operator.
Source§

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

Source§

impl<T: Default> 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, 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.