vector.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. return vector.normalize({
  57. x = pos2.x - pos1.x,
  58. y = pos2.y - pos1.y,
  59. z = pos2.z - pos1.z
  60. })
  61. end
  62. function vector.angle(a, b)
  63. local dotp = vector.dot(a, b)
  64. local cp = vector.cross(a, b)
  65. local crossplen = vector.length(cp)
  66. return math.atan2(crossplen, dotp)
  67. end
  68. function vector.dot(a, b)
  69. return a.x * b.x + a.y * b.y + a.z * b.z
  70. end
  71. function vector.cross(a, b)
  72. return {
  73. x = a.y * b.z - a.z * b.y,
  74. y = a.z * b.x - a.x * b.z,
  75. z = a.x * b.y - a.y * b.x
  76. }
  77. end
  78. function vector.add(a, b)
  79. if type(b) == "table" then
  80. return {x = a.x + b.x,
  81. y = a.y + b.y,
  82. z = a.z + b.z}
  83. else
  84. return {x = a.x + b,
  85. y = a.y + b,
  86. z = a.z + b}
  87. end
  88. end
  89. function vector.subtract(a, b)
  90. if type(b) == "table" then
  91. return {x = a.x - b.x,
  92. y = a.y - b.y,
  93. z = a.z - b.z}
  94. else
  95. return {x = a.x - b,
  96. y = a.y - b,
  97. z = a.z - b}
  98. end
  99. end
  100. function vector.multiply(a, b)
  101. if type(b) == "table" then
  102. return {x = a.x * b.x,
  103. y = a.y * b.y,
  104. z = a.z * b.z}
  105. else
  106. return {x = a.x * b,
  107. y = a.y * b,
  108. z = a.z * b}
  109. end
  110. end
  111. function vector.divide(a, b)
  112. if type(b) == "table" then
  113. return {x = a.x / b.x,
  114. y = a.y / b.y,
  115. z = a.z / b.z}
  116. else
  117. return {x = a.x / b,
  118. y = a.y / b,
  119. z = a.z / b}
  120. end
  121. end
  122. function vector.sort(a, b)
  123. return {x = math.min(a.x, b.x), y = math.min(a.y, b.y), z = math.min(a.z, b.z)},
  124. {x = math.max(a.x, b.x), y = math.max(a.y, b.y), z = math.max(a.z, b.z)}
  125. end