vector2.nim 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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): Vector2Obj {.inline.} =
  9. Vector2Obj(x: x, y: y)
  10. proc Vector2*(b: Vector2Obj): Vector2Obj {.inline.} =
  11. Vector2Obj(x: b.x, y: b.y)
  12. proc Vector2*(num: float): Vector2Obj {.inline.} =
  13. Vector2Obj(x: num, y: num)
  14. proc Vector2*(): Vector2Obj {.inline.} =
  15. Vector2Obj(x: 0, y: 0)
  16. proc newVector2*(x, y: float): Vector2Ref {.inline.} =
  17. Vector2Ref(x: x, y: y)
  18. proc newVector2*(o: Vector2Obj): Vector2Ref {.inline.} =
  19. Vector2Ref(x: o.x, y: o.y)
  20. proc newVector2*(o: float): Vector2Ref {.inline.} =
  21. Vector2Ref(x: o, y: o)
  22. proc newVector2*(): Vector2Ref {.inline.} =
  23. Vector2Ref(x: 0f, y: 0f)
  24. proc abs*(a: Vector2Obj): Vector2Obj =
  25. Vector2(abs(a.x), abs(a.y))
  26. proc angle*(a: Vector2Obj): float {.inline.} =
  27. arctan2(a.y, a.x)
  28. proc clear*(a: var Vector2Obj) =
  29. a.x = 0
  30. a.y = 0
  31. proc cross*(a, b: Vector2Obj): float {.inline.} =
  32. a.x*b.x - a.y*b.y
  33. proc cross*(a: Vector2Obj, x, y: float): float {.inline.} =
  34. a.x*x - a.y*y
  35. proc dot*(a, b: Vector2Obj): float {.inline.} =
  36. a.x*b.x + a.y*b.y
  37. proc dot*(a: Vector2Obj, x, y: float): float {.inline.} =
  38. a.x*x + a.y*y
  39. proc angleTo*(a, b: Vector2Obj): float {.inline.} =
  40. arctan2(a.cross(b), a.dot(b))
  41. proc angleTo*(a: Vector2Obj, x, y: float): float {.inline.} =
  42. arctan2(a.cross(x, y), a.dot(x, y))
  43. proc angleToPoint*(a, b: Vector2Obj): float {.inline.} =
  44. arctan2(a.y - b.y, a.x - b.x)
  45. proc angleToPoint*(a: Vector2Obj, x, y: float): float {.inline.} =
  46. arctan2(a.y - y, a.x - x)
  47. proc length*(a: Vector2Obj): float {.inline.} =
  48. sqrt(a.x*a.x + a.y*a.y)
  49. proc normalize*(a: var Vector2Obj) =
  50. var l: float = a.x*a.x + a.y*a.y
  51. if l != 0:
  52. l = sqrt(l)
  53. a.x /= l
  54. a.y /= l
  55. proc normalized*(a: Vector2Obj): Vector2Obj =
  56. result = Vector2(a)
  57. result.normalize()
  58. proc distance*(a, b: Vector2Obj): float {.inline.} =
  59. sqrt((b.x - a.x)*(b.x - a.x) + (b.y - a.y)*(b.y - a.y))
  60. proc distance*(a: Vector2Obj, x, y: float): float {.inline.} =
  61. sqrt((x - a.x)*(x - a.x) + (y - a.y)*(y - a.y))
  62. proc directionTo*(a, b: Vector2Obj): Vector2Obj =
  63. result = Vector2(b.x - a.x, b.y - a.y)
  64. result.normalize()
  65. proc directionTo*(a: Vector2Obj, x, y: float): Vector2Obj =
  66. result = Vector2(x - a.x, y - a.y)
  67. result.normalize()
  68. proc intersects*(a, b, c, d: Vector2Obj): bool =
  69. let
  70. uA = ((d.x-c.x)*(a.y-c.y) - (d.y-c.y)*(a.x-c.x)) / ((d.y-c.y)*(b.x-a.x) - (d.x-c.x)*(b.y-a.y))
  71. uB = ((b.x-a.x)*(a.y-c.y) - (b.y-a.y)*(a.x-c.x)) / ((d.y-c.y)*(b.x-a.x) - (d.x-c.x)*(b.y-a.y))
  72. return uA >= 0 and uA <= 1 and uB >= 0 and uB <= 1
  73. proc isEmpty*(a: Vector2Obj): bool =
  74. a.x == 0f and a.y == 0f
  75. # --- Operators --- #
  76. proc `$`*(a: Vector2Obj): string {.inline.} =
  77. "Vector2(" & $a.x & ", " & $a.y & ")"
  78. proc `+`*(a, b: Vector2Obj): Vector2Obj =
  79. Vector2(a.x + b.x, a.y + b.y)
  80. proc `-`*(a, b: Vector2Obj): Vector2Obj =
  81. Vector2(a.x - b.x, a.y - b.y)
  82. proc `/`*(a, b: Vector2Obj): Vector2Obj =
  83. Vector2(a.x / b.x, a.y / b.y)
  84. proc `*`*(a, b: Vector2Obj): Vector2Obj =
  85. Vector2(a.x * b.x, a.y * b.y)
  86. proc `*`*(x: Vector2Obj, y: float): Vector2Obj =
  87. Vector2(x.x * y, x.y * y)
  88. proc `+`*(x: Vector2Obj, y: float): Vector2Obj =
  89. Vector2(x.x + y, x.y + y)
  90. proc `-`*(x: Vector2Obj, y: float): Vector2Obj =
  91. Vector2(x.x - y, x.y - y)
  92. proc `/`*(x: Vector2Obj, y: float): Vector2Obj =
  93. Vector2(x.x / y, x.y / y)
  94. proc `*=`*(x: var Vector2Obj, y: Vector2Obj) =
  95. x = x * y
  96. proc `+=`*(x: var Vector2Obj, y: Vector2Obj) =
  97. x = x + y
  98. proc `-=`*(x: var Vector2Obj, y: Vector2Obj) =
  99. x = x - y
  100. proc `/=`*(x: var Vector2Obj, y: Vector2Obj) =
  101. x = x / y
  102. proc `>`*(x, y: Vector2Obj): bool =
  103. x.x > y.x and x.y > y.y
  104. proc `<`*(x, y: Vector2Obj): bool =
  105. x.x < y.x and x.y < y.y
  106. proc `>=`*(x, y: Vector2Obj): bool =
  107. x.x >= y.x and x.y >= y.y
  108. proc `<=`*(x, y: Vector2Obj): bool =
  109. x.x <= y.x and x.y <= y.y
  110. proc `==`*(x, y: Vector2Obj): bool =
  111. x.x == y.x and x.y == y.y
  112. proc `!=`*(x, y: Vector2Obj): bool =
  113. x.x != y.x and x.y != y.y