2012-04-10 15:00:15 +00:00
|
|
|
--[[
|
2013-02-20 15:54:57 +00:00
|
|
|
Copyright (c) 2012-2013 Matthias Richter
|
2012-04-10 15:00:15 +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.
|
|
|
|
]]--
|
|
|
|
|
2013-07-30 09:55:06 +00:00
|
|
|
local sqrt, cos, sin, atan2 = math.sqrt, math.cos, math.sin, math.atan2
|
2012-04-10 15:00:15 +00:00
|
|
|
|
|
|
|
local function str(x,y)
|
|
|
|
return "("..tonumber(x)..","..tonumber(y)..")"
|
|
|
|
end
|
|
|
|
|
2012-04-10 15:32:01 +00:00
|
|
|
local function mul(s, x,y)
|
2012-04-10 15:00:15 +00:00
|
|
|
return s*x, s*y
|
|
|
|
end
|
|
|
|
|
2012-04-10 15:32:01 +00:00
|
|
|
local function div(s, x,y)
|
2012-04-10 15:00:15 +00:00
|
|
|
return x/s, y/s
|
|
|
|
end
|
|
|
|
|
2012-04-12 14:08:36 +00:00
|
|
|
local function add(x1,y1, x2,y2)
|
|
|
|
return x1+x2, y1+y2
|
|
|
|
end
|
|
|
|
|
|
|
|
local function sub(x1,y1, x2,y2)
|
|
|
|
return x1-x2, y1-y2
|
|
|
|
end
|
|
|
|
|
2012-04-10 15:00:15 +00:00
|
|
|
local function permul(x1,y1, x2,y2)
|
|
|
|
return x1*x2, y1*y2
|
|
|
|
end
|
|
|
|
|
|
|
|
local function dot(x1,y1, x2,y2)
|
|
|
|
return x1*x2 + y1*y2
|
|
|
|
end
|
|
|
|
|
|
|
|
local function det(x1,y1, x2,y2)
|
2012-04-10 15:32:01 +00:00
|
|
|
return x1*y2 - y1*x2
|
2012-04-10 15:00:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function eq(x1,y1, x2,y2)
|
|
|
|
return x1 == x2 and y1 == y2
|
|
|
|
end
|
|
|
|
|
|
|
|
local function lt(x1,y1, x2,y2)
|
|
|
|
return x1 < x2 or (x1 == x2 and y1 < y2)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function le(x1,y1, x2,y2)
|
|
|
|
return x1 <= x2 and y1 <= y2
|
|
|
|
end
|
|
|
|
|
|
|
|
local function len2(x,y)
|
|
|
|
return x*x + y*y
|
|
|
|
end
|
|
|
|
|
|
|
|
local function len(x,y)
|
|
|
|
return sqrt(x*x + y*y)
|
|
|
|
end
|
|
|
|
|
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 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-03 21:04:27 +00:00
|
|
|
|
2017-11-04 10:19:27 +00:00
|
|
|
return fromPolar(math.random()*2*math.pi,
|
|
|
|
math.random() * (len_max-len_min) + len_min)
|
2017-11-01 14:44:04 +00:00
|
|
|
end
|
|
|
|
|
2017-03-16 19:14:34 +00:00
|
|
|
local function toPolar(x, y)
|
|
|
|
return atan2(y,x), len(x,y)
|
|
|
|
end
|
|
|
|
|
2013-07-24 14:16:50 +00:00
|
|
|
local function dist2(x1,y1, x2,y2)
|
|
|
|
return len2(x1-x2, y1-y2)
|
|
|
|
end
|
|
|
|
|
2012-04-10 15:00:15 +00:00
|
|
|
local function dist(x1,y1, x2,y2)
|
|
|
|
return len(x1-x2, y1-y2)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function normalize(x,y)
|
|
|
|
local l = len(x,y)
|
2012-05-18 23:36:10 +00:00
|
|
|
if l > 0 then
|
|
|
|
return x/l, y/l
|
|
|
|
end
|
|
|
|
return x,y
|
2012-04-10 15:00:15 +00:00
|
|
|
end
|
|
|
|
|
2012-04-10 15:32:01 +00:00
|
|
|
local function rotate(phi, x,y)
|
2012-04-10 15:00:15 +00:00
|
|
|
local c, s = cos(phi), sin(phi)
|
|
|
|
return c*x - s*y, s*x + c*y
|
|
|
|
end
|
|
|
|
|
|
|
|
local function perpendicular(x,y)
|
|
|
|
return -y, x
|
|
|
|
end
|
|
|
|
|
|
|
|
local function project(x,y, u,v)
|
|
|
|
local s = (x*u + y*v) / (u*u + v*v)
|
|
|
|
return s*u, s*v
|
|
|
|
end
|
|
|
|
|
|
|
|
local function mirror(x,y, u,v)
|
|
|
|
local s = 2 * (x*u + y*v) / (u*u + v*v)
|
|
|
|
return s*u - x, s*v - y
|
|
|
|
end
|
|
|
|
|
2013-04-26 02:07:57 +00:00
|
|
|
-- ref.: http://blog.signalsondisplay.com/?p=336
|
2013-04-27 10:26:56 +00:00
|
|
|
local function trim(maxLen, x, y)
|
|
|
|
local s = maxLen * maxLen / len2(x, y)
|
2014-03-03 22:00:23 +00:00
|
|
|
s = s > 1 and 1 or math.sqrt(s)
|
2013-04-27 10:26:56 +00:00
|
|
|
return x * s, y * s
|
2013-04-26 02:07:57 +00:00
|
|
|
end
|
2012-04-10 15:00:15 +00:00
|
|
|
|
2013-07-30 09:55:06 +00:00
|
|
|
local function angleTo(x,y, u,v)
|
2013-11-04 21:04:16 +00:00
|
|
|
if u and v then
|
|
|
|
return atan2(y, x) - atan2(v, u)
|
|
|
|
end
|
|
|
|
return atan2(y, x)
|
2013-07-30 09:55:06 +00:00
|
|
|
end
|
|
|
|
|
2012-04-10 15:00:15 +00:00
|
|
|
-- the module
|
|
|
|
return {
|
|
|
|
str = str,
|
|
|
|
|
2017-11-03 21:04:27 +00:00
|
|
|
fromPolar = fromPolar,
|
|
|
|
toPolar = toPolar,
|
|
|
|
randomDirection = randomDirection,
|
2017-03-16 19:14:34 +00:00
|
|
|
|
2012-04-10 15:00:15 +00:00
|
|
|
-- arithmetic
|
|
|
|
mul = mul,
|
|
|
|
div = div,
|
2012-04-12 14:08:36 +00:00
|
|
|
add = add,
|
|
|
|
sub = sub,
|
2012-04-10 15:00:15 +00:00
|
|
|
permul = permul,
|
|
|
|
dot = dot,
|
|
|
|
det = det,
|
|
|
|
cross = det,
|
|
|
|
|
|
|
|
-- relation
|
|
|
|
eq = eq,
|
|
|
|
lt = lt,
|
|
|
|
le = le,
|
|
|
|
|
|
|
|
-- misc operations
|
|
|
|
len2 = len2,
|
|
|
|
len = len,
|
2013-07-24 14:16:50 +00:00
|
|
|
dist2 = dist2,
|
2012-04-10 15:00:15 +00:00
|
|
|
dist = dist,
|
|
|
|
normalize = normalize,
|
|
|
|
rotate = rotate,
|
|
|
|
perpendicular = perpendicular,
|
|
|
|
project = project,
|
|
|
|
mirror = mirror,
|
2013-07-30 09:55:06 +00:00
|
|
|
trim = trim,
|
|
|
|
angleTo = angleTo,
|
2012-04-10 15:00:15 +00:00
|
|
|
}
|