-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscale.lua
More file actions
84 lines (68 loc) · 1.94 KB
/
Copy pathscale.lua
File metadata and controls
84 lines (68 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
--[[
-- Copyright (c) 2015 Patrick Werneck
--
-- See the file LICENSE for copying permission.
--]]
local scale = {}
scale.__index = scale
-- scale() creates (0,1) -> (0,1) domain
-- scale(a,b) creates (a,b) -> (0,1) domain
-- scale(a,b,c,d) creates (a,b) -> (c,d) domain
--
-- a,b resp. c,d can be packed to {lower=a, upper=b}
function scale.create(imin, imax, omin, omax)
local self = setmetatable({}, scale)
if type(imin) == "table" then
self.inDomain = imin
omin, omax = imax, omin
elseif type(imin) ~= "number" or type(imax) ~= "number" then
self:setInDomain(0, 1)
else
self:setInDomain(imin, imax)
end
if type(omin) == "table" then
self.outDomain = omin
elseif type(omin) ~= "number" or type(omax) ~= "number" then
self:setOutDomain(0, 1)
else
self:setOutDomain(omin, omax)
end
return self
end
function scale:setInDomain(a, b)
self.inDomain = {lower = a, upper = b}
end
function scale:setOutDomain(a, b)
self.outDomain = {lower = a, upper = b}
end
function scale:scale(value)
local id, od = self.inDomain, self.outDomain
return (value - id.lower) / (id.upper - id.lower) * (od.upper - od.lower) + od.lower
end
function scale:unscale(scaled)
local id, od = self.inDomain, self.outDomain
return (scaled - od.lower) / (od.upper - od.lower) * (id.upper - id.lower) + id.lower
end
function scale:add(a, b)
return self:scale(self:unscale(a) + self:unscale(b))
end
function scale:sub(a, b)
return self:scale(self:unscale(a) - self:unscale(b))
end
function scale:mul(a, b)
return self:scale(self:unscale(a) * self:unscale(b))
end
function scale:div(a, b)
return self:scale(self:unscale(a) / self:unscale(b))
end
--[[
function axis:nice()
-- TODO: implement
error("not implemented")
local strMin = tostring(min):match("%d+")
local strMax = tostring(max):match("%d+")
min = min - (min % 10 ^ ( tostring(min):len() - 2))
max = max + (10 ^ ( tostring(max):len() - 2) - max % 10 ^ ( tostring(max):len() - 2))
end
]]--
return scale