-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector.lua
executable file
·118 lines (94 loc) · 2.75 KB
/
vector.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
local vector = {}
vector.sqLen = function(x,y)
return x*x + y*y
end
vector.len = function(x,y)
return math.sqrt(x*x + y*y)
end
vector.dot = function(x0,y0, x1,y1)
return (x0*x1 + y0*y1)
end
-- returns dx,dy
vector.fromTo = function(x0,y0, x1,y1)
return x1-x0, y1-y0
end
-- returns dx,dy with given len (starting at x0/y0)
vector.fromToWithLen = function(x0,y0, x1,y1, len)
return vector.normalizeToLen(x1-x0, y1-y0, len)
end
vector.lenFromTo = function(x0,y0, x1,y1)
return vector.len(vector.fromTo(x0,y0, x1,y1))
end
vector.sqLenFromTo = function(x0,y0, x1,y1)
return vector.sqLen(vector.fromTo(x0,y0, x1,y1))
end
vector.angleFromTo = function(x0,y0, x1,y1)
x0,y0 = vector.normalize(x0,y0)
x1,y1 = vector.normalize(x1,y1)
return math.acos(vector.dot(x0,y0, x1,y1))
end
-- returns x,y
vector.normalize = function(x,y)
local l = vector.len(x,y)
return x/l, y/l
end
-- 0|-1 -> -1/2 pi, 1|0 -> 0 pi, 0|1 -> 1/2 pi, -1|0 -> 1 pi
-- returns radians
vector.toRotation = function(x,y)
local radians = math.atan2(y,x)
return radians
end
-- 0 points upwards, adjusted for love gfx rotation
-- 0|-1 -> 0, 1|0 -> 1/2 pi, 0|1 -> 1 pi, -1|0 -> -1/2 pi
vector.toVisualRotation = function(x,y)
return vector.toRotation(x,y) + math.pi * 0.5
end
-- return x,y
vector.fromRotation = function (radians, len)
len = len or 1
return len * math.cos(radians), len * math.sin(radians)
end
-- return x,y
vector.fromVisualRotation = function (radians, len)
len = len or 1
local r = radians - 0.5 * math.pi
return len * math.cos(r), len * math.sin(r)
end
-- return dir (eg. "up", "down", "left", "right")
-- visual up is 0,-1
vector.dirFromVisualRotation = function (dx,dy)
local maxDir = nil
local maxDot = nil
if maxDot == nil or vector.dot(dx,dy, 1,0) > maxDot then maxDot = vector.dot(dx,dy, 1,0) maxDir = "right" end
if maxDot == nil or vector.dot(dx,dy, -1,0) > maxDot then maxDot = vector.dot(dx,dy, -1,0) maxDir = "left" end
if maxDot == nil or vector.dot(dx,dy, 0,-1) > maxDot then maxDot = vector.dot(dx,dy, 0,-1) maxDir = "up" end
if maxDot == nil or vector.dot(dx,dy, 0,1) > maxDot then maxDot = vector.dot(dx,dy, 0,1) maxDir = "down" end
return maxDir
end
--[[
for x=-1,1 do
for y=-1,1 do
if math.abs(x) + math.abs(y) == 1 then
print(x,y, "vis", vector.toVisualRotation(x,y), vector.fromVisualRotation(vector.toVisualRotation(x,y)))
print(x,y, "math", vector.toRotation(x,y), vector.fromRotation(vector.toRotation(x,y)))
end
end
end
]]
vector.normalizeToLen = function(x,y,l)
local nx,ny = vector.normalize (x,y)
return nx*l, ny*l
end
-- returns x,y
vector.add = function(x0,y0, x1,y1)
return x0+x1, y0+y1
end
-- returns x,y
vector.sub = function(x0,y0, x1,y1)
return x0-x1, y0-y1
end
-- returns x,y
vector.mul = function(x,y,s)
return x*s,y*s
end
return vector