-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVector2.lua
258 lines (203 loc) · 6.06 KB
/
Vector2.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
--[[
MADE BY HASNAIN RAZA
Vector2 is a simple two dimensional vector mathematics class.
DEMO (all features)
local a, b, c = Vector2.new(1, 1), Vector2.new(2, 2), Vector2.new(3, 3)
print(a + b)
print(a:addVectors(b))
print(Vector2.addVectors(a, b))
print(a - b)
print(a:subtractVectors(b))
print(Vector2.subtractVectors(a, b))
print(a * b)
print(a:multiplyVectors(b))
print(Vector2.multiplyVectors(a, b))
print(a / b)
print(a:divideVectors(b))
print(Vector2.divideVectors(a, b))
print(-c)
print(a + 1)
print(a:addScalar(1))
print(Vector2.addScalar(a, b))
print(a - 1)
print(a:subtractScalar(1))
print(Vector2.subtractScalar(a, b))
print(a * 1)
print(a:multiplyScalar(1))
print(Vector2.multiplyScalar(a, b))
print(a / 1)
print(a:multiplyScalar(1))
print(Vector2.multiplyScalar(a, b))
print(c.magnitude)
print(c:getMagnitude())
print(Vector2.getMagnitude(c))
print(c.unit)
print(c:getUnitVector())
print(Vector2.getUnitVector(c))
print(a == b)
print(a:equals(b, 0.01))
print(Vector2.equals(a, b, 0.01))
print(
c:mapVector(function(x, y)
return (x + y), (x - y)
end)
)
It is recommended that you rely on arithmetic operators rather than calling functions
because error handling is done primarily on the operators.
--]]
local Vector2 = {}
--// METAMETHODS //--
local function umn(self)
return (self * -1)
end
local function add(self, value)
if (type(value) == "number") then
return Vector2.addScalar(self, value)
elseif (type(value) == "table") then
if (value.className) and (value.className == "Vector2") then
return Vector2.addVectors(self, value)
else
error("attempt to perform arithmetic (add) on " .. self.className .. " and unknown")
end
else
error("attempt to perform arithmetic (add) on " .. self.className .. " and " .. type(value))
end
end
local function sub(self, value)
if (type(value) == "number") then
return Vector2.subtractScalar(self, value)
elseif (type(value) == "table") then
if (value.className) and (value.className == "Vector2") then
return Vector2.subtractVectors(self, value)
else
error("attempt to perform arithmetic (subtract) on " .. self.className .. " and unknown")
end
else
error("attempt to perform arithmetic (subtract) on " .. self.className .. " and " .. type(value))
end
end
local function mul(self, value)
if (type(value) == "number") then
return Vector2.multiplyScalar(self, value)
elseif (type(value) == "table") then
if (value.className) and (value.className == "Vector2") then
return Vector2.multiplyVectors(self, value)
else
error("attempt to perform arithmetic (multiply) on " .. self.className .. " and unknown")
end
else
error("attempt to perform arithmetic (multiply) on " .. self.className .. " and " .. type(value))
end
end
local function div(self, value)
if (type(value) == "number") then
return Vector2.divideScalar(self, value)
elseif (type(value) == "table") then
if (value.className) and (value.className == "Vector2") then
return Vector2.divideVectors(self, value)
else
error("attempt to perform arithmetic (divide) on " .. self.className .. " and unknown")
end
else
error("attempt to perform arithmetic (divide) on " .. self.className .. " and " .. type(value))
end
end
local function tostringMetamethod(self)
return ("( " .. self.x .. ", " .. self.y .. ")")
end
local function eq(self, value)
return Vector2.equals(self, value, 0.01)
end
--// CONSTRUCTOR //--
function Vector2.new(x, y)
-- Handles Input --
if (x) and (type(x) ~= "number") then error("number expected, got " .. type(x)) end
if (y) and (type(y) ~= "number") then error("number expected, got " .. type(y)) end
----
local dataTable = setmetatable(
{
x = x or 0,
y = y or 0,
className = "Vector2"
},
Vector2
)
local proxyTable = setmetatable(
{
},
{
__index = function(self, index)
if (index == "magnitude") then
return Vector2.getMagnitude(self)
elseif (index == "unit") then
return Vector2.getUnitVector(self)
else
return (dataTable[index])
end
end,
__newindex = function(self, index, newValue)
if (index == "x") or (index == "y") then
if (newValue) and (type(newValue) ~= "number") then
error("number expected, got " .. type(index))
else
dataTable[index] = newValue
end
else
error(newValue .. " cannot be assigned")
end
end,
__unm = umn,
__add = add,
__sub = sub,
__mul = mul,
__div = div,
__eq = eq,
__tostring = tostringMetamethod
}
)
return proxyTable
end
--// METHODS //--
function Vector2.addVectors(firstVector2, secondVector2)
return Vector2.new(firstVector2.x + secondVector2.x, firstVector2.y + secondVector2.y)
end
function Vector2.subtractVectors(firstVector2, secondVector2)
return Vector2.new(firstVector2.x - secondVector2.x, firstVector2.y - secondVector2.y)
end
function Vector2.multiplyVectors(firstVector2, secondVector2)
return Vector2.new(firstVector2.x * secondVector2.x, firstVector2.y * secondVector2.y)
end
function Vector2.divideVectors(firstVector2, secondVector2)
return Vector2.new(firstVector2.x / secondVector2.x, firstVector2.y / secondVector2.y)
end
function Vector2.addScalar(vector2, scalar)
return Vector2.new(vector2.x + scalar, vector2.y + scalar)
end
function Vector2.subtractScalar(vector2, scalar)
return Vector2.new(vector2.x - scalar, vector2.y - scalar)
end
function Vector2.multiplyScalar(vector2, scalar)
return Vector2.new(vector2.x * scalar, vector2.y * scalar)
end
function Vector2.divideScalar(vector2, scalar)
return Vector2.new(vector2.x / scalar, vector2.y / scalar)
end
function Vector2.mapVector(vector2, mapFunction)
local x, y = mapFunction(vector2.x, vector2.y)
return Vector2.new(x, y)
end
function Vector2.equals(firstVector2, secondVector2, epsilon)
return ((firstVector2 - secondVector2).magnitude < epsilon)
end
function Vector2.getMagnitude(vector2)
return math.sqrt(
vector2.x^2 +
vector2.y^2
)
end
function Vector2.getUnitVector(vector2)
return (vector2/vector2.magnitude)
end
--// INSTRUCTIONS //--
Vector2.__index = Vector2
return Vector2