38 lines
1.0 KiB
Lua
Raw Normal View History

2025-01-18 21:26:02 +08:00
---clamp
---@param v number @number
---@param Min number @min
---@param Max number @max
---@return number @clamp v between Min and Max
function math.clamp(v, Min, Max)
Min = math.min(Min, Max)
Max = math.max(Min, Max)
if v < Min then
return Min
end
if v > Max then
return Max
end
return v
end
---clamp
---@param a number @First number to compare
---@param b number @Second number to compare
---@param Tolerance number @Maximum allowed difference for considering them as 'nearly equal'
---@return boolean @true if a and b are nearly equal
function math.isNearlyEqual(a, b, Tolerance)
if Tolerance == nil then
Tolerance = 0.01
end
return math.abs(a - b) <= Tolerance
end
--- 在圆中随机生成一个点
function math.RandomCirclePoint(Radius, Center)
local angle = math.random() * 2 * math.pi;
local tr = Radius * math.sqrt(math.random());
local x = Center.X + tr * math.cos(angle);
local y = Center.Y + tr * math.sin(angle);
return { X = x, Y = y };
end