Python: find area of polygon from xyz coordinates

后端 未结 5 1152
你的背包
你的背包 2020-12-28 18:07

I\'m trying to use the shapely.geometry.Polygon module to find the area of polygons but it performs all calculations on the xy plane. This is fine

5条回答
  •  独厮守ぢ
    2020-12-28 18:45

    Fyi, here is the same algorithm in Mathematica, with a baby unit test

    ClearAll[vertexPairs, testPoly, area3D, planeUnitNormal, pairwise];
    pairwise[list_, fn_] := MapThread[fn, {Drop[list, -1], Drop[list, 1]}];
    vertexPairs[Polygon[{points___}]] := Append[{points}, First[{points}]];
    testPoly = Polygon[{{20, -30, 0}, {40, -30, 0}, {40, -30, 20}, {20, -30, 20}}];
    planeUnitNormal[Polygon[{points___}]] :=
      With[{ps = Take[{points}, 3]},
       With[{p0 = First[ps]},
        With[{qs = (# - p0) & /@ Rest[ps]},
         Normalize[Cross @@ qs]]]];
    area3D[p : Polygon[{polys___}]] :=
      With[{n = planeUnitNormal[p], vs = vertexPairs[p]},
       With[{areas = (Dot[n, #]) & /@ pairwise[vs, Cross]},
        Plus @@ areas/2]];
    area3D[testPoly]
    

提交回复
热议问题