2011-01-13 13:38:36 +00:00
|
|
|
--[[
|
2011-01-18 17:55:51 +00:00
|
|
|
Copyright (c) 2010-2011 Matthias Richter
|
2011-01-13 13:38:36 +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-01-18 17:55:51 +00:00
|
|
|
local setmetatable, getmetatable = setmetatable, getmetatable
|
2011-02-02 20:04:00 +00:00
|
|
|
local type, tonumber = type, tonumber
|
2011-01-18 17:55:51 +00:00
|
|
|
local sqrt, cos, sin = math.sqrt, math.cos, math.sin
|
2011-01-13 13:38:36 +00:00
|
|
|
module(...)
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
local vector = {}
|
|
|
|
vector.__index = vector
|
2011-01-13 13:38:36 +00:00
|
|
|
|
|
|
|
function new(x,y)
|
|
|
|
local v = {x = x or 0, y = y or 0}
|
2011-02-02 20:04:00 +00:00
|
|
|
return setmetatable(v, vector)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function isvector(v)
|
2011-01-18 17:55:51 +00:00
|
|
|
return getmetatable(v) == vector
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector:clone()
|
|
|
|
return new(self.x, self.y)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector:unpack()
|
2011-01-13 13:38:36 +00:00
|
|
|
return self.x, self.y
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector:__tostring()
|
2011-01-13 13:38:36 +00:00
|
|
|
return "("..tonumber(self.x)..","..tonumber(self.y)..")"
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector.__unm(a)
|
|
|
|
return new(-a.x, -a.y)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector.__add(a,b)
|
|
|
|
return new(a.x+b.x, a.y+b.y)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector.__sub(a,b)
|
|
|
|
return new(a.x-b.x, a.y-b.y)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector.__mul(a,b)
|
2011-01-13 13:38:36 +00:00
|
|
|
if type(a) == "number" then
|
2011-01-18 17:55:51 +00:00
|
|
|
return new(a*b.x, a*b.y)
|
2011-01-13 13:38:36 +00:00
|
|
|
elseif type(b) == "number" then
|
2011-01-18 17:55:51 +00:00
|
|
|
return new(b*a.x, b*a.y)
|
2011-01-13 13:38:36 +00:00
|
|
|
else
|
|
|
|
return a.x*b.x + a.y*b.y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector.__div(a,b)
|
|
|
|
return new(a.x / b, a.y / b)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector.__eq(a,b)
|
2011-01-13 13:38:36 +00:00
|
|
|
return a.x == b.x and a.y == b.y
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector.__lt(a,b)
|
2011-01-13 13:38:36 +00:00
|
|
|
return a.x < b.x or (a.x == b.x and a.y < b.y)
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector.__le(a,b)
|
2011-01-13 13:38:36 +00:00
|
|
|
return a.x <= b.x and a.y <= b.y
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector.permul(a,b)
|
|
|
|
return new(a.x*b.x, a.y*b.y)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector:len2()
|
2011-01-13 13:38:36 +00:00
|
|
|
return self * self
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector:len()
|
2011-01-13 13:38:36 +00:00
|
|
|
return sqrt(self*self)
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector.dist(a, b)
|
2011-01-13 13:38:36 +00:00
|
|
|
return (b-a):len()
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector:normalize_inplace()
|
2011-01-13 13:38:36 +00:00
|
|
|
local l = self:len()
|
|
|
|
self.x, self.y = self.x / l, self.y / l
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector:normalized()
|
2011-01-13 13:38:36 +00:00
|
|
|
return self / self:len()
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector:rotate_inplace(phi)
|
2011-01-13 13:38:36 +00:00
|
|
|
local c, s = cos(phi), sin(phi)
|
|
|
|
self.x, self.y = c * self.x - s * self.y, s * self.x + c * self.y
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector:rotated(phi)
|
2011-01-13 13:38:36 +00:00
|
|
|
return self:clone():rotate_inplace(phi)
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector:perpendicular()
|
|
|
|
return new(-self.y, self.x)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector:projectOn(v)
|
2011-01-13 13:38:36 +00:00
|
|
|
return (self * v) * v / v:len2()
|
|
|
|
end
|
|
|
|
|
2011-01-18 17:55:51 +00:00
|
|
|
function vector:cross(other)
|
2011-01-13 13:38:36 +00:00
|
|
|
return self.x * other.y - self.y * other.x
|
|
|
|
end
|
2011-01-18 17:55:51 +00:00
|
|
|
|
|
|
|
-- vector() as shortcut to vector.new()
|
|
|
|
do
|
|
|
|
local m = {}
|
|
|
|
m.__call = function(_, ...) return new(...) end
|
|
|
|
setmetatable(_M, m)
|
|
|
|
end
|