Skip to content

Commit fba2650

Browse files
committedNov 1, 2013
Add more checks to vector functions
1 parent 1cbba87 commit fba2650

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed
 

‎builtin/vector.lua

+25-14
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
11

22
vector = {}
33

4+
local function assert_vector(v)
5+
assert(type(v) == "table" and v.x and v.y and v.z, "Invalid vector")
6+
end
7+
48
function vector.new(a, b, c)
5-
assert(a)
69
if type(a) == "table" then
10+
assert(a.x and a.y and a.z, "Invalid vector passed to vector.new()")
711
return {x=a.x, y=a.y, z=a.z}
8-
else
9-
assert(b and c)
12+
elseif a then
13+
assert(b and c, "Invalid arguments for vector.new()")
1014
return {x=a, y=b, z=c}
1115
end
1216
return {x=0, y=0, z=0}
1317
end
1418

1519
function vector.equals(a, b)
16-
assert(a and b)
20+
assert_vector(a)
21+
assert_vector(b)
1722
return a.x == b.x and
1823
a.y == b.y and
1924
a.z == b.z
2025
end
2126

2227
function vector.length(v)
23-
assert(v)
28+
assert_vector(v)
2429
return math.hypot(v.x, math.hypot(v.y, v.z))
2530
end
2631

2732
function vector.normalize(v)
28-
assert(v)
33+
assert_vector(v)
2934
local len = vector.length(v)
3035
if len == 0 then
31-
return vector.new()
36+
return {x=0, y=0, z=0}
3237
else
3338
return vector.divide(v, len)
3439
end
3540
end
3641

3742
function vector.round(v)
38-
assert(v)
43+
assert_vector(v)
3944
return {
4045
x = math.floor(v.x + 0.5),
4146
y = math.floor(v.y + 0.5),
@@ -44,15 +49,17 @@ function vector.round(v)
4449
end
4550

4651
function vector.distance(a, b)
47-
assert(a and b)
52+
assert_vector(a)
53+
assert_vector(b)
4854
local x = a.x - b.x
4955
local y = a.y - b.y
5056
local z = a.z - b.z
5157
return math.hypot(x, math.hypot(y, z))
5258
end
5359

5460
function vector.direction(pos1, pos2)
55-
assert(pos1 and pos2)
61+
assert_vector(pos1)
62+
assert_vector(pos2)
5663
local x_raw = pos2.x - pos1.x
5764
local y_raw = pos2.y - pos1.y
5865
local z_raw = pos2.z - pos1.z
@@ -82,7 +89,8 @@ end
8289

8390

8491
function vector.add(a, b)
85-
assert(a and b)
92+
assert_vector(a)
93+
assert_vector(b)
8694
if type(b) == "table" then
8795
return {x = a.x + b.x,
8896
y = a.y + b.y,
@@ -95,7 +103,8 @@ function vector.add(a, b)
95103
end
96104

97105
function vector.subtract(a, b)
98-
assert(a and b)
106+
assert_vector(a)
107+
assert_vector(b)
99108
if type(b) == "table" then
100109
return {x = a.x - b.x,
101110
y = a.y - b.y,
@@ -108,7 +117,8 @@ function vector.subtract(a, b)
108117
end
109118

110119
function vector.multiply(a, b)
111-
assert(a and b)
120+
assert_vector(a)
121+
assert_vector(b)
112122
if type(b) == "table" then
113123
return {x = a.x * b.x,
114124
y = a.y * b.y,
@@ -121,7 +131,8 @@ function vector.multiply(a, b)
121131
end
122132

123133
function vector.divide(a, b)
124-
assert(a and b)
134+
assert_vector(a)
135+
assert_vector(b)
125136
if type(b) == "table" then
126137
return {x = a.x / b.x,
127138
y = a.y / b.y,

0 commit comments

Comments
 (0)
Please sign in to comment.