-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlib_vector.lua
More file actions
188 lines (154 loc) · 3.97 KB
/
lib_vector.lua
File metadata and controls
188 lines (154 loc) · 3.97 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
Vector = {}
Vector.__index = Vector
local Rad = math.rad
local Sin = math.sin
local Cos = math.cos
local Sqr = math.sqrt
local Atan2 = math.atan2
function Vector.__add(a, b)
if type(a) == "number" then
return Vector.new(b.x + a, b.y + a)
elseif type(b) == "number" then
return Vector.new(a.x + b, a.y + b)
else
return Vector.new(a.x + b.x, a.y + b.y)
end
end
function Vector.__sub(a, b)
if type(a) == "number" then
return Vector.new(b.x - a, b.y - a)
elseif type(b) == "number" then
return Vector.new(a.x - b, a.y - b)
else
return Vector.new(a.x - b.x, a.y - b.y)
end
end
function Vector.__mul(a, b)
if type(a) == "number" then
return Vector.new(b.x * a, b.y * a)
elseif type(b) == "number" then
return Vector.new(a.x * b, a.y * b)
else
return Vector.new(a.x * b.x, a.y * b.y)
end
end
function Vector.__div(a, b)
if type(a) == "number" then
return Vector.new(b.x / a, b.y / a)
elseif type(b) == "number" then
return Vector.new(a.x / b, a.y / b)
else
return Vector.new(a.x / b.x, a.y / b.y)
end
end
function Vector.__eq(a, b)
return a.x == b.x and a.y == b.y
end
function Vector.__lt(a, b)
return a.x < b.x or (a.x == b.x and a.y < b.y)
end
function Vector.__le(a, b)
return a.x <= b.x and a.y <= b.y
end
function Vector.__tostring(a)
return "(" + a.x + ", " + a.y + ")"
end
function Vector.new(x, y)
return setmetatable({ x = x or 0, y = y or 0 }, Vector)
end
function Vector.angle()
return Atan2(self.y,self.x)
end
function Vector.newFromAngleFixed(inAngle, inVelocity)
local vx = Cos(Rad(inAngle+90))
local vy = Sin(Rad(inAngle+90))
if(inVelocity ~= nil)then
vx = vx * inVelocity
vy = vy * inVelocity
end
return setmetatable({ x = vx or 0, y = vy or 0 }, Vector)
end
function Vector.newFromAngleCorona(inAngle, inVelocity)
local vx = Cos(Rad(inAngle+180))
local vy = Sin(Rad(inAngle+180))
if(inVelocity ~= nil)then
vx = vx * inVelocity
vy = vy * inVelocity
end
return setmetatable({ x = vx or 0, y = vy or 0 }, Vector)
end
function Vector.newFromAngle(inAngle, inVelocity)
local vx = Cos(Rad(inAngle-90))
local vy = Sin(Rad(inAngle-90))
if(inVelocity ~= nil)then
vx = vx * inVelocity
vy = vy * inVelocity
end
return setmetatable({ x = vx or 0, y = vy or 0 }, Vector)
end
function Vector.newFromObjects(inObj1, inObj2)
local vx = inObj2.x - inObj1.x
local vy = inObj2.y - inObj1.y
return setmetatable({ x = vx or 0, y = vy or 0 }, Vector)
end
function Vector.newFromObjectsNormalized(inObj1, inObj2)
tmpVec = Vector.new(inObj2.x - inObj1.x, inObj2.y - inObj1.y)
return tmpVec:Normalize()
end
function Vector.newFromObjectsCNormalized(inObj1, inObj2)
tmpVec = Vector.new(inObj2.centerx - inObj1.x, inObj2.centery - inObj1.y)
return tmpVec:Normalize()
end
function Vector.distance(a, b)
return (b - a):len()
end
function Vector:clone()
return Vector.new(self.x, self.y)
end
function Vector.dotproduct(a, b)
return ((a.x * b.x) + (a.y * b.y))
end
function Vector:unpack()
return self.x, self.y
end
function Vector:len()
return Sqr(self.x * self.x + self.y * self.y)
end
function Vector:lenSq()
return self.x * self.x + self.y * self.y
end
function Vector:Normalize()
local len = self:len()
self.x = self.x / len
self.y = self.y / len
return self
end
function Vector:normalized()
return self / self:len()
end
function Vector:rotate(phi)
local c = Cos(phi)
local s = Sin(phi)
self.x = c * self.x - s * self.y
self.y = s * self.x + c * self.y
return self
end
function Vector:rotated(phi)
return self:clone():rotate(phi)
end
function Vector:reflect(vn)
local d = self.dotproduct(self, vn)
self.x = self.x - 2 * d * vn.x
self.y = self.y - 2 * d * vn.y
return self
end
function Vector:normal()
return Vector.new(-self.y, self.x)
end
function Vector:projectOn(other)
return (self * other) * other / other:lenSq()
end
function Vector:cross(other)
return self.x * other.y - self.y * other.x
end
setmetatable(Vector, { __call = function(_, ...) return Vector.new(...) end })