Skip to content

Commit

Permalink
Add Lua helper functions vector.apply(v) math.sign(x, tolerance)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallJoker authored and kahrl committed Dec 6, 2014
1 parent 0e78aa2 commit 2fd14e1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions builtin/common/misc_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ function math.hypot(x, y)
return x * math.sqrt(1 + t * t)
end

--------------------------------------------------------------------------------
function math.sign(x, tolerance)
tolerance = tolerance or 0
if x > tolerance then
return 1
elseif x < -tolerance then
return -1
end
return 0
end

--------------------------------------------------------------------------------
function get_last_folder(text,count)
local parts = text:split(DIR_DELIM)
Expand Down
8 changes: 8 additions & 0 deletions builtin/common/vector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ function vector.round(v)
}
end

function vector.apply(v, func)
return {
x = func(v.x),
y = func(v.y),
z = func(v.z)
}
end

function vector.distance(a, b)
local x = a.x - b.x
local y = a.y - b.y
Expand Down
4 changes: 4 additions & 0 deletions doc/lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,7 @@ vector.distance(p1, p2) -> number
vector.length(v) -> number
vector.normalize(v) -> vector
vector.round(v) -> vector
vector.apply(v, func) -> vector
vector.equals(v1, v2) -> bool
For the following functions x can be either a vector or a number.
vector.add(v, x) -> vector
Expand All @@ -1296,6 +1297,9 @@ dump(obj, dumped={})
math.hypot(x, y)
^ Get the hypotenuse of a triangle with legs x and y.
Useful for distance calculation.
math.sign(x, tolerance)
^ Get the sign of a number
Optional: Also returns 0 when the absolute value is within the tolerance (default 0)
string:split(separator)
^ e.g. string:split("a,b", ",") == {"a","b"}
string:trim()
Expand Down

0 comments on commit 2fd14e1

Please sign in to comment.