Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.27 KB | None | 0 0
  1. use std::ops::Add;
  2. use std::ops::Sub;
  3. use std::ops::Mul;
  4. use std::ops::Div;
  5. use std::ops::AddAssign;
  6. use std::ops::SubAssign;
  7. use std::ops::MulAssign;
  8. use std::ops::DivAssign;
  9.  
  10. use rand::prelude::*;
  11.  
  12. #[derive(Copy, Clone)]
  13. pub struct Vector2D {
  14.     pub x: f32,
  15.     pub y: f32,
  16. }
  17.  
  18. impl Default for Vector2D {
  19.     fn default() -> Self {
  20.         let mut rng = thread_rng();
  21.         Self {
  22.             x: 0.0,
  23.             y: 0.0,
  24.         }
  25.     }
  26. }
  27.  
  28. impl Vector2D {
  29.     pub fn new(x: f32, y: f32) -> Self {
  30.         Self {
  31.             x,
  32.             y,
  33.         }
  34.     }
  35.  
  36.     pub fn random() -> Self {
  37.         let mut rng = thread_rng();
  38.         Self {
  39.             x: rng.gen_range(0.0, crate::WIDTH),
  40.             y: rng.gen_range(0.0, crate::HEIGHT),
  41.         }
  42.     }
  43.  
  44.     pub fn random_unit() -> Self {
  45.         let mut rng = thread_rng();
  46.         let angle = rng.gen_range(0.0, std::f32::consts::TAU);
  47.  
  48.         Self::up().rotate(angle)
  49.     }
  50.  
  51.     pub fn zero() -> Self { Self { x: 0.0, y: 0.0 }}
  52.     pub fn up() -> Self { Self { x: 0.0, y: -1.0 }}
  53.     pub fn down() -> Self { Self { x: 0.0, y: 1.0 }}
  54.     pub fn left() -> Self { Self { x: -1.0, y: 0.0 }}
  55.     pub fn right() -> Self { Self { x: 1.0, y: 0.0 }}
  56.  
  57.     pub fn rotate(&self, angle: f32) -> Self {
  58.         Self {
  59.             x: (angle.cos() * self.x) - (angle.sin() * self.y),
  60.             y: (angle.sin() * self.x) + (angle.cos() * self.y),
  61.         }
  62.     }
  63.  
  64.    
  65.     pub fn to_array(&self) -> [f32; 2] {
  66.         [self.x, self.y]
  67.     }
  68.  
  69.     pub fn normalise(&self) -> Self {
  70.         let length = self.length();
  71.  
  72.         if length == 0.0 {
  73.             Self::zero()
  74.         } else {
  75.             Self {
  76.                 x: self.x / length,
  77.                 y: self.y / length,
  78.             }
  79.         }
  80.     }
  81.    
  82.     pub fn distance(self, other: Self) -> f32 {
  83.         (self - other).length()
  84.     }
  85.  
  86.     pub fn length(&self) -> f32 {
  87.         (self.x.powi(2) + self.y.powi(2)).sqrt()
  88.     }
  89. }
  90.  
  91. impl Add for Vector2D {
  92.     type Output = Self;
  93.  
  94.     fn add(self, other: Self) -> Self {
  95.         Self {
  96.             x: self.x + other.x,
  97.             y: self.y + other.y,
  98.         }
  99.     }
  100. }
  101.  
  102. impl Sub for Vector2D {
  103.     type Output = Self;
  104.  
  105.     fn sub(self, other: Self) -> Self {
  106.         Self {
  107.             x: self.x - other.x,
  108.             y: self.y - other.y,
  109.         }
  110.     }
  111. }
  112.  
  113. impl Mul<f32> for Vector2D {
  114.     type Output = Self;
  115.     fn mul(self, n: f32) -> Self {
  116.         Self {
  117.             x: self.x * n,
  118.             y: self.y * n,
  119.         }
  120.     }
  121. }
  122.  
  123. impl Div<f32> for Vector2D {
  124.     type Output = Self;
  125.     fn div(self, n: f32) -> Self {
  126.         Self {
  127.             x: self.x / n,
  128.             y: self.y / n,
  129.         }
  130.     }
  131. }
  132.  
  133. impl AddAssign for Vector2D {
  134.     fn add_assign(&mut self, other: Self) {
  135.         self.x += other.x;
  136.         self.y += other.y;
  137.     }
  138. }
  139.  
  140. impl SubAssign for Vector2D {
  141.     fn sub_assign(&mut self, other: Self) {
  142.         self.x -= other.x;
  143.         self.y -= other.y;
  144.     }
  145. }
  146.  
  147. impl MulAssign<f32> for Vector2D {
  148.     fn mul_assign(&mut self, n: f32) {
  149.         self.x *= n;
  150.         self.y *= n;
  151.     }
  152. }
  153.  
  154. impl DivAssign<f32> for Vector2D {
  155.     fn div_assign(&mut self, n: f32) {
  156.         self.x /= n;
  157.         self.y /= n;
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement