]> git.lizzy.rs Git - minetest.git/blob - builtin/common/vector.lua
Test crafting hash type only once for a recipe
[minetest.git] / builtin / common / vector.lua
1
2 vector = {}
3
4 function vector.new(a, b, c)
5         if type(a) == "table" then
6                 assert(a.x and a.y and a.z, "Invalid vector passed to vector.new()")
7                 return {x=a.x, y=a.y, z=a.z}
8         elseif a then
9                 assert(b and c, "Invalid arguments for vector.new()")
10                 return {x=a, y=b, z=c}
11         end
12         return {x=0, y=0, z=0}
13 end
14
15 function vector.equals(a, b)
16         return a.x == b.x and
17                a.y == b.y and
18                a.z == b.z
19 end
20
21 function vector.length(v)
22         return math.hypot(v.x, math.hypot(v.y, v.z))
23 end
24
25 function vector.normalize(v)
26         local len = vector.length(v)
27         if len == 0 then
28                 return {x=0, y=0, z=0}
29         else
30                 return vector.divide(v, len)
31         end
32 end
33
34 function vector.floor(v)
35         return {
36                 x = math.floor(v.x),
37                 y = math.floor(v.y),
38                 z = math.floor(v.z)
39         }
40 end
41
42 function vector.round(v)
43         return {
44                 x = math.floor(v.x + 0.5),
45                 y = math.floor(v.y + 0.5),
46                 z = math.floor(v.z + 0.5)
47         }
48 end
49
50 function vector.apply(v, func)
51         return {
52                 x = func(v.x),
53                 y = func(v.y),
54                 z = func(v.z)
55         }
56 end
57
58 function vector.distance(a, b)
59         local x = a.x - b.x
60         local y = a.y - b.y
61         local z = a.z - b.z
62         return math.hypot(x, math.hypot(y, z))
63 end
64
65 function vector.direction(pos1, pos2)
66         return vector.normalize({
67                 x = pos2.x - pos1.x,
68                 y = pos2.y - pos1.y,
69                 z = pos2.z - pos1.z
70         })
71 end
72
73 function vector.angle(a, b)
74         local dotp = a.x * b.x + a.y * b.y + a.z * b.z
75         local cpx = a.y * b.z - a.z * b.y
76         local cpy = a.z * b.x - a.x * b.z
77         local cpz = a.x * b.y - a.y * b.x
78         local crossplen = math.sqrt(cpx ^ 2 + cpy ^ 2 + cpz ^ 2)
79         return math.atan2(crossplen, dotp)
80 end
81
82 function vector.add(a, b)
83         if type(b) == "table" then
84                 return {x = a.x + b.x,
85                         y = a.y + b.y,
86                         z = a.z + b.z}
87         else
88                 return {x = a.x + b,
89                         y = a.y + b,
90                         z = a.z + b}
91         end
92 end
93
94 function vector.subtract(a, b)
95         if type(b) == "table" then
96                 return {x = a.x - b.x,
97                         y = a.y - b.y,
98                         z = a.z - b.z}
99         else
100                 return {x = a.x - b,
101                         y = a.y - b,
102                         z = a.z - b}
103         end
104 end
105
106 function vector.multiply(a, b)
107         if type(b) == "table" then
108                 return {x = a.x * b.x,
109                         y = a.y * b.y,
110                         z = a.z * b.z}
111         else
112                 return {x = a.x * b,
113                         y = a.y * b,
114                         z = a.z * b}
115         end
116 end
117
118 function vector.divide(a, b)
119         if type(b) == "table" then
120                 return {x = a.x / b.x,
121                         y = a.y / b.y,
122                         z = a.z / b.z}
123         else
124                 return {x = a.x / b,
125                         y = a.y / b,
126                         z = a.z / b}
127         end
128 end
129
130 function vector.sort(a, b)
131         return {x = math.min(a.x, b.x), y = math.min(a.y, b.y), z = math.min(a.z, b.z)},
132                 {x = math.max(a.x, b.x), y = math.max(a.y, b.y), z = math.max(a.z, b.z)}
133 end