2022-01-15 00:08:58 +02:00
|
|
|
-- Copyright (C) 2022 veden
|
|
|
|
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU General Public License as published by
|
|
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU General Public License for more details.
|
|
|
|
|
|
|
|
-- You should have received a copy of the GNU General Public License
|
|
|
|
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
if MathUtilsG then
|
|
|
|
return MathUtilsG
|
2019-02-16 06:17:30 +02:00
|
|
|
end
|
2023-03-11 22:53:06 +02:00
|
|
|
local MathUtils = {}
|
2016-10-15 02:00:18 +02:00
|
|
|
|
|
|
|
-- imports
|
|
|
|
|
|
|
|
-- constants
|
|
|
|
|
2022-07-07 06:14:00 +02:00
|
|
|
local TICKS_A_MINUTE = 60 * 60
|
2016-10-15 02:00:18 +02:00
|
|
|
|
|
|
|
-- imported functions
|
|
|
|
|
2016-11-04 01:51:35 +02:00
|
|
|
local mSqrt = math.sqrt
|
|
|
|
local mLog10 = math.log10
|
2016-10-15 02:00:18 +02:00
|
|
|
|
2018-01-14 02:12:09 +02:00
|
|
|
local mFloor = math.floor
|
2018-01-26 07:13:47 +02:00
|
|
|
local mAbs = math.abs
|
2017-07-01 06:36:23 +02:00
|
|
|
|
2017-06-13 05:16:43 +02:00
|
|
|
-- module code
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.roundToFloor(number, multiple)
|
2018-01-21 09:42:47 +02:00
|
|
|
return mFloor(number / multiple) * multiple
|
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.roundToNearest(number, multiple)
|
2016-10-15 02:00:18 +02:00
|
|
|
local num = number + (multiple * 0.5)
|
|
|
|
return num - (num % multiple)
|
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.randomTickEvent(rg, tick, low, high)
|
|
|
|
return tick + MathUtils.randomTickDuration(rg, low, high)
|
2022-10-26 19:55:00 +02:00
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.randomTickDuration(rg, low, high)
|
2019-04-25 08:13:22 +02:00
|
|
|
local range = high - low
|
2021-12-06 01:33:24 +02:00
|
|
|
local minutesToTick = (range * rg()) + low
|
2023-03-11 22:53:06 +02:00
|
|
|
return MathUtils.roundToNearest(TICKS_A_MINUTE * minutesToTick, 1)
|
2019-11-30 02:49:22 +02:00
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.distort(xorRandom, num, stdDev, min, max)
|
2021-02-14 06:49:54 +02:00
|
|
|
local amin = min or num * 0.70
|
|
|
|
local amax = max or num * 1.30
|
2020-04-13 00:41:45 +02:00
|
|
|
local sd = stdDev or 0.17
|
2019-11-30 02:49:22 +02:00
|
|
|
if (num < 0) then
|
2021-02-14 08:24:17 +02:00
|
|
|
local t = amin
|
|
|
|
amin = amax
|
2021-02-14 06:49:54 +02:00
|
|
|
amax = t
|
2019-11-30 02:49:22 +02:00
|
|
|
end
|
2023-03-11 22:53:06 +02:00
|
|
|
return MathUtils.roundToNearest(MathUtils.gaussianRandomRangeRG(num, num * sd, amin, amax, xorRandom), 0.01)
|
2016-10-15 02:00:18 +02:00
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.linearInterpolation(percent, min, max)
|
2018-02-14 10:28:42 +02:00
|
|
|
return ((max - min) * percent) + min
|
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.xorRandom(state)
|
2018-01-14 02:12:09 +02:00
|
|
|
local xor = bit32.bxor
|
|
|
|
local lshift = bit32.lshift
|
|
|
|
local rshift = bit32.rshift
|
2019-02-16 06:17:30 +02:00
|
|
|
|
2022-05-02 06:38:38 +02:00
|
|
|
local seed = state + 32685453
|
2018-01-14 02:12:09 +02:00
|
|
|
|
|
|
|
return function()
|
2022-05-02 06:38:38 +02:00
|
|
|
seed = xor(seed, lshift(seed, 13))
|
|
|
|
seed = xor(seed, rshift(seed, 17))
|
|
|
|
seed = xor(seed, lshift(seed, 5))
|
|
|
|
return seed * 2.32830643654e-10 -- 2.32830643654e-10 = 1 / 2^32, 2.32830643708e-10 = 1 / ((2^32)-1)
|
2018-01-14 02:12:09 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-04 01:51:35 +02:00
|
|
|
--[[
|
|
|
|
Used for gaussian random numbers
|
|
|
|
--]]
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.gaussianRandomRG(mean, std_dev, rg)
|
2018-01-14 02:12:09 +02:00
|
|
|
-- marsagliaPolarMethod
|
2019-02-16 06:17:30 +02:00
|
|
|
local iid1
|
|
|
|
local iid2
|
|
|
|
local q
|
2016-11-04 01:51:35 +02:00
|
|
|
repeat
|
2019-10-19 23:04:38 +02:00
|
|
|
iid1 = 2 * rg() + -1
|
|
|
|
iid2 = 2 * rg() + -1
|
|
|
|
q = (iid1 * iid1) + (iid2 * iid2)
|
2018-01-14 02:12:09 +02:00
|
|
|
until (q ~= 0) and (q < 1)
|
|
|
|
local s = mSqrt((-2 * mLog10(q)) / q)
|
|
|
|
local v = iid1 * s
|
2019-02-16 06:17:30 +02:00
|
|
|
|
2018-01-14 02:12:09 +02:00
|
|
|
return mean + (v * std_dev)
|
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.gaussianRandomRangeRG(mean, std_dev, min, max, rg)
|
2018-01-14 02:12:09 +02:00
|
|
|
local r
|
2019-08-17 03:52:16 +02:00
|
|
|
if (min >= max) then
|
2019-10-19 23:04:38 +02:00
|
|
|
return min
|
2018-01-14 02:12:09 +02:00
|
|
|
end
|
|
|
|
repeat
|
2019-10-19 23:04:38 +02:00
|
|
|
local iid1
|
|
|
|
local iid2
|
|
|
|
local q
|
|
|
|
repeat
|
|
|
|
iid1 = 2 * rg() + -1
|
|
|
|
iid2 = 2 * rg() + -1
|
|
|
|
q = (iid1 * iid1) + (iid2 * iid2)
|
|
|
|
until (q ~= 0) and (q < 1)
|
|
|
|
local s = mSqrt((-2 * mLog10(q)) / q)
|
|
|
|
local v = iid1 * s
|
|
|
|
r = mean + (v * std_dev)
|
2018-01-14 02:12:09 +02:00
|
|
|
until (r >= min) and (r <= max)
|
|
|
|
return r
|
2016-11-04 01:51:35 +02:00
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.euclideanDistanceNamed(p1, p2)
|
2017-06-13 05:16:43 +02:00
|
|
|
local xs = p1.x - p2.x
|
|
|
|
local ys = p1.y - p2.y
|
|
|
|
return ((xs * xs) + (ys * ys)) ^ 0.5
|
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.euclideanDistancePoints(x1, y1, x2, y2)
|
2017-06-13 05:16:43 +02:00
|
|
|
local xs = x1 - x2
|
|
|
|
local ys = y1 - y2
|
|
|
|
return ((xs * xs) + (ys * ys)) ^ 0.5
|
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.manhattenDistancePoints(x1, y1, x2, y2)
|
2022-03-20 01:29:12 +02:00
|
|
|
return mAbs((x1 - x2) + (y1 - y2))
|
2018-01-26 07:13:47 +02:00
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.euclideanDistanceArray(p1, p2)
|
2017-06-13 05:16:43 +02:00
|
|
|
local xs = p1[1] - p2[1]
|
|
|
|
local ys = p1[2] - p2[2]
|
|
|
|
return ((xs * xs) + (ys * ys)) ^ 0.5
|
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
function MathUtils.distortPosition(rg, position, size)
|
|
|
|
local xDistort = MathUtils.gaussianRandomRangeRG(1, 0.5, 0, 2, rg) - 1
|
|
|
|
local yDistort = MathUtils.gaussianRandomRangeRG(1, 0.5, 0, 2, rg) - 1
|
2019-10-21 02:53:16 +02:00
|
|
|
position.x = position.x + (xDistort * size)
|
|
|
|
position.y = position.y + (yDistort * size)
|
2019-04-08 07:22:02 +02:00
|
|
|
return position
|
2017-06-13 05:16:43 +02:00
|
|
|
end
|
|
|
|
|
2023-03-23 02:59:28 +02:00
|
|
|
function MathUtils.distortPositionConcentricCircles(rg, position, size, min)
|
|
|
|
local xDistort = MathUtils.gaussianRandomRangeRG(1, 0.5, 0, 2, rg) - 1
|
|
|
|
local yDistort = MathUtils.gaussianRandomRangeRG(1, 0.5, 0, 2, rg) - 1
|
|
|
|
local xModifier = (xDistort * size)
|
|
|
|
if xModifier < 0 then
|
|
|
|
xModifier = xModifier + -min
|
|
|
|
else
|
|
|
|
xModifier = xModifier + min
|
|
|
|
end
|
|
|
|
local yModifier = (yDistort * size)
|
|
|
|
if yModifier < 0 then
|
|
|
|
yModifier = yModifier + -min
|
|
|
|
else
|
|
|
|
yModifier = yModifier + min
|
|
|
|
end
|
|
|
|
|
|
|
|
position.x = position.x + xModifier
|
|
|
|
position.y = position.y + yModifier
|
|
|
|
return position
|
|
|
|
end
|
|
|
|
|
2023-03-11 22:53:06 +02:00
|
|
|
MathUtilsG = MathUtils
|
|
|
|
return MathUtils
|