vector.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. --[[
  2. Copyright (c) 2010-2013 Matthias Richter
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. Except as contained in this notice, the name(s) of the above copyright holders
  12. shall not be used in advertising or otherwise to promote the sale, use or
  13. other dealings in this Software without prior written authorization.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ]]--
  22. local assert = assert
  23. local sqrt, cos, sin, atan2 = math.sqrt, math.cos, math.sin, math.atan2
  24. local vector = {}
  25. vector.__index = vector
  26. local function new(x,y)
  27. return setmetatable({x = x or 0, y = y or 0}, vector)
  28. end
  29. local zero = new(0,0)
  30. local function isvector(v)
  31. return type(v) == 'table' and type(v.x) == 'number' and type(v.y) == 'number'
  32. end
  33. function vector:clone()
  34. return new(self.x, self.y)
  35. end
  36. function vector:unpack()
  37. return self.x, self.y
  38. end
  39. function vector:__tostring()
  40. return "("..tonumber(self.x)..","..tonumber(self.y)..")"
  41. end
  42. function vector.__unm(a)
  43. return new(-a.x, -a.y)
  44. end
  45. function vector.__add(a,b)
  46. assert(isvector(a) and isvector(b), "Add: wrong argument types (<vector> expected)")
  47. return new(a.x+b.x, a.y+b.y)
  48. end
  49. function vector.__sub(a,b)
  50. assert(isvector(a) and isvector(b), "Sub: wrong argument types (<vector> expected)")
  51. return new(a.x-b.x, a.y-b.y)
  52. end
  53. function vector.__mul(a,b)
  54. if type(a) == "number" then
  55. return new(a*b.x, a*b.y)
  56. elseif type(b) == "number" then
  57. return new(b*a.x, b*a.y)
  58. else
  59. assert(isvector(a) and isvector(b), "Mul: wrong argument types (<vector> or <number> expected)")
  60. return a.x*b.x + a.y*b.y
  61. end
  62. end
  63. function vector.__div(a,b)
  64. assert(isvector(a) and type(b) == "number", "wrong argument types (expected <vector> / <number>)")
  65. return new(a.x / b, a.y / b)
  66. end
  67. function vector.__eq(a,b)
  68. return a.x == b.x and a.y == b.y
  69. end
  70. function vector.__lt(a,b)
  71. return a.x < b.x or (a.x == b.x and a.y < b.y)
  72. end
  73. function vector.__le(a,b)
  74. return a.x <= b.x and a.y <= b.y
  75. end
  76. function vector.permul(a,b)
  77. assert(isvector(a) and isvector(b), "permul: wrong argument types (<vector> expected)")
  78. return new(a.x*b.x, a.y*b.y)
  79. end
  80. function vector:len2()
  81. return self.x * self.x + self.y * self.y
  82. end
  83. function vector:len()
  84. return sqrt(self.x * self.x + self.y * self.y)
  85. end
  86. function vector.dist(a, b)
  87. assert(isvector(a) and isvector(b), "dist: wrong argument types (<vector> expected)")
  88. local dx = a.x - b.x
  89. local dy = a.y - b.y
  90. return sqrt(dx * dx + dy * dy)
  91. end
  92. function vector.dist2(a, b)
  93. assert(isvector(a) and isvector(b), "dist: wrong argument types (<vector> expected)")
  94. local dx = a.x - b.x
  95. local dy = a.y - b.y
  96. return (dx * dx + dy * dy)
  97. end
  98. function vector:normalize_inplace()
  99. local l = self:len()
  100. if l > 0 then
  101. self.x, self.y = self.x / l, self.y / l
  102. end
  103. return self
  104. end
  105. function vector:normalized()
  106. return self:clone():normalize_inplace()
  107. end
  108. function vector:rotate_inplace(phi)
  109. local c, s = cos(phi), sin(phi)
  110. self.x, self.y = c * self.x - s * self.y, s * self.x + c * self.y
  111. return self
  112. end
  113. function vector:rotated(phi)
  114. local c, s = cos(phi), sin(phi)
  115. return new(c * self.x - s * self.y, s * self.x + c * self.y)
  116. end
  117. function vector:perpendicular()
  118. return new(-self.y, self.x)
  119. end
  120. function vector:projectOn(v)
  121. assert(isvector(v), "invalid argument: cannot project vector on " .. type(v))
  122. -- (self * v) * v / v:len2()
  123. local s = (self.x * v.x + self.y * v.y) / (v.x * v.x + v.y * v.y)
  124. return new(s * v.x, s * v.y)
  125. end
  126. function vector:mirrorOn(v)
  127. assert(isvector(v), "invalid argument: cannot mirror vector on " .. type(v))
  128. -- 2 * self:projectOn(v) - self
  129. local s = 2 * (self.x * v.x + self.y * v.y) / (v.x * v.x + v.y * v.y)
  130. return new(s * v.x - self.x, s * v.y - self.y)
  131. end
  132. function vector:cross(v)
  133. assert(isvector(v), "cross: wrong argument types (<vector> expected)")
  134. return self.x * v.y - self.y * v.x
  135. end
  136. -- ref.: http://blog.signalsondisplay.com/?p=336
  137. function vector:trim_inplace(maxLen)
  138. local s = maxLen * maxLen / self:len2()
  139. s = (s > 1 and 1) or math.sqrt(s)
  140. self.x, self.y = self.x * s, self.y * s
  141. return self
  142. end
  143. function vector:angleTo(other)
  144. if other then
  145. return atan2(self.y, self.x) - atan2(other.y, other.x)
  146. end
  147. return atan2(self.y, self.x)
  148. end
  149. function vector:trimmed(maxLen)
  150. return self:clone():trim_inplace(maxLen)
  151. end
  152. -- the module
  153. return setmetatable({new = new, isvector = isvector, zero = zero},
  154. {__call = function(_, ...) return new(...) end})