3
$\begingroup$

I have many polygons of the following form that I wish to do more complicated processing and analysis based on it's updated location (such as relationships between geometries).

Translate[Polygon[{{0, 0}, {0, 50}, {100, 50}, {100, 0}}], {954, 840}]

I thought I had found my solution of how to recover the coordinates of a translated Polygon from an earlier thread about Points. However, when I tried to applied Normal M12.1 just returns the expression again.

Normal[Translate[Polygon[{{0, 0}, {0, 50}, {100, 50}, {100, 0}}], {954, 840}]]

I've tried some more complicated ways to extract the coordinates from a Graphics object to no avail. Does anyone have any suggestions about how they would recover the Polygons updated extents?

$\endgroup$

2 Answers 2

1
$\begingroup$
translateToNormal[t_Translate] := TranslationTransform[#2] /@ # & @@ t

tp = Translate[Polygon[{{0, 0}, {0, 50}, {100, 50}, {100, 0}}], {954, 840}]

translateToNormal[tp]
 Polygon[{{954, 840}, {954, 890}, {1054, 890}, {1054, 840}}]

Alternatively,

translateToNormal2 = # /. Translate -> (TranslationTransform[#2] /@ # &) &;

translateToNormal2 @ tp
 Polygon[{{954, 840}, {954, 890}, {1054, 890}, {1054, 840}}]
Graphics[{FaceForm[], EdgeForm[{AbsoluteThickness[10], Red}], translateToNormal[tp],
  EdgeForm[{Thin, Black}], tp}]

enter image description here

To get the coordinates:

normalCoords[t_Translate] := TranslationTransform[#2] @ #[[1]] & @@ t
normalCoords[tp]
{{954, 840}, {954, 890}, {1054, 890}, {1054, 840}}
$\endgroup$
1
  • $\begingroup$ It works thanks. I wasn't able to reverse what #2 and # are actioning upon? $\endgroup$
    – BBirdsell
    Commented Dec 10, 2020 at 6:03
0
$\begingroup$

Using TransformedRegion:

Clear["Global`*"];
poly = Polygon[{{0, 0}, {0, 50}, {100, 50}, {100, 0}}];
tvec = {954, 840};

tpoly = TransformedRegion[poly
   , TranslationTransform[tvec]];

Results:

PolygonCoordinates[tpoly]

{{954, 840}, {954, 890}, {1054, 840}, {1054, 890}}

RegionQ /@ {poly, tpoly}

{True, True}

Graphics[{Red, poly, Blue, tpoly
  , Dashed, Black
  , Arrow[{RegionCentroid@poly
    , RegionCentroid@poly + {tvec[[1]], 0}}]
  , Arrow[{RegionCentroid@poly + {tvec[[1]], 0}
    , RegionCentroid@tpoly}]
  }]

enter image description here


More operations can be cascaded using a Dot operator. For example, a rotation:

upoly = TransformedRegion[poly
   , TranslationTransform[tvec] . RotationTransform[π/4]];

PolygonCoordinates@upoly

{{954 - 25 Sqrt[2], 840 + 25 Sqrt[2]}, {954, 840}, {954 + 25 Sqrt[2], 840 + 75 Sqrt[2]}, {954 + 50 Sqrt[2], 840 + 50 Sqrt[2]}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.