-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.lua
39 lines (31 loc) · 1.02 KB
/
util.lua
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
local util = {}
-- Maths
function util.add(pos1, pos2)
local x = (pos1.x or pos1[1]) + (pos2.x or pos2[1])
local y = (pos1.y or pos1[2]) + (pos2.y or pos2[2])
return {x=x, y=y}
end
function util.sub(pos1, pos2)
local x = (pos1.x or pos1[1]) - (pos2.x or pos2[1])
local y = (pos1.y or pos1[2]) - (pos2.y or pos2[2])
return {x=x, y=y}
end
function util.serialize_area_box(box)
local leftTop = box.left_top or box[1]
local rightBottom = box.right_bottom or box[2]
local size = util.sub(rightBottom, leftTop)
-- round to 2 decimal places remove differences between loading and runtime floating point inaccuracies
local size_string = {
x = string.format("%.2f", size.x),
y = string.format("%.2f", size.y)
}
return (size_string.x .. ":" .. size_string.y):gsub("[.]", "'")
end
-- Strings
function util.starts_with(str, start)
return str:sub(1, #start) == start
end
function util.ends_with(str, ending)
return ending == "" or str:sub(-#ending) == ending
end
return util