From 9dee80e2040adca30e2f1a766d5801225c800980 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: Wed, 25 Dec 2024 11:23:47 +0800 Subject: [PATCH] - Add function Utils.pointInPolygon, Utils.doString, Utils.drawPolygon. --- Scripts/Utils.lua | 77 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/Scripts/Utils.lua b/Scripts/Utils.lua index 0db9781..15ff9af 100644 --- a/Scripts/Utils.lua +++ b/Scripts/Utils.lua @@ -279,4 +279,81 @@ do end --if coa_name == 'red' or coa_name == 'blue' and type(coa_data) == 'table' then end --for coa_name, coa_data in pairs(mission.coalition) do end + + function Utils.pointInPolygon(point, poly, maxalt) + point = Utils.makeVec3(point) + local px = point.x + local pz = point.z + local cn = 0 + local newpoly = Utils.deepCopy(poly) + + if not maxalt or (point.y <= maxalt) then + local polysize = #newpoly + newpoly[#newpoly + 1] = newpoly[1] + + newpoly[1] = Utils.makeVec3(newpoly[1]) + + for k = 1, polysize do + newpoly[k+1] = Utils.makeVec3(newpoly[k+1]) + if ((newpoly[k].z <= pz) and (newpoly[k+1].z > pz)) or ((newpoly[k].z > pz) and (newpoly[k+1].z <= pz)) then + local vt = (pz - newpoly[k].z) / (newpoly[k+1].z - newpoly[k].z) + if (px < newpoly[k].x + vt*(newpoly[k+1].x - newpoly[k].x)) then + cn = cn + 1 + end + end + end + + return cn%2 == 1 + else + return false + end + end + + function Utils.doString(s) + local f, err = loadstring(s) + if f then + return true, f() + else + return false, err + end + end + + function Utils.drawPolygon(Coalition,MarkID,vecs,Color,FillColor,LineType,ReadOnly,Text) + local s=string.format("trigger.action.markupToAll(7, %d, %d,", Coalition, MarkID) + for _,vec in pairs(vecs) do + --s=s..string.format("%s,", UTILS._OneLineSerialize(vec)) + s=s..string.format("{x=%.1f, y=%.1f, z=%.1f},", vec.x, vec.y, vec.z) + end + s=s..string.format("{%.3f, %.3f, %.3f, %.3f},", Color[1], Color[2], Color[3], Color[4]) + s=s..string.format("{%.3f, %.3f, %.3f, %.3f},", FillColor[1], FillColor[2], FillColor[3], FillColor[4]) + s=s..string.format("%d,", LineType or 1) + s=s..string.format("%s", tostring(ReadOnly)) + if Text and type(Text)=="string" and string.len(Text)>0 then + s=s..string.format(", \"%s\"", tostring(Text)) + end + s=s..")" + + -- Execute string command + local success=Utils.doString(s) + + if not success then + self:E("ERROR: Could not draw polygon") + env.info(s) + end + end + + + + + + + + + + + + + + + end \ No newline at end of file