2010-09-19 13:37:55 +00:00
|
|
|
--[[
|
2013-02-20 15:54:57 +00:00
|
|
|
Copyright (c) 2010-2013 Matthias Richter
|
2010-09-19 13:37:55 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
Except as contained in this notice, the name(s) of the above copyright holders
|
|
|
|
shall not be used in advertising or otherwise to promote the sale, use or
|
|
|
|
other dealings in this Software without prior written authorization.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
]]--
|
|
|
|
|
2011-07-06 12:19:31 +00:00
|
|
|
local assert = assert
|
2013-07-30 09:54:24 +00:00
|
|
|
local sqrt, cos, sin, atan2 = math.sqrt, math.cos, math.sin, math.atan2
|
2010-08-09 15:52:48 +00:00
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
local vector = {}
|
|
|
|
vector.__index = vector
|
|
|
|
|
2011-07-06 12:19:31 +00:00
|
|
|
local function new(x,y)
|
2012-06-12 10:58:33 +00:00
|
|
|
return setmetatable({x = x or 0, y = y or 0}, vector)
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
2013-07-30 09:54:24 +00:00
|
|
|
local zero = new(0,0)
|
2010-08-09 15:52:48 +00:00
|
|
|
|
2017-03-16 19:14:34 +00:00
|
|
|
local function fromPolar(angle, radius)
|
2017-11-01 14:44:04 +00:00
|
|
|
radius = radius or 1
|
2017-03-16 19:14:34 +00:00
|
|
|
return new(cos(angle) * radius, sin(angle) * radius)
|
|
|
|
end
|
|
|
|
|
2017-11-03 21:04:27 +00:00
|
|
|
local function randomDirection(len_min, len_max)
|
|
|
|
len_min = len_min or 1
|
|
|
|
len_max = len_max or len_min
|
|
|
|
|
2017-11-04 10:19:27 +00:00
|
|
|
assert(len_max > 0, "len_max must be greater than zero")
|
|
|
|
assert(len_max >= len_min, "len_max must be greater than or equal to len_min")
|
2017-11-11 08:43:38 +00:00
|
|
|
|
|
|
|
return fromPolar(math.random() * 2*math.pi,
|
2017-11-04 10:19:27 +00:00
|
|
|
math.random() * (len_max-len_min) + len_min)
|
2017-11-01 14:44:04 +00:00
|
|
|
end
|
|
|
|
|
2011-07-06 12:19:31 +00:00
|
|
|
local function isvector(v)
|
2014-08-22 11:47:55 +00:00
|
|
|
return type(v) == 'table' and type(v.x) == 'number' and type(v.y) == 'number'
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector:clone()
|
|
|
|
return new(self.x, self.y)
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector:unpack()
|
2010-08-09 15:52:48 +00:00
|
|
|
return self.x, self.y
|
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector:__tostring()
|
2010-08-09 15:52:48 +00:00
|
|
|
return "("..tonumber(self.x)..","..tonumber(self.y)..")"
|
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector.__unm(a)
|
|
|
|
return new(-a.x, -a.y)
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector.__add(a,b)
|
2010-08-09 15:52:48 +00:00
|
|
|
assert(isvector(a) and isvector(b), "Add: wrong argument types (<vector> expected)")
|
2011-01-18 16:44:38 +00:00
|
|
|
return new(a.x+b.x, a.y+b.y)
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector.__sub(a,b)
|
2010-09-30 09:44:31 +00:00
|
|
|
assert(isvector(a) and isvector(b), "Sub: wrong argument types (<vector> expected)")
|
2011-01-18 16:44:38 +00:00
|
|
|
return new(a.x-b.x, a.y-b.y)
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector.__mul(a,b)
|
2010-08-09 15:52:48 +00:00
|
|
|
if type(a) == "number" then
|
2011-01-18 16:44:38 +00:00
|
|
|
return new(a*b.x, a*b.y)
|
2010-08-09 15:52:48 +00:00
|
|
|
elseif type(b) == "number" then
|
2011-01-18 16:44:38 +00:00
|
|
|
return new(b*a.x, b*a.y)
|
2010-08-09 15:52:48 +00:00
|
|
|
else
|
|
|
|
assert(isvector(a) and isvector(b), "Mul: wrong argument types (<vector> or <number> expected)")
|
|
|
|
return a.x*b.x + a.y*b.y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector.__div(a,b)
|
2010-08-09 15:52:48 +00:00
|
|
|
assert(isvector(a) and type(b) == "number", "wrong argument types (expected <vector> / <number>)")
|
2011-01-18 16:44:38 +00:00
|
|
|
return new(a.x / b, a.y / b)
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2018-05-23 15:13:51 +00:00
|
|
|
function vector.__idiv(a,b)
|
|
|
|
assert(isvector(a) and type(b) == "number", "wrong argument types (expected <vector> / <number>)")
|
|
|
|
return new(a.x // b, a.y // b)
|
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector.__eq(a,b)
|
2010-08-09 15:52:48 +00:00
|
|
|
return a.x == b.x and a.y == b.y
|
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector.__lt(a,b)
|
2010-08-09 15:52:48 +00:00
|
|
|
return a.x < b.x or (a.x == b.x and a.y < b.y)
|
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector.__le(a,b)
|
2010-08-09 15:52:48 +00:00
|
|
|
return a.x <= b.x and a.y <= b.y
|
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector.permul(a,b)
|
2010-08-09 15:52:48 +00:00
|
|
|
assert(isvector(a) and isvector(b), "permul: wrong argument types (<vector> expected)")
|
2011-01-18 16:44:38 +00:00
|
|
|
return new(a.x*b.x, a.y*b.y)
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2017-03-16 19:14:34 +00:00
|
|
|
function vector:toPolar()
|
|
|
|
return new(atan2(self.x, self.y), self:len())
|
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector:len2()
|
2011-07-06 12:19:31 +00:00
|
|
|
return self.x * self.x + self.y * self.y
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector:len()
|
2012-01-21 22:35:31 +00:00
|
|
|
return sqrt(self.x * self.x + self.y * self.y)
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector.dist(a, b)
|
2010-08-09 15:52:48 +00:00
|
|
|
assert(isvector(a) and isvector(b), "dist: wrong argument types (<vector> expected)")
|
2012-01-21 22:35:31 +00:00
|
|
|
local dx = a.x - b.x
|
|
|
|
local dy = a.y - b.y
|
|
|
|
return sqrt(dx * dx + dy * dy)
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2013-08-05 14:35:49 +00:00
|
|
|
function vector.dist2(a, b)
|
|
|
|
assert(isvector(a) and isvector(b), "dist: wrong argument types (<vector> expected)")
|
|
|
|
local dx = a.x - b.x
|
|
|
|
local dy = a.y - b.y
|
|
|
|
return (dx * dx + dy * dy)
|
|
|
|
end
|
|
|
|
|
2015-10-12 06:21:16 +00:00
|
|
|
function vector:normalizeInplace()
|
2010-08-09 15:52:48 +00:00
|
|
|
local l = self:len()
|
2012-05-18 23:34:56 +00:00
|
|
|
if l > 0 then
|
|
|
|
self.x, self.y = self.x / l, self.y / l
|
|
|
|
end
|
2010-08-09 15:52:48 +00:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector:normalized()
|
2015-10-12 06:21:16 +00:00
|
|
|
return self:clone():normalizeInplace()
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2015-10-12 06:21:16 +00:00
|
|
|
function vector:rotateInplace(phi)
|
2011-01-18 16:44:38 +00:00
|
|
|
local c, s = cos(phi), sin(phi)
|
2010-08-09 15:52:48 +00:00
|
|
|
self.x, self.y = c * self.x - s * self.y, s * self.x + c * self.y
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector:rotated(phi)
|
2012-01-21 22:35:31 +00:00
|
|
|
local c, s = cos(phi), sin(phi)
|
|
|
|
return new(c * self.x - s * self.y, s * self.x + c * self.y)
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
2010-08-17 11:17:01 +00:00
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector:perpendicular()
|
|
|
|
return new(-self.y, self.x)
|
2010-08-17 11:17:01 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 16:44:38 +00:00
|
|
|
function vector:projectOn(v)
|
2012-02-17 20:12:41 +00:00
|
|
|
assert(isvector(v), "invalid argument: cannot project vector on " .. type(v))
|
2012-01-21 22:35:31 +00:00
|
|
|
-- (self * v) * v / v:len2()
|
|
|
|
local s = (self.x * v.x + self.y * v.y) / (v.x * v.x + v.y * v.y)
|
|
|
|
return new(s * v.x, s * v.y)
|
2010-09-30 09:46:33 +00:00
|
|
|
end
|
|
|
|
|
2012-02-17 20:12:41 +00:00
|
|
|
function vector:mirrorOn(v)
|
|
|
|
assert(isvector(v), "invalid argument: cannot mirror vector on " .. type(v))
|
|
|
|
-- 2 * self:projectOn(v) - self
|
2012-01-21 22:35:31 +00:00
|
|
|
local s = 2 * (self.x * v.x + self.y * v.y) / (v.x * v.x + v.y * v.y)
|
|
|
|
return new(s * v.x - self.x, s * v.y - self.y)
|
2011-07-06 12:19:31 +00:00
|
|
|
end
|
|
|
|
|
2012-02-17 20:12:41 +00:00
|
|
|
function vector:cross(v)
|
|
|
|
assert(isvector(v), "cross: wrong argument types (<vector> expected)")
|
|
|
|
return self.x * v.y - self.y * v.x
|
2010-08-17 11:17:01 +00:00
|
|
|
end
|
2011-01-18 16:44:38 +00:00
|
|
|
|
2013-04-26 02:07:57 +00:00
|
|
|
-- ref.: http://blog.signalsondisplay.com/?p=336
|
2015-10-12 06:21:16 +00:00
|
|
|
function vector:trimInplace(maxLen)
|
2013-04-27 10:26:56 +00:00
|
|
|
local s = maxLen * maxLen / self:len2()
|
2014-03-03 19:28:40 +00:00
|
|
|
s = (s > 1 and 1) or math.sqrt(s)
|
2013-04-27 10:26:56 +00:00
|
|
|
self.x, self.y = self.x * s, self.y * s
|
|
|
|
return self
|
2013-04-26 02:07:57 +00:00
|
|
|
end
|
|
|
|
|
2013-07-30 09:54:24 +00:00
|
|
|
function vector:angleTo(other)
|
2013-11-04 21:04:16 +00:00
|
|
|
if other then
|
2014-02-17 17:32:42 +00:00
|
|
|
return atan2(self.y, self.x) - atan2(other.y, other.x)
|
2013-11-04 21:04:16 +00:00
|
|
|
end
|
2014-02-17 17:32:42 +00:00
|
|
|
return atan2(self.y, self.x)
|
2013-07-30 09:54:24 +00:00
|
|
|
end
|
|
|
|
|
2013-04-27 10:26:56 +00:00
|
|
|
function vector:trimmed(maxLen)
|
2015-10-12 06:21:16 +00:00
|
|
|
return self:clone():trimInplace(maxLen)
|
2013-04-26 02:07:57 +00:00
|
|
|
end
|
|
|
|
|
2011-07-06 12:19:31 +00:00
|
|
|
|
|
|
|
-- the module
|
2017-11-01 14:44:04 +00:00
|
|
|
return setmetatable({
|
2017-11-03 21:04:27 +00:00
|
|
|
new = new,
|
|
|
|
fromPolar = fromPolar,
|
|
|
|
randomDirection = randomDirection,
|
|
|
|
isvector = isvector,
|
|
|
|
zero = zero
|
2017-11-01 14:44:04 +00:00
|
|
|
}, {
|
|
|
|
__call = function(_, ...) return new(...) end
|
|
|
|
})
|