Vec2

Struct Vec2 

Source
pub struct Vec2 {
    pub x: f32,
    pub y: f32,
    /* private fields */
}
Expand description

A two dimensional mathematical vector

§Examples

use miscmath::prelude::*;

let a = Vec2::default();

assert!( ( a.x < 0.00001 ) && ( a.y < 0.00001 ) );

Fields§

§x: f32
§y: f32

Implementations§

Source§

impl Vec2

Source

pub fn new(x: &f32, y: &f32) -> Vec2

Generates a new instance of Vec2 initialized to chosen values and returns it

§Examples
use miscmath::prelude::*;

let a = Vec2::new( &5.6, &7.2 );

assert!( ( ( a.x - 5.6 ) < 0.00001 ) && ( ( a.y - 7.2 ) < 0.00001 ) );
Source

pub fn create_random(range_x: &Range<f32>) -> Vec2

Generates a new instance of Vec2 initialized to random values in the range entered and returns it

§Examples


Source

pub fn create_random2(range_x: &Range<f32>, range_y: &Range<f32>) -> Vec2

Generates a new instance of Vec2 initialized to random values in the range entered and returns it

§Examples


Source

pub fn from_angle(theta: &f32, in_mag: &Option<f32>) -> Vec2

Generates a new instance of Vec2 based on a entered angle with a magnitude of 1

§Examples
  

   
Source

pub fn from_rand_angle(range_x: &Range<f32>, mag: &Option<f32>) -> Vec2

Generates a new instance of Vec2 initialized to random values in the range entered and returns it

§Examples


Source

pub fn random_unit(range: &Range<f32>) -> Vec2

Generates a new instance of Vec2 initialized to a magnitude of 1 and a angle of random value in the range entered and returns it

§Examples
use std::f32::consts::TAU;
use miscmath::prelude::*;

let mut a = Vec2::random_unit( &( 0.0..TAU ) );

assert!( ( ( a.x - 1.0 ) < 0.00001 ) && ( a.y < TAU ) );
Source

pub fn unit() -> Vec2

Generates a new instance of Vec2 initialized as a unit vector of angle 0 and returns it

§Examples
use miscmath::prelude::*;

let mut b = Vec2::unit();

assert!( ( b.x - 1.0 < 0.00001 ) && ( b.y < 0.00001 ) );
Source

pub fn add(&mut self, rhs: &Vec2)

Adds the components from the rhs Vec2 to the corresponding components of self

§Examples
  

   
Source

pub fn angle_between(&self, rhs: &Vec2) -> f32

Calculates the angle between self and a passed in Vec2

§Examples
use miscmath::prelude::*;

let mut a = Vec2::new( &5.6, &7.2 );
let mut b = Vec2::unit();

let angle = a.angle_between( &mut b );

assert!( ( angle - 1.4731481877 ) < 0.00001 );
Source

pub fn constrain(&mut self, x_rng: &Range<f32>, y_rng: &Range<f32>)

§Examples


Source

pub fn cross(&mut self)

Generates a new instance of Vec2 which is perpendicular to the self instance

§Examples
  

   
Source

pub fn dist(&self, rhs: &Vec2) -> f32

Calculates the distance between self and the Vec2 passed in

§Examples


Source

pub fn dist_sq(&self, rhs: &Vec2) -> f32

Calculates the distance squared between self and the Vec2 passed in

§Examples
  

   
Source

pub fn dbg(&self)

Prints debug information of self to terminal

§Examples
  

   
Source

pub fn dot(&self, rhs: &Vec2) -> f32

Calculates the dot product between self and a passed in Vec2

§Examples
  

   
Source

pub fn div(&mut self, rhs: &f32)

Divides the components of self by a scalar value rhs

§Examples
  

   
Source

pub fn div2(&mut self, rhs: &Vec2)

Divides the components of self by a Vec2 rhs

§Examples


Source

pub fn lerp(&mut self, rhs: &Vec2, amt: UnitF)

Linearly interpolates between self and a passed in Vec2

§Examples
use miscmath::prelude::*;

let mut a = Vec2::new( &5.6, &7.2 );
let mut b = Vec2::unit();

a.lerp( &b, UnitF::new( 0.5 ) );

assert!( ( ( a.x - 3.3 ) < 0.00001 ) && ( ( a.y - 3.6 ) < 0.00001 ) );
Source

pub fn mag(&self) -> f32

Calculates the magnitude of self

§Examples
  

   
Source

pub fn mag_sq(&self) -> f32

Calculates the magnitude squared of self

§Examples
  

   
Source

pub fn mult(&mut self, rhs: &f32)

Multiplies the components of self by a scalar value rhs

§Examples
  

   
Source

pub fn mult2(&mut self, rhs: &Vec2)

Multiplies the components of self by a Vec2 rhs

§Examples


Source

pub fn norm(&mut self)

Normalizes the magnitude of self to 1, angle is unchanged

§Examples
  

   
Source

pub fn rem(&mut self, rhs: &f32)

Sets the components of self to the remainder of scalar division by rhs

§Examples
  

   
Source

pub fn rotate(&mut self, theta: f32)

Rotates self by the angle entered

§Examples
  

   
Source

pub fn set(&mut self, input1: &f32, input2: &f32, coord_system: &CoordSystem)

Sets components of vector to values entered

§Examples
  

   
Source

pub fn set_mag(&mut self, input: &f32)

Sets the magnitude of self

§Examples
  

   
Source

pub fn set_theta(&mut self, input: &f32)

Sets the theta of self

§Examples
  

   
Source

pub fn sub(&mut self, rhs: &Vec2)

Subtracts the components of the rhs Vec2 from the corresponding components of self

§Examples
  

   
Source

pub fn system(&self) -> CoordSystem

Returns the current coordinate system

§Examples
  

   
Source

pub fn theta(&self) -> f32

Calculates the theta of self

§Examples
  

   

Trait Implementations§

Source§

impl Add for Vec2

Implements Add for Vec2

§Examples

use miscmath::prelude::*;
 
let mut x = Vec2::default();
let y = Vec2::new( &5.0, &7.0);
x += y;

assert_eq!(x, y);
Source§

type Output = Vec2

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign for Vec2

Implements AddAssign for Vec2

§Examples



Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for Vec2

Source§

fn clone(&self) -> Vec2

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 Debug for Vec2

Source§

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

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

impl Default for Vec2

Implements Default for Vec2 Generates a new instance of Vec2 initialized to zero and returns it

§Examples

use miscmath::prelude::*;

let a = Vec2::default();

assert!( ( a.x < 0.00001 ) && ( a.y < 0.00001 ) );
Source§

fn default() -> Self

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

impl Div for Vec2

Implements Div for Vec2

§Examples



Source§

type Output = Vec2

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl DivAssign for Vec2

Implements DivAssign for Vec2

§Examples



Source§

fn div_assign(&mut self, rhs: Self)

Performs the /= operation. Read more
Source§

impl Mul for Vec2

Implements Mul for Vec2

§Examples



Source§

type Output = Vec2

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl MulAssign for Vec2

Implements MulAssign for Vec2

§Examples



Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl PartialEq for Vec2

Implements PartialEq for Vec2

§Examples

  

Source§

fn eq(&self, rhs: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, rhs: &Self) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Rem for Vec2

Implements Rem for Vec2

§Examples



Source§

type Output = Vec2

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Self) -> Self::Output

Performs the % operation. Read more
Source§

impl RemAssign for Vec2

Implements RemAssign for Vec2

§Examples



Source§

fn rem_assign(&mut self, rhs: Self)

Performs the %= operation. Read more
Source§

impl Sub for Vec2

Implements Sub for Vec2

§Examples



Source§

type Output = Vec2

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl SubAssign for Vec2

Implements SubAssign for Vec2

§Examples



Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl Copy for Vec2

Auto Trait Implementations§

§

impl Freeze for Vec2

§

impl RefUnwindSafe for Vec2

§

impl Send for Vec2

§

impl Sync for Vec2

§

impl Unpin for Vec2

§

impl UnwindSafe for Vec2

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V