vector.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. vector = {}
  2. local function assert_vector(v)
  3. assert(type(v) == "table" and v.x and v.y and v.z, "Invalid vector")
  4. end
  5. function vector.new(a, b, c)
  6. if type(a) == "table" then
  7. assert(a.x and a.y and a.z, "Invalid vector passed to vector.new()")
  8. return {x=a.x, y=a.y, z=a.z}
  9. elseif a then
  10. assert(b and c, "Invalid arguments for vector.new()")
  11. return {x=a, y=b, z=c}
  12. end
  13. return {x=0, y=0, z=0}
  14. end
  15. function vector.equals(a, b)
  16. assert_vector(a)
  17. assert_vector(b)
  18. return a.x == b.x and
  19. a.y == b.y and
  20. a.z == b.z
  21. end
  22. function vector.length(v)
  23. assert_vector(v)
  24. return math.hypot(v.x, math.hypot(v.y, v.z))
  25. end
  26. function vector.normalize(v)
  27. assert_vector(v)
  28. local len = vector.length(v)
  29. if len == 0 then
  30. return {x=0, y=0, z=0}
  31. else
  32. return vector.divide(v, len)
  33. end
  34. end
  35. function vector.round(v)
  36. assert_vector(v)
  37. return {
  38. x = math.floor(v.x + 0.5),
  39. y = math.floor(v.y + 0.5),
  40. z = math.floor(v.z + 0.5)
  41. }
  42. end
  43. function vector.distance(a, b)
  44. assert_vector(a)
  45. assert_vector(b)
  46. local x = a.x - b.x
  47. local y = a.y - b.y
  48. local z = a.z - b.z
  49. return math.hypot(x, math.hypot(y, z))
  50. end
  51. function vector.direction(pos1, pos2)
  52. assert_vector(pos1)
  53. assert_vector(pos2)
  54. local x_raw = pos2.x - pos1.x
  55. local y_raw = pos2.y - pos1.y
  56. local z_raw = pos2.z - pos1.z
  57. local x_abs = math.abs(x_raw)
  58. local y_abs = math.abs(y_raw)
  59. local z_abs = math.abs(z_raw)
  60. if x_abs >= y_abs and
  61. x_abs >= z_abs then
  62. y_raw = y_raw * (1 / x_abs)
  63. z_raw = z_raw * (1 / x_abs)
  64. x_raw = x_raw / x_abs
  65. end
  66. if y_abs >= x_abs and
  67. y_abs >= z_abs then
  68. x_raw = x_raw * (1 / y_abs)
  69. z_raw = z_raw * (1 / y_abs)
  70. y_raw = y_raw / y_abs
  71. end
  72. if z_abs >= y_abs and
  73. z_abs >= x_abs then
  74. x_raw = x_raw * (1 / z_abs)
  75. y_raw = y_raw * (1 / z_abs)
  76. z_raw = z_raw / z_abs
  77. end
  78. return {x=x_raw, y=y_raw, z=z_raw}
  79. end
  80. function vector.add(a, b)
  81. assert_vector(a)
  82. if type(b) == "table" then
  83. assert_vector(b)
  84. return {x = a.x + b.x,
  85. y = a.y + b.y,
  86. z = a.z + b.z}
  87. else
  88. return {x = a.x + b,
  89. y = a.y + b,
  90. z = a.z + b}
  91. end
  92. end
  93. function vector.subtract(a, b)
  94. assert_vector(a)
  95. if type(b) == "table" then
  96. assert_vector(b)
  97. return {x = a.x - b.x,
  98. y = a.y - b.y,
  99. z = a.z - b.z}
  100. else
  101. return {x = a.x - b,
  102. y = a.y - b,
  103. z = a.z - b}
  104. end
  105. end
  106. function vector.multiply(a, b)
  107. assert_vector(a)
  108. if type(b) == "table" then
  109. assert_vector(b)
  110. return {x = a.x * b.x,
  111. y = a.y * b.y,
  112. z = a.z * b.z}
  113. else
  114. return {x = a.x * b,
  115. y = a.y * b,
  116. z = a.z * b}
  117. end
  118. end
  119. function vector.divide(a, b)
  120. assert_vector(a)
  121. if type(b) == "table" then
  122. assert_vector(b)
  123. return {x = a.x / b.x,
  124. y = a.y / b.y,
  125. z = a.z / b.z}
  126. else
  127. return {x = a.x / b,
  128. y = a.y / b,
  129. z = a.z / b}
  130. end
  131. end