vec2.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. --- A 2 component vector.
  2. -- @module vec2
  3. local modules = (...):gsub('%.[^%.]+$', '') .. "."
  4. local vec3 = require(modules .. "vec3")
  5. local acos = math.acos
  6. local atan2 = math.atan2
  7. local sqrt = math.sqrt
  8. local cos = math.cos
  9. local sin = math.sin
  10. local vec2 = {}
  11. local vec2_mt = {}
  12. -- Private constructor.
  13. local function new(x, y)
  14. return setmetatable({
  15. x = x or 0,
  16. y = y or 0
  17. }, vec2_mt)
  18. end
  19. -- Do the check to see if JIT is enabled. If so use the optimized FFI structs.
  20. local status, ffi
  21. if type(jit) == "table" and jit.status() then
  22. status, ffi = pcall(require, "ffi")
  23. if status then
  24. ffi.cdef "typedef struct { double x, y;} cpml_vec2;"
  25. new = ffi.typeof("cpml_vec2")
  26. end
  27. end
  28. --- Constants
  29. -- @table vec2
  30. -- @field unit_x X axis of rotation
  31. -- @field unit_y Y axis of rotation
  32. -- @field zero Empty vector
  33. vec2.unit_x = new(1, 0)
  34. vec2.unit_y = new(0, 1)
  35. vec2.zero = new(0, 0)
  36. --- The public constructor.
  37. -- @param x Can be of three types: </br>
  38. -- number X component
  39. -- table {x, y} or {x = x, y = y}
  40. -- scalar to fill the vector eg. {x, x}
  41. -- @tparam number y Y component
  42. -- @treturn vec2 out
  43. function vec2.new(x, y)
  44. -- number, number
  45. if x and y then
  46. assert(type(x) == "number", "new: Wrong argument type for x (<number> expected)")
  47. assert(type(y) == "number", "new: Wrong argument type for y (<number> expected)")
  48. return new(x, y)
  49. -- {x, y} or {x=x, y=y}
  50. elseif type(x) == "table" then
  51. local xx, yy = x.x or x[1], x.y or x[2]
  52. assert(type(xx) == "number", "new: Wrong argument type for x (<number> expected)")
  53. assert(type(yy) == "number", "new: Wrong argument type for y (<number> expected)")
  54. return new(xx, yy)
  55. -- number
  56. elseif type(x) == "number" then
  57. return new(x, x)
  58. else
  59. return new()
  60. end
  61. end
  62. --- Convert point from polar to cartesian.
  63. -- @tparam number radius Radius of the point
  64. -- @tparam number theta Angle of the point (in radians)
  65. -- @treturn vec2 out
  66. function vec2.from_cartesian(radius, theta)
  67. return new(radius * cos(theta), radius * sin(theta))
  68. end
  69. --- Clone a vector.
  70. -- @tparam vec2 a Vector to be cloned
  71. -- @treturn vec2 out
  72. function vec2.clone(a)
  73. return new(a.x, a.y)
  74. end
  75. --- Add two vectors.
  76. -- @tparam vec2 a Left hand operant
  77. -- @tparam vec2 b Right hand operant
  78. -- @treturn vec2 out
  79. function vec2.add(a, b)
  80. return new(
  81. a.x + b.x,
  82. a.y + b.y
  83. )
  84. end
  85. --- Subtract one vector from another.
  86. -- @tparam vec2 a Left hand operant
  87. -- @tparam vec2 b Right hand operant
  88. -- @treturn vec2 out
  89. function vec2.sub(a, b)
  90. return new(
  91. a.x - b.x,
  92. a.y - b.y
  93. )
  94. end
  95. --- Multiply a vector by another vector.
  96. -- @tparam vec2 a Left hand operant
  97. -- @tparam vec2 b Right hand operant
  98. -- @treturn vec2 out
  99. function vec2.mul(a, b)
  100. return new(
  101. a.x * b.x,
  102. a.y * b.y
  103. )
  104. end
  105. --- Divide a vector by another vector.
  106. -- @tparam vec2 a Left hand operant
  107. -- @tparam vec2 b Right hand operant
  108. -- @treturn vec2 out
  109. function vec2.div(a, b)
  110. return new(
  111. a.x / b.x,
  112. a.y / b.y
  113. )
  114. end
  115. --- Get the normal of a vector.
  116. -- @tparam vec2 a Vector to normalize
  117. -- @treturn vec2 out
  118. function vec2.normalize(a)
  119. if a:is_zero() then
  120. return new()
  121. end
  122. return a:scale(1 / a:len())
  123. end
  124. --- Trim a vector to a given length.
  125. -- @tparam vec2 a Vector to be trimmed
  126. -- @tparam number len Length to trim the vector to
  127. -- @treturn vec2 out
  128. function vec2.trim(a, len)
  129. return a:normalize():scale(math.min(a:len(), len))
  130. end
  131. --- Get the cross product of two vectors.
  132. -- @tparam vec2 a Left hand operant
  133. -- @tparam vec2 b Right hand operant
  134. -- @treturn number magnitude
  135. function vec2.cross(a, b)
  136. return a.x * b.y - a.y * b.x
  137. end
  138. --- Get the dot product of two vectors.
  139. -- @tparam vec2 a Left hand operant
  140. -- @tparam vec2 b Right hand operant
  141. -- @treturn number dot
  142. function vec2.dot(a, b)
  143. return a.x * b.x + a.y * b.y
  144. end
  145. --- Get the length of a vector.
  146. -- @tparam vec2 a Vector to get the length of
  147. -- @treturn number len
  148. function vec2.len(a)
  149. return sqrt(a.x * a.x + a.y * a.y)
  150. end
  151. --- Get the squared length of a vector.
  152. -- @tparam vec2 a Vector to get the squared length of
  153. -- @treturn number len
  154. function vec2.len2(a)
  155. return a.x * a.x + a.y * a.y
  156. end
  157. --- Get the distance between two vectors.
  158. -- @tparam vec2 a Left hand operant
  159. -- @tparam vec2 b Right hand operant
  160. -- @treturn number dist
  161. function vec2.dist(a, b)
  162. local dx = a.x - b.x
  163. local dy = a.y - b.y
  164. return sqrt(dx * dx + dy * dy)
  165. end
  166. --- Get the squared distance between two vectors.
  167. -- @tparam vec2 a Left hand operant
  168. -- @tparam vec2 b Right hand operant
  169. -- @treturn number dist
  170. function vec2.dist2(a, b)
  171. local dx = a.x - b.x
  172. local dy = a.y - b.y
  173. return dx * dx + dy * dy
  174. end
  175. --- Scale a vector by a scalar.
  176. -- @tparam vec2 a Left hand operant
  177. -- @tparam number b Right hand operant
  178. -- @treturn vec2 out
  179. function vec2.scale(a, b)
  180. return new(
  181. a.x * b,
  182. a.y * b
  183. )
  184. end
  185. --- Rotate a vector.
  186. -- @tparam vec2 a Vector to rotate
  187. -- @tparam number phi Angle to rotate vector by (in radians)
  188. -- @treturn vec2 out
  189. function vec2.rotate(a, phi)
  190. local c = cos(phi)
  191. local s = sin(phi)
  192. return new(
  193. c * a.x - s * a.y,
  194. s * a.x + c * a.y
  195. )
  196. end
  197. --- Get the perpendicular vector of a vector.
  198. -- @tparam vec2 a Vector to get perpendicular axes from
  199. -- @treturn vec2 out
  200. function vec2.perpendicular(a)
  201. return new(-a.y, a.x)
  202. end
  203. --- Angle from one vector to another.
  204. -- @tparam vec2 a Vector
  205. -- @tparam vec2 b Vector
  206. -- @treturn number angle
  207. function vec2.angle_to(a, b)
  208. if b then
  209. return atan2(a.y - b.y, a.x - b.x)
  210. end
  211. return atan2(a.y, a.x)
  212. end
  213. --- Angle between two vectors.
  214. -- @tparam vec2 a Vector
  215. -- @tparam vec2 b Vector
  216. -- @treturn number angle
  217. function vec2.angle_between(a, b)
  218. if b then
  219. if vec2.is_vec2(a) then
  220. return acos(a:dot(b) / (a:len() * b:len()))
  221. end
  222. return acos(vec3.dot(a, b) / (vec3.len(a) * vec3.len(b)))
  223. end
  224. return 0
  225. end
  226. --- Lerp between two vectors.
  227. -- @tparam vec2 a Left hand operant
  228. -- @tparam vec2 b Right hand operant
  229. -- @tparam number s Step value
  230. -- @treturn vec2 out
  231. function vec2.lerp(a, b, s)
  232. return a + (b - a) * s
  233. end
  234. --- Unpack a vector into individual components.
  235. -- @tparam vec2 a Vector to unpack
  236. -- @treturn number x
  237. -- @treturn number y
  238. function vec2.unpack(a)
  239. return a.x, a.y
  240. end
  241. --- Return a boolean showing if a table is or is not a vec2.
  242. -- @tparam vec2 a Vector to be tested
  243. -- @treturn boolean is_vec2
  244. function vec2.is_vec2(a)
  245. if type(a) == "cdata" then
  246. return ffi.istype("cpml_vec2", a)
  247. end
  248. return
  249. type(a) == "table" and
  250. type(a.x) == "number" and
  251. type(a.y) == "number"
  252. end
  253. --- Return a boolean showing if a table is or is not a zero vec2.
  254. -- @tparam vec2 a Vector to be tested
  255. -- @treturn boolean is_zero
  256. function vec2.is_zero(a)
  257. return a.x == 0 and a.y == 0
  258. end
  259. --- Convert point from cartesian to polar.
  260. -- @tparam vec2 a Vector to convert
  261. -- @treturn number radius
  262. -- @treturn number theta
  263. function vec2.to_polar(a)
  264. local radius = sqrt(a.x^2 + a.y^2)
  265. local theta = atan2(a.y, a.x)
  266. theta = theta > 0 and theta or theta + 2 * math.pi
  267. return radius, theta
  268. end
  269. --- Return a formatted string.
  270. -- @tparam vec2 a Vector to be turned into a string
  271. -- @treturn string formatted
  272. function vec2.to_string(a)
  273. return string.format("(%+0.3f,%+0.3f)", a.x, a.y)
  274. end
  275. vec2_mt.__index = vec2
  276. vec2_mt.__tostring = vec2.to_string
  277. function vec2_mt.__call(_, x, y)
  278. return vec2.new(x, y)
  279. end
  280. function vec2_mt.__unm(a)
  281. return new(-a.x, -a.y)
  282. end
  283. function vec2_mt.__eq(a, b)
  284. if not vec2.is_vec2(a) or not vec2.is_vec2(b) then
  285. return false
  286. end
  287. return a.x == b.x and a.y == b.y
  288. end
  289. function vec2_mt.__add(a, b)
  290. assert(vec2.is_vec2(a), "__add: Wrong argument type for left hand operant. (<cpml.vec2> expected)")
  291. assert(vec2.is_vec2(b), "__add: Wrong argument type for right hand operant. (<cpml.vec2> expected)")
  292. return a:add(b)
  293. end
  294. function vec2_mt.__sub(a, b)
  295. assert(vec2.is_vec2(a), "__add: Wrong argument type for left hand operant. (<cpml.vec2> expected)")
  296. assert(vec2.is_vec2(b), "__add: Wrong argument type for right hand operant. (<cpml.vec2> expected)")
  297. return a:sub(b)
  298. end
  299. function vec2_mt.__mul(a, b)
  300. assert(vec2.is_vec2(a), "__mul: Wrong argument type for left hand operant. (<cpml.vec2> expected)")
  301. assert(vec2.is_vec2(b) or type(b) == "number", "__mul: Wrong argument type for right hand operant. (<cpml.vec2> or <number> expected)")
  302. if vec2.is_vec2(b) then
  303. return a:mul(b)
  304. end
  305. return a:scale(b)
  306. end
  307. function vec2_mt.__div(a, b)
  308. assert(vec2.is_vec2(a), "__div: Wrong argument type for left hand operant. (<cpml.vec2> expected)")
  309. assert(vec2.is_vec2(b) or type(b) == "number", "__div: Wrong argument type for right hand operant. (<cpml.vec2> or <number> expected)")
  310. if vec2.is_vec2(b) then
  311. return a:div(b)
  312. end
  313. return a:scale(1 / b)
  314. end
  315. if status then
  316. ffi.metatype(new, vec2_mt)
  317. end
  318. return setmetatable({}, vec2_mt)