-
Notifications
You must be signed in to change notification settings - Fork 6
/
utility.lua
170 lines (140 loc) · 3.97 KB
/
utility.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
local utility = {}
local function vectorDet(x1,y1, x2,y2)
return x1*y2 - y1*x2
end
local function vectorCross( V, W )
return V.x*W.y - V.y*W.x
end
--[[function areColinear(p, q, r, eps)
return math.abs(vectorDet(q.x-p.x, q.y-p.y, r.x-p.x,r.y-p.y)) <= (eps or 1e-32)
end]]
-- test wether a and b lie on the same side of the line c->d
local function onSameSide(a,b, c,d)
local px, py = d.x-c.x, d.y-c.y
local l = vectorDet(px,py, a.x-c.x, a.y-c.y)
local m = vectorDet(px,py, b.x-c.x, b.y-c.y)
return l*m >= 0
end
local function sign( v )
return (v > 0 and 1) or (v < 0 and -1) or 0
end
-- Get which side of a line a point lies (-1, 0 or 1)
function whichSideOfLine( l1,l2, p )
--return sign( (Bx-Ax)*(Y-Ay) - (By-Ay)*(X-Ax) )
return sign( (l1.x-l2.x)*(p.y-l1.y) - (l2.y-l1.y)*(p.x-l1.x) )
end
function projectPointOntoLine( l1,l2, p )
-- Direction vector of line:
local u = { x = l2.x - l1.x, y = l2.y - l1.y }
-- Perpendicular to that:
local n = { x =-u.y, y = u.x }
local v = { x = p.x - l1.x, y = p.y - l1.y }
local dist = vectorDot( u, v )/vectorDot( u, u )
return { x=u.x*dist + l1.x, y = u.y*dist + l1.y }
end
function utility.linePointDist( l1,l2, p )
local o = projectPointOntoLine( l1,l2, p )
return utility.dist( o, p )
end
function utility.triangleArea( t )
local p = projectPointOntoLine( t[1],t[2], t[3] )
local width = utility.dist( t[1], t[2] )
local height = utility.dist( t[3], p )
return 0.5*width*height
end
function utility.pointInTriangle(p, a,b,c)
return onSameSide(p,a, b,c) and onSameSide(p,b, a,c) and onSameSide(p,c, a,b)
end
function vectorDot( q, p )
return q.x*p.x + q.y*p.y
end
function utility.printTable( t, depth )
depth = depth or 1
for k, v in pairs( t ) do
if type(v) == "table" then
print( string.rep( "\t", depth ) .. k .. " = {")
utility.printTable( v, depth + 1 )
print( string.rep( "\t", depth ) .. "}" )
else
print( string.rep( "\t", depth ) .. k .. " = ", v )
end
end
end
function utility.tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function utility.dist( p1, p2 )
local dx = p1.x - p2.x
local dy = p1.y - p2.y
return math.sqrt( dx*dx + dy*dy )
end
function utility.length( p )
return math.sqrt( p.x*p.x + p.y*p.y )
end
function utility.normalize( p )
local len = utility.length( p )
if len > 0 then
return {x = p.x/len, y = p.y/len}
else return p
end
end
function utility.interpolateCos ( rel)
return -math.cos(math.pi*rel)*0.5 + 0.5
end
function utility.segSegIntersection( seg1, seg2 )
--local t = (q - p) x s /( r x s)
local r = {x = seg2.p2.x - seg2.p1.x, y = seg2.p2.y - seg2.p1.y }
local s = {x = seg1.p2.x - seg1.p1.x, y = seg1.p2.y - seg1.p1.y }
local denom1 = vectorCross( r, s )
local denom2 = vectorCross( s, r )
if denom1 == 0 or denom2 == 0 then
return false
end
local diff1 = { x = seg1.p1.x - seg2.p1.x, y = seg1.p1.y - seg2.p1.y }
local diff2 = { x = seg2.p1.x - seg1.p1.x, y = seg2.p1.y - seg1.p1.y }
local numer1 = vectorCross( diff1, s )
local numer2 = vectorCross( diff2, r )
local t = numer1/denom1
if t < 0 or t > 1 then
return false
end
local u = numer2/denom2
if u < 0 or u > 1 then
return false
end
return true, t, u
end
function utility.numFromString( str )
local num = 0
for k = 1, #str do
num = num + string.byte( str:sub(k,k) )
end
return num
end
function utility.log( str, filename )
filename = filename or "log.txt"
ok, file = pcall( io.open, filename, "a" )
if file then
file:write( str .. "\n" )
file:close()
end
end
serverInfo = {
numPlayers = 0,
map = "-",
state = "Lobby",
}
function utility.createServerInfo()
local str = "Name:" .. SERVER_NAME .. ","
str = str .. "Map:" .. serverInfo.map .. ","
str = str .. "Players:" .. serverInfo.numPlayers .. "/" .. MAX_PLAYERS .. ","
str = str .. "State:" .. serverInfo.state
str = str:gsub("%s", "_")
return str
end
function utility.rotatePoint( x, y, ang )
return x*math.cos(ang) - y*math.sin(ang), x*math.sin(ang) + y*math.cos(ang)
end
return utility