-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector2.lua
More file actions
72 lines (57 loc) · 1.27 KB
/
Copy pathvector2.lua
File metadata and controls
72 lines (57 loc) · 1.27 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
Vector2 = {}
Vector2.__index = Vector2
function Vector2:new(x, y)
local vector = {
x = x,
y = y
}
setmetatable(vector, self)
return vector
end
function Vector2:copy()
return Vector2:new(self.x, self.y)
end
function Vector2:__tostring()
return string.format("Vector2 (%f, %f)", self.x, self.y)
end
function Vector2:__eq(other)
return self.x == other.x and self.y == other.y
end
function Vector2:__add(other)
return Vector2:new(self.x + other.x, self.y + other.y)
end
function Vector2:__sub(other)
return Vector2:new(self.x - other.x, self.y - other.y)
end
function Vector2:__mul(value)
return Vector2:new(self.x * value, self.y * value)
end
function Vector2:__div(value)
return Vector2:new(self.x / value, self.y / value)
end
function Vector2:zero()
self.x = 0
self.y = 0
end
function Vector2:isZero()
return self.x == 0 and self.y == 0
end
function Vector2:length()
return math.sqrt((self.x ^ 2) + (self.y ^ 2))
end
function Vector2:lengthSq()
return (self.x ^ 2) + (self.y ^ 2)
end
function Vector2:normalize()
local length = self:length()
if length > 0 then
self.x = self.x / length
self.y = self.y / length
end
end
function Vector2:dot(other)
return self.x * other.x + self.y * other.y
end
function Vector2:perp()
return Vector2:new(-self.y, self.x)
end