1
1
2
2
vector = {}
3
3
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
+
4
8
function vector .new (a , b , c )
5
- assert (a )
6
9
if type (a ) == " table" then
10
+ assert (a .x and a .y and a .z , " Invalid vector passed to vector.new()" )
7
11
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() " )
10
14
return {x = a , y = b , z = c }
11
15
end
12
16
return {x = 0 , y = 0 , z = 0 }
13
17
end
14
18
15
19
function vector .equals (a , b )
16
- assert (a and b )
20
+ assert_vector (a )
21
+ assert_vector (b )
17
22
return a .x == b .x and
18
23
a .y == b .y and
19
24
a .z == b .z
20
25
end
21
26
22
27
function vector .length (v )
23
- assert (v )
28
+ assert_vector (v )
24
29
return math .hypot (v .x , math .hypot (v .y , v .z ))
25
30
end
26
31
27
32
function vector .normalize (v )
28
- assert (v )
33
+ assert_vector (v )
29
34
local len = vector .length (v )
30
35
if len == 0 then
31
- return vector . new ()
36
+ return { x = 0 , y = 0 , z = 0 }
32
37
else
33
38
return vector .divide (v , len )
34
39
end
35
40
end
36
41
37
42
function vector .round (v )
38
- assert (v )
43
+ assert_vector (v )
39
44
return {
40
45
x = math.floor (v .x + 0.5 ),
41
46
y = math.floor (v .y + 0.5 ),
@@ -44,15 +49,17 @@ function vector.round(v)
44
49
end
45
50
46
51
function vector .distance (a , b )
47
- assert (a and b )
52
+ assert_vector (a )
53
+ assert_vector (b )
48
54
local x = a .x - b .x
49
55
local y = a .y - b .y
50
56
local z = a .z - b .z
51
57
return math .hypot (x , math .hypot (y , z ))
52
58
end
53
59
54
60
function vector .direction (pos1 , pos2 )
55
- assert (pos1 and pos2 )
61
+ assert_vector (pos1 )
62
+ assert_vector (pos2 )
56
63
local x_raw = pos2 .x - pos1 .x
57
64
local y_raw = pos2 .y - pos1 .y
58
65
local z_raw = pos2 .z - pos1 .z
82
89
83
90
84
91
function vector .add (a , b )
85
- assert (a and b )
92
+ assert_vector (a )
93
+ assert_vector (b )
86
94
if type (b ) == " table" then
87
95
return {x = a .x + b .x ,
88
96
y = a .y + b .y ,
@@ -95,7 +103,8 @@ function vector.add(a, b)
95
103
end
96
104
97
105
function vector .subtract (a , b )
98
- assert (a and b )
106
+ assert_vector (a )
107
+ assert_vector (b )
99
108
if type (b ) == " table" then
100
109
return {x = a .x - b .x ,
101
110
y = a .y - b .y ,
@@ -108,7 +117,8 @@ function vector.subtract(a, b)
108
117
end
109
118
110
119
function vector .multiply (a , b )
111
- assert (a and b )
120
+ assert_vector (a )
121
+ assert_vector (b )
112
122
if type (b ) == " table" then
113
123
return {x = a .x * b .x ,
114
124
y = a .y * b .y ,
@@ -121,7 +131,8 @@ function vector.multiply(a, b)
121
131
end
122
132
123
133
function vector .divide (a , b )
124
- assert (a and b )
134
+ assert_vector (a )
135
+ assert_vector (b )
125
136
if type (b ) == " table" then
126
137
return {x = a .x / b .x ,
127
138
y = a .y / b .y ,
0 commit comments