[refactor]: use fromPolar() in randomDirection()

More compact code and one less temporary table in
vector:randomDirection()
This commit is contained in:
Matthias Richter 2017-11-04 11:19:27 +01:00
parent cdafc00075
commit 96c9648a62
2 changed files with 8 additions and 10 deletions

View File

@ -87,14 +87,11 @@ local function randomDirection(len_min, len_max)
len_min = len_min or 1
len_max = len_max or len_min
assert(len_max > 0, "randomDirection: len_max must be greater than zero")
assert(len_max >= len_min, "randomDirection: len_max must be greater than or equal to len_min")
assert(len_max > 0, "len_max must be greater than zero")
assert(len_max >= len_min, "len_max must be greater than or equal to len_min")
local range = len_max - len_min
local rnd = math.random() * range
local x,y = fromPolar(math.random()*2*math.pi)
return x * (rnd + len_min), y * (rnd + len_min)
return fromPolar(math.random()*2*math.pi,
math.random() * (len_max-len_min) + len_min)
end
local function toPolar(x, y)

View File

@ -44,12 +44,13 @@ local function randomDirection(len_min, len_max)
len_min = len_min or 1
len_max = len_max or len_min
assert(len_max > 0, "randomDirection: len_max must be greater than zero")
assert(len_max >= len_min, "randomDirection: len_max must be greater than or equal to len_min")
assert(len_max > 0, "len_max must be greater than zero")
assert(len_max >= len_min, "len_max must be greater than or equal to len_min")
local range = len_max - len_min
local rnd = math.random() * range
return fromPolar(math.random()*2*math.pi) * (rnd + len_min)
return fromPolar(math.random()*2*math.pi,
math.random() * (len_max-len_min) + len_min)
end
local function isvector(v)