vector2.nim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # author: Ethosa
  2. import math
  3. {.used.}
  4. type
  5. Vector2Obj* = object
  6. x*, y*: float
  7. Vector2Ref* = ref Vector2Obj
  8. proc Vector2*(x, y: float): Vector2Ref {.inline.} =
  9. Vector2Ref(x: x, y: y)
  10. proc Vector2*(b: Vector2Ref): Vector2Ref {.inline.} =
  11. Vector2Ref(x: b.x, y: b.y)
  12. proc Vector2*(): Vector2Ref {.inline.} =
  13. Vector2Ref(x: 0, y: 0)
  14. proc abs*(a: Vector2Ref): Vector2Ref =
  15. Vector2(abs(a.x), abs(a.y))
  16. proc angle*(a: Vector2Ref): float =
  17. arctan2(a.y, a.x)
  18. proc cross*(a, b: Vector2Ref): float =
  19. a.x*b.x - a.y*b.y
  20. proc cross*(a: Vector2Ref, x, y: float): float =
  21. a.x*x - a.y*y
  22. proc dot*(a, b: Vector2Ref): float =
  23. a.x*b.x + a.y*b.y
  24. proc dot*(a: Vector2Ref, x, y: float): float =
  25. a.x*x + a.y*y
  26. proc angleTo*(a, b: Vector2Ref): float =
  27. arctan2(a.cross(b), a.dot(b))
  28. proc angleTo*(a: Vector2Ref, x, y: float): float =
  29. arctan2(a.cross(x, y), a.dot(x, y))
  30. proc angleToPoint*(a, b: Vector2Ref): float =
  31. arctan2(a.y - b.y, a.x - b.x)
  32. proc angleToPoint*(a: Vector2Ref, x, y: float): float =
  33. arctan2(a.y - y, a.x - x)
  34. proc length*(a: Vector2Ref): float =
  35. sqrt(a.x*a.x + a.y*a.y)
  36. proc normalize*(a: Vector2Ref) =
  37. var l: float = a.x*a.x + a.y*a.y
  38. if l != 0:
  39. l = sqrt(l)
  40. a.x /= l
  41. a.y /= l
  42. proc normalized*(a: Vector2Ref): Vector2Ref =
  43. result = Vector2(a)
  44. result.normalize()
  45. proc distance*(a, b: Vector2Ref): float =
  46. sqrt((b.x - a.x)*(b.x - a.x) + (b.y - a.y)*(b.y - a.y))
  47. proc distance*(a: Vector2Ref, x, y: float): float =
  48. sqrt((x - a.x)*(x - a.x) + (y - a.y)*(y - a.y))
  49. proc directionTo*(a, b: Vector2Ref): Vector2Ref =
  50. result = Vector2(b.x - a.x, b.y - a.y)
  51. result.normalize()
  52. proc directionTo*(a: Vector2Ref, x, y: float): Vector2Ref =
  53. result = Vector2(x - a.x, y - a.y)
  54. result.normalize()
  55. # --- Operators --- #
  56. proc `+`*(a, b: Vector2Ref): Vector2Ref =
  57. Vector2(a.x + b.x, a.y + b.y)
  58. proc `-`*(a, b: Vector2Ref): Vector2Ref =
  59. Vector2(a.x - b.x, a.y - b.y)
  60. proc `/`*(a, b: Vector2Ref): Vector2Ref =
  61. Vector2(a.x / b.x, a.y / b.y)
  62. proc `*`*(a, b: Vector2Ref): Vector2Ref =
  63. Vector2(a.x * b.x, a.y * b.y)
  64. proc `*`*(x: Vector2Ref, y: float): Vector2Ref =
  65. Vector2(x.x * y, x.y * y)
  66. proc `+`*(x: Vector2Ref, y: float): Vector2Ref =
  67. Vector2(x.x + y, x.y + y)
  68. proc `-`*(x: Vector2Ref, y: float): Vector2Ref =
  69. Vector2(x.x - y, x.y - y)
  70. proc `/`*(x: Vector2Ref, y: float): Vector2Ref =
  71. Vector2(x.x / y, x.y / y)
  72. proc `*=`*(x: var Vector2Ref, y: Vector2Ref) =
  73. x = x * y
  74. proc `+=`*(x: var Vector2Ref, y: Vector2Ref) =
  75. x = x + y
  76. proc `-=`*(x: var Vector2Ref, y: Vector2Ref) =
  77. x = x - y
  78. proc `/=`*(x: var Vector2Ref, y: Vector2Ref) =
  79. x = x / y
  80. proc `>`*(x, y: Vector2Ref): bool =
  81. x.x > y.x and x.y > y.y
  82. proc `<`*(x, y: Vector2Ref): bool =
  83. x.x < y.x and x.y < y.y
  84. proc `>=`*(x, y: Vector2Ref): bool =
  85. x.x >= y.x and x.y >= y.y
  86. proc `<=`*(x, y: Vector2Ref): bool =
  87. x.x <= y.x and x.y <= y.y
  88. proc `==`*(x, y: Vector2Ref): bool =
  89. x.x == y.x and x.y == y.y
  90. proc `!=`*(x, y: Vector2Ref): bool =
  91. x.x != y.x and x.y != y.y
  92. proc `$`*(a: Vector2Ref): string =
  93. "Vector2(" & $a.x & ", " & $a.y & ")"