-
Notifications
You must be signed in to change notification settings - Fork 4
/
Vector2D.lua
213 lines (170 loc) · 3.68 KB
/
Vector2D.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
Vector2D = {}
local mSqrt = math.sqrt;
local mCos = math.cos;
local mSin = math.sin;
local mAtan = math.atan;
local mAtan2 = math.atan2;
local pi = math.pi;
function Vector2D:new(x, y) -- The constructor
local object = { x = x, y = y }
setmetatable(object, { __index = Vector2D }) -- Inheritance
return object
end
function Vector2D:copy()
return Vector2D:new(self.x, self.y)
end
function Vector2D:copyTo(otherVector)
otherVector.x = self.x
otherVector.y = self.y
end
function Vector2D:magnitude()
return mSqrt(self.x^2 + self.y^2)
end
function Vector2D:length()
return mSqrt(self.x * self.x + self.y *self.y);
end
function Vector2D:Length(vec)
return mSqrt(vec.x * vec.x + vec.y *vec.y);
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:cos_sin()
self.x = mCos(self.x);
self.y = mSin(self.y)
end
function Vector2D:dotProduct(vec)
return self.x * vec.y - self.y * vec.x;
end
function Vector2D:sign(vec)
local t = Vector2D:perpendicular(self);
t = t:dotProduct(vec);
if t < 0 then
return -1;
else
return 1;
end
end
function Vector2D:perpendicular(vec)
return Vector2D:new(-vec.y , vec.x);
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:angle()
local ang = mAtan(self.x , self.y);
if(self.y< 0 and self.x > 0) then
return ang;
end
if(self.y < 0 and self.x < 0) or(self.y > 0 and self.x < 0) then
return ang + pi;
end
return ang + (pi *2);
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:truncate(s)
if #self > s then
self:normalize();
self = self:mult(s);
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 mSqrt( (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:toRad(vec)
return mAtan2(vec.y,vec.y);
end
function Vector2D:Dist(vec1, vec2)
return mSqrt( (vec2.x - vec1.x) + (vec2.y - vec1.y) )
end
function Vector2D:distance(vec1)
local dx = vec1.x - self.x;
local dy = vec1.y - self.y;
return mSqrt(dx*dx + dy*dy);
end
function Vector2D:Distance(vec1, vec2)
local dx = vec2.x - vec1.x;
local dy = vec2.y - vec1.y;
return mSqrt(dx*dx + dy*dy);
end
return Vector2D