summaryrefslogtreecommitdiff
path: root/util.lua
blob: 452e6d5508521df36ab2765012563647d03512a9 (plain)
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

local M = {}

function M.copy(t)
	local c = {}
	for k,v in pairs(t) do
		c[k] = v
	end
	return c
end

function M.deepcopy(t)
	local copied = {}
	local function dc(t)
		if not copied[t] then
			local c = {}
			copied[t] = c
			for k,v in pairs(t) do
				if type(v) == 'table' then
					v = dc(v)
				end
				c[k] = v
			end
		end
		return copied[t]
	end
	return dc(t)
end

function M.rot(a, x, y)
	return x*math.cos(a) - y*math.sin(a), x*math.sin(a) + y*math.cos(a)
end

function M.magnitude(v)
	local s = 0
	for _, a in ipairs(v) do
		s = s + a*a
	end
	return math.sqrt(s)
end

return M