2
0

vector.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.round(v)
  29. return {
  30. x = math.floor(v.x + 0.5),
  31. y = math.floor(v.y + 0.5),
  32. z = math.floor(v.z + 0.5)
  33. }
  34. end
  35. function vector.apply(v, func)
  36. return {
  37. x = func(v.x),
  38. y = func(v.y),
  39. z = func(v.z)
  40. }
  41. end
  42. function vector.distance(a, b)
  43. local x = a.x - b.x
  44. local y = a.y - b.y
  45. local z = a.z - b.z
  46. return math.hypot(x, math.hypot(y, z))
  47. end
  48. function vector.direction(pos1, pos2)
  49. local x_raw = pos2.x - pos1.x
  50. local y_raw = pos2.y - pos1.y
  51. local z_raw = pos2.z - pos1.z
  52. local x_abs = math.abs(x_raw)
  53. local y_abs = math.abs(y_raw)
  54. local z_abs = math.abs(z_raw)
  55. if x_abs >= y_abs and
  56. x_abs >= z_abs then
  57. y_raw = y_raw * (1 / x_abs)
  58. z_raw = z_raw * (1 / x_abs)
  59. x_raw = x_raw / x_abs
  60. end
  61. if y_abs >= x_abs and
  62. y_abs >= z_abs then
  63. x_raw = x_raw * (1 / y_abs)
  64. z_raw = z_raw * (1 / y_abs)
  65. y_raw = y_raw / y_abs
  66. end
  67. if z_abs >= y_abs and
  68. z_abs >= x_abs then
  69. x_raw = x_raw * (1 / z_abs)
  70. y_raw = y_raw * (1 / z_abs)
  71. z_raw = z_raw / z_abs
  72. end
  73. return {x=x_raw, y=y_raw, z=z_raw}
  74. end
  75. function vector.add(a, b)
  76. if type(b) == "table" then
  77. return {x = a.x + b.x,
  78. y = a.y + b.y,
  79. z = a.z + b.z}
  80. else
  81. return {x = a.x + b,
  82. y = a.y + b,
  83. z = a.z + b}
  84. end
  85. end
  86. function vector.subtract(a, b)
  87. if type(b) == "table" then
  88. return {x = a.x - b.x,
  89. y = a.y - b.y,
  90. z = a.z - b.z}
  91. else
  92. return {x = a.x - b,
  93. y = a.y - b,
  94. z = a.z - b}
  95. end
  96. end
  97. function vector.multiply(a, b)
  98. if type(b) == "table" then
  99. return {x = a.x * b.x,
  100. y = a.y * b.y,
  101. z = a.z * b.z}
  102. else
  103. return {x = a.x * b,
  104. y = a.y * b,
  105. z = a.z * b}
  106. end
  107. end
  108. function vector.divide(a, b)
  109. if type(b) == "table" then
  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