-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2D.lua
More file actions
135 lines (106 loc) · 2.37 KB
/
Vector2D.lua
File metadata and controls
135 lines (106 loc) · 2.37 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
Vector2D = {}
function Vector2D:new(x, y)
local object = { x = x, y = y }
setmetatable(object, { __index = Vector2D })
return object
end
function Vector2D:copy()
return Vector2D:new(self.x, self.y)
end
function Vector2D:magnitude()
return math.sqrt(self.x^2 + self.y^2)
end
function Vector2D:normalize()
local temp
temp = self:magnitude()
if temp > 0 then
self.x = self.x / temp
self.y = self.y / temp
end
end
function Vector2D:limit(l)
if self.x > l then
self.x = l
end
if self.y > l then
self.y = l
end
end
function Vector2D:equals(vec)
if self.x == vec.x and self.y == vec.y then
return true
else
return false
end
end
function Vector2D:add(vec)
self.x = self.x + vec.x
self.y = self.y + vec.y
end
function Vector2D:sub(vec)
self.x = self.x - vec.x
self.y = self.y - vec.y
end
function Vector2D:mult(s)
self.x = self.x * s
self.y = self.y * s
end
function Vector2D:div(s)
self.x = self.x / s
self.y = self.y / s
end
function Vector2D:dot(vec)
return self.x * vec.x + self.y * vec.y
end
function Vector2D:dist(vec2)
return math.sqrt( (vec2.x - self.x) + (vec2.y - self.y) )
end
-- Class Methods
function Vector2D:Normalize(vec)
local tempVec = Vector2D:new(vec.x,vec.y)
local temp
temp = tempVec:magnitude()
if temp > 0 then
tempVec.x = tempVec.x / temp
tempVec.y = tempVec.y / temp
end
return tempVec
end
function Vector2D:Limit(vec,l)
local tempVec = Vector2D:new(vec.x,vec.y)
if tempVec.x > l then
tempVec.x = l
end
if tempVec.y > l then
tempVec.y = l
end
return tempVec
end
function Vector2D:Add(vec1, vec2)
local vec = Vector2D:new(0,0)
vec.x = vec1.x + vec2.x
vec.y = vec1.y + vec2.y
return vec
end
function Vector2D:Sub(vec1, vec2)
local vec = Vector2D:new(0,0)
vec.x = vec1.x - vec2.x
vec.y = vec1.y - vec2.y
return vec
end
function Vector2D:Mult(vec, s)
local tempVec = Vector2D:new(0,0)
tempVec.x = vec.x * s
tempVec.y = vec.y * s
return tempVec
end
function Vector2D:Div(vec, s)
local tempVec = Vector2D:new(0,0)
tempVec.x = vec.x / s
tempVec.y = vec.y / s
return tempVec
end
function Vector2D:Dist(vec1, vec2)
return math.sqrt( (vec2.x - vec1.x) + (vec2.y - vec1.y) )
end
return Vector2D