vector.lua 2.4 KB

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