aboutsummaryrefslogtreecommitdiff
path: root/builtin/common/vector.lua
diff options
context:
space:
mode:
authorDS <vorunbekannt75@web.de>2021-04-05 15:55:56 +0200
committerGitHub <noreply@github.com>2021-04-05 15:55:56 +0200
commit23325277659132e95b346307b591c944625bda16 (patch)
tree29523e7d4334630a302ab573810643e87e61f38a /builtin/common/vector.lua
parent19c283546c5418382ed3ab648ca165ef1cc7994b (diff)
downloadminetest-23325277659132e95b346307b591c944625bda16.tar.gz
minetest-23325277659132e95b346307b591c944625bda16.tar.bz2
minetest-23325277659132e95b346307b591c944625bda16.zip
Add vector.to_string and vector.from_string (#10323)
Writing vectors as strings is very common and should belong to `vector.*`. `minetest.pos_to_string` is also too long to write, implies that one should only use it for positions and leaves no spaces after the commas.
Diffstat (limited to 'builtin/common/vector.lua')
-rw-r--r--builtin/common/vector.lua16
1 files changed, 16 insertions, 0 deletions
diff --git a/builtin/common/vector.lua b/builtin/common/vector.lua
index b04c12610..2ef8fc617 100644
--- a/builtin/common/vector.lua
+++ b/builtin/common/vector.lua
@@ -12,6 +12,22 @@ function vector.new(a, b, c)
return {x=0, y=0, z=0}
end
+function vector.from_string(s, init)
+ local x, y, z, np = string.match(s, "^%s*%(%s*([^%s,]+)%s*[,%s]%s*([^%s,]+)%s*[,%s]" ..
+ "%s*([^%s,]+)%s*[,%s]?%s*%)()", init)
+ x = tonumber(x)
+ y = tonumber(y)
+ z = tonumber(z)
+ if not (x and y and z) then
+ return nil
+ end
+ return {x = x, y = y, z = z}, np
+end
+
+function vector.to_string(v)
+ return string.format("(%g, %g, %g)", v.x, v.y, v.z)
+end
+
function vector.equals(a, b)
return a.x == b.x and
a.y == b.y and