This repository was archived by the owner on Jul 11, 2026. It is now read-only.
forked from darrellanderson/CrLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.ttslua
More file actions
99 lines (89 loc) · 3.57 KB
/
Copy pathUtil.ttslua
File metadata and controls
99 lines (89 loc) · 3.57 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
-------------------------------------------------------------------------------
--- Lua utility functions
-- @author Darrell
-------------------------------------------------------------------------------
local TAG = 'CrLua.Util'
CrLua = CrLua or {} -- global, <include> wraps in a do .. end block
CrLua.Util = assert(not CrLua.Util) and {
_require = {}
}
-------------------------------------------------------------------------------
--- Compute the distance between two {x,y,z} vectors.
-- @param a table : {x,y,z} keys.
-- @param b table : {x,y,z} keys.
-- @return distance squared (sqrt it if need exact, sq is fine for sorting).
-------------------------------------------------------------------------------
function CrLua.Util.distanceSq(a, b)
local dx = a.x - b.x
local dy = a.y - b.y
local dz = a.z - b.z
return (dx * dx) + (dy * dy) + (dz * dz)
end
function CrLua.Util._testDistanceSq()
local a = { x = 0, y = 0, z = 0 }
local b = { x = 1, y = 2, z = 3 }
local distanceSq = CrLua.Util.distanceSq(a, b)
assert(distanceSq == 14)
end
-------------------------------------------------------------------------------
--- Find the minimum value in a table, with optional function for value.
-- @param table : table with arbitrary keys (may be a list).
-- @param optional minFunction : call for each value to minimize.
-- @return ? : table value with the min value.
-- Function can return false/nil to reject value from consideration.
-------------------------------------------------------------------------------
function CrLua.Util.min(table, minFunction)
assert(type(table) == 'table' and ((not minFunction) or type(minFunction) == 'function'))
local minScore = nil
local minKey, minValue = nil, nil
for k, v in pairs(table) do
local score = (minFunction and minFunction(v)) or tonumber(v)
if score and (not minScore or score < minScore) then
minScore = score
minKey = k
minValue = v
end
end
return minKey, minValue
end
function CrLua.Util._testMin()
local list = { 2, 1, 3, 5, 4 }
local minFunction = function(v)
return v
end
local k1, min1 = CrLua.Util.min(list)
local k2, min2 = CrLua.Util.min(list, minFunction)
assert(k1 == k2 and k1 == 2, 'k=' .. (k1 or '<nil>'))
assert(min1 == min2 and min1 == 1)
end
-------------------------------------------------------------------------------
--- Find the maximum value in a table, with optional function for value.
-- @param table : table with arbitrary keys (may be a list).
-- @param optional maxFunction : call for each value to maximize.
-- @return ? : table value with the max value.
-- Function can return false/nil to reject value from consideration.
-------------------------------------------------------------------------------
function CrLua.Util.max(table, maxFunction)
assert(type(table) == 'table' and ((not maxFunction) or type(maxFunction) == 'function'))
local maxScore = nil
local maxKey, maxValue = nil, nil
for k, v in pairs(table) do
local score = (maxFunction and maxFunction(v)) or tonumber(v)
if score and (not maxScore or score > maxScore) then
maxScore = score
maxKey = k
maxValue = v
end
end
return maxKey, maxValue
end
function CrLua.Util._testMax()
local list = { 2, 1, 3, 5, 4 }
local maxFunction = function(v)
return v
end
local k1, max1 = CrLua.Util.max(list)
local k2, max2 = CrLua.Util.max(list, maxFunction)
assert(k1 == k2 and k1 == 4)
assert(max1 == max2 and max1 == 5)
end