-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.lua
89 lines (71 loc) · 1.89 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
Vector = {}
Vector.__index = Vector
function math.map(value, imin, imax, omin, omax)
local scale = (omax - omin) / (imax - imin)
return omin + (value - imin) * scale
end
function Vector:create(x, y)
local vector = {}
setmetatable(vector, Vector)
vector.x = x
vector.y = y
return vector
end
function Vector:__tostring()
return "Vector(x = " .. string.format("%.2f", self.x) .. ", y = " .. string.format("%.2f", self.y) .. ")"
end
function Vector:__add(other)
return Vector:create(self.x + other.x, self.y + other.y)
end
function Vector:__sub(other)
return Vector:create(self.x - other.x, self.y - other.y)
end
function Vector:__mul(value)
return Vector:create(self.x * value, self.y * value)
end
function Vector:__div(value)
return Vector:create(self.x / value, self.y / value)
end
function Vector:mag()
return math.sqrt(self.x * self.x + self.y * self.y)
end
function Vector:norm()
m = self:mag()
if (m > 0) then
self:div(m)
end
end
function Vector:add(other)
self.x = self.x + other.x
self.y = self.y + other.y
end
function Vector:sub(other)
self.x = self.x - other.x
self.y = self.y - other.y
end
function Vector:mul(value)
self.x = self.x * value
self.y = self.y * value
end
function Vector:div(value)
self.x = self.x / value
self.y = self.y / value
end
function Vector:limit(max)
if self:mag() > max then
self:norm()
self:mul(max)
end
end
function Vector:copy()
return Vector:create(self.x, self.y)
end
function Vector:heading()
return math.atan2(self.y, self.x)
end
function Vector:distTo(other)
return math.sqrt((other.x - self.x) * (other.x - self.x) + (other.y - self.y) * (other.y - self.y))
end
function dotProduct(vec1, vec2)
return (vec1.x * vec2.x + vec1.y * vec2.y)
end