hump/vector-light.lua

173 lines
3.7 KiB
Lua
Raw Normal View History

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
local function fromPolar(angle, radius)
return cos(angle)*radius, sin(angle)*radius
end
local function toPolar(x, y)
return atan2(y,x), len(x,y)
end
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)
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
-- 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)
s = s > 1 and 1 or math.sqrt(s)
2013-04-27 10:26:56 +00:00
return x * s, y * s
end
2012-04-10 15:00:15 +00:00
2013-07-30 09:55:06 +00:00
local function angleTo(x,y, u,v)
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,
fromPolar = fromPolar,
toPolar = toPolar,
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,
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
}