- Add getAvgPoint()

- Add getRandPointInCircle()
- Add getRandomPointInPoly()
-Add
This commit is contained in:
2025-01-02 12:42:22 +08:00
parent ac517b24f6
commit 14c4e124b7
+88 -1
View File
@@ -165,7 +165,7 @@ do
function Utils.getDir(vec, point) function Utils.getDir(vec, point)
local dir = math.atan2(vec.z, vec.x) local dir = math.atan2(vec.z, vec.x)
if point then if point then
dir = dir + mist.getNorthCorrection(point) dir = dir + Utils.getNorthCorrection(point)
end end
if dir < 0 then if dir < 0 then
dir = dir + 2 * math.pi -- put dir in range of 0 to 2*pi dir = dir + 2 * math.pi -- put dir in range of 0 to 2*pi
@@ -181,6 +181,93 @@ do
return dir return dir
end end
function Utils.getAvgPoint(points)
local avgX, avgY, avgZ, totNum = 0, 0, 0, 0
for i = 1, #points do
--log:warn(points[i])
local nPoint = Utils.makeVec3(points[i])
if nPoint.z then
avgX = avgX + nPoint.x
avgY = avgY + nPoint.y
avgZ = avgZ + nPoint.z
totNum = totNum + 1
end
end
if totNum ~= 0 then
return {x = avgX/totNum, y = avgY/totNum, z = avgZ/totNum}
end
end
-- need to return a Vec3 or Vec2?
function Utils.getRandPointInCircle(p, r, innerRadius, maxA, minA)
local point = Utils.makeVec3(p)
local theta = 2*math.pi*math.random()
local radius = r or 1000
local minR = innerRadius or 0
if maxA and not minA then
theta = math.rad(math.random(0, maxA - math.random()))
elseif maxA and minA then
if minA < maxA then
theta = math.rad(math.random(minA, maxA) - math.random())
else
theta = math.rad(math.random(maxA, minA) - math.random())
end
end
local rad = math.random() + math.random()
if rad > 1 then
rad = 2 - rad
end
local radMult
if minR and minR <= radius then
--radMult = (radius - innerRadius)*rad + innerRadius
radMult = radius * math.sqrt((minR^2 + (radius^2 - minR^2) * math.random()) / radius^2)
else
radMult = radius*rad
end
local rndCoord
if radius > 0 then
rndCoord = {x = math.cos(theta)*radMult + point.x, y = math.sin(theta)*radMult + point.z}
else
rndCoord = {x = point.x, y = point.z}
end
return rndCoord
end
function Utils.getRandomPointInPoly(zone)
--env.info('Zone Size: '.. #zone)
local avg = Utils.getAvgPoint(zone)
--log:warn(avg)
local radius = 0
local minR = math.huge
local newCoord = {}
for i = 1, #zone do
if Utils.get2DDist(avg, zone[i]) > radius then
radius = Utils.get2DDist(avg, zone[i])
end
if Utils.get2DDist(avg, zone[i]) < minR then
minR = Utils.utils.get2DDist(avg, zone[i])
end
end
--log:warn('minR: $1', minR)
--log:warn('Radius: $1', radius)
local lSpawnPos = {}
for j = 1, 100 do
newCoord = Utils.getRandPointInCircle(avg, radius)
if Utils.pointInPolygon(newCoord, zone) then
break
end
if j == 100 then
newCoord = Utils.getRandPointInCircle(avg, radius)
log:warn("Failed to find point in poly; Giving random point from center of the poly")
end
end
return newCoord
end
--- Converts a TACAN Channel/Mode couple into a frequency in Hz. --- Converts a TACAN Channel/Mode couple into a frequency in Hz.
-- @param #number TACANChannel The TACAN channel, i.e. the 10 in "10X". -- @param #number TACANChannel The TACAN channel, i.e. the 10 in "10X".
-- @param #string TACANMode The TACAN mode, i.e. the "X" in "10X". -- @param #string TACANMode The TACAN mode, i.e. the "X" in "10X".