From 14c4e124b75fc9c7d1918e0610efe34281c5ede8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=BC=E5=A4=9C=E7=8B=8E=E6=98=B5?= <1046295287@qq.com> Date: Thu, 2 Jan 2025 12:42:22 +0800 Subject: [PATCH] - Add getAvgPoint() - Add getRandPointInCircle() - Add getRandomPointInPoly() -Add --- Scripts/Utils.lua | 89 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/Scripts/Utils.lua b/Scripts/Utils.lua index 0ecdf3e..712fd3b 100644 --- a/Scripts/Utils.lua +++ b/Scripts/Utils.lua @@ -165,7 +165,7 @@ do function Utils.getDir(vec, point) local dir = math.atan2(vec.z, vec.x) if point then - dir = dir + mist.getNorthCorrection(point) + dir = dir + Utils.getNorthCorrection(point) end if dir < 0 then dir = dir + 2 * math.pi -- put dir in range of 0 to 2*pi @@ -181,6 +181,93 @@ do return dir 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. -- @param #number TACANChannel The TACAN channel, i.e. the 10 in "10X". -- @param #string TACANMode The TACAN mode, i.e. the "X" in "10X".