Guest User

Untitled

a guest
Jun 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. // nalgebra shim. the playground should really support it though
  2. pub mod na {
  3. pub struct Vector2<T> {
  4. pub x: T,
  5. pub y: T,
  6. }
  7. impl<T> Vector2<T> {
  8. pub fn new(x: T, y: T) -> Self {
  9. Vector2 {
  10. x,
  11. y
  12. }
  13. }
  14. }
  15. }
  16.  
  17. pub const FLOAT_ERROR_ABS: f32 = 0.0001;
  18.  
  19.  
  20. /// this function takes two vectors and compares only their direction, not
  21. /// their length. if the direction of both vectors is equal, this function
  22. /// returns true, otherwise false.
  23. /// the dot product is not what I want, as it is positive if two vectors are
  24. /// parallel, which means vectors thac point in the exact opposite direction
  25. /// of each other will pass, which is not what I want.
  26. pub fn direction_eq(v1: &na::Vector2<f32>, v2: &na::Vector2<f32>) -> bool {
  27. // there may be a more efficient way to do this. it works good enough for
  28. // now though.
  29. // the x and y deltas of both vectors must both be positive or negative.
  30. // because 0.0 can be negative or positive, we do not use
  31. // `is_sign_positive`
  32. if !( (v1.x >= 0.0) == (v2.x >= 0.0) || (v1.y >= 0.0) == (v2.y >= 0.0) ) {
  33. return false;
  34. }
  35. let x = v1.x / v2.x;
  36. if x.is_nan() {
  37. return true;
  38. }
  39. let y = v1.y / v2.y;
  40. if y.is_nan() {
  41. return true;
  42. }
  43. (x - y).abs() < FLOAT_ERROR_ABS
  44. }
  45.  
  46.  
  47. fn main() {
  48. assert_eq!(true, direction_eq(&na::Vector2::new(1.0, 1.0), &na::Vector2::new(1.0, 1.0)));
  49. assert_eq!(true, direction_eq(&na::Vector2::new(1.0, 1.0), &na::Vector2::new(2.0, 2.0)));
  50. assert_eq!(true, direction_eq(&na::Vector2::new(-1.0, -1.0), &na::Vector2::new(-2.0, -2.0)));
  51. assert_eq!(true, direction_eq(&na::Vector2::new(1.0, -1.0), &na::Vector2::new(2.0, -2.0)));
  52. assert_eq!(true, direction_eq(&na::Vector2::new(-1.0, 1.0), &na::Vector2::new(-2.0, 2.0)));
  53. assert_eq!(true, direction_eq(&na::Vector2::new(2.0, 2.0), &na::Vector2::new(1.0, 1.0)));
  54. assert_eq!(true, direction_eq(&na::Vector2::new(0.000005, 0.0003), &na::Vector2::new(0.05, 3.0)));
  55. assert_eq!(true, direction_eq(&na::Vector2::new(1.0, 0.0), &na::Vector2::new(2.0, 0.0)));
  56. assert_eq!(true, direction_eq(&na::Vector2::new(0.0, 1.0), &na::Vector2::new(0.0, 3.0)));
  57. assert_eq!(true, direction_eq(&na::Vector2::new(-1.0, 1.0), &na::Vector2::new(-1.0, 1.0)));
  58. assert_eq!(false, direction_eq(&na::Vector2::new(1.0, 1.0), &na::Vector2::new(0.0, 3.0)));
  59. assert_eq!(false, direction_eq(&na::Vector2::new(1.0, 1.0), &na::Vector2::new(1.0, 2.0)));
  60. assert_eq!(false, direction_eq(&na::Vector2::new(1.0, 1.0), &na::Vector2::new(2.0, 1.0)));
  61. assert_eq!(false, direction_eq(&na::Vector2::new(1.0, 1.0), &na::Vector2::new(-1.0, -1.0)));
  62. assert_eq!(false, direction_eq(&na::Vector2::new(-1.0, 1.0), &na::Vector2::new(1.0, -1.0)));
  63. assert_eq!(false, direction_eq(&na::Vector2::new(1.0, -1.0), &na::Vector2::new(-1.0, 1.0)));
  64.  
  65. println!("!dlrow olleh");
  66. }
Add Comment
Please, Sign In to add comment