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