vector.lua 2.7 KB

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