2
$\begingroup$

What is the best way to obtain the transformed polygon coordinates? In the example below g3 still contains GeometricTransformation[Polygon[...]... etc. — not the transformed coordinates.

g1 = Graphics[{Green, 
     Polygon[{{0, 0}, {0, 1}, {1, Sqrt[3]}, {2, 1}, {0, 0}}]}];
g2 = g1 /. {Polygon[x_] :> GeometricTransformation[Polygon[x],
      RotationTransform[-30 Degree, {0, 0}]], Green -> LightBlue};
g3 = Graphics[First /@ {g1, g2}]

enter image description here

g3 // InputForm

Graphics[{{RGBColor[0, 1, 0], Polygon[{{0, 0}, {0, 1}, {1, Sqrt3}, {2, 1}, {0, 0}}]}, {RGBColor[0.87, 0.94, 1], GeometricTransformation[ Polygon[{{0, 0}, {0, 1}, {1, Sqrt3}, {2, 1}, {0, 0}}], {{{Sqrt3/2, 1/2}, {-1/2, Sqrt3/2}}, {0, 0}}]}}]

Edit

Carl Woll's 2017 solution here works, but can this be done with built-in functionality?

NormalizeGraphics@g3 // InputForm

Graphics[{{RGBColor[0, 1, 0], Polygon[{{0, 0}, {0, 1}, {1, Sqrt3}, {2, 1}, {0, 0}}]}, {RGBColor[0.87, 0.94, 1], Polygon[{{0, 0}, {1/2, Sqrt3/2}, {Sqrt3, 1}, {1/2 + Sqrt3, -1 + Sqrt3/2}}, {1, 2, 3, 4}]}}]

Actually it does not work in this case:

g1 = Graphics[{Green,
    Polygon[{{0, 0}, {0, 1}, {1, Sqrt[3]}, {2, 1}, {0, 0}}]}];
g2 = NormalizeGraphics[g1 /. {Polygon[x_] :> GeometricTransformation[Polygon[x],
       RotationTransform[-30 Degree, {0, 0}]], Green -> LightBlue}];
g3 = NormalizeGraphics[g2 /. {Polygon[x_] :> GeometricTransformation[Polygon[x],
       RotationTransform[-30 Degree, {0, 0}]], LightBlue -> Orange}];
g4 = Graphics[First /@ {g1, g2, g3}]

enter image description here

It should look like this

enter image description here

Fixed with

g2 = Normal @ NormalizeGraphics[
    g1 /. {Polygon[x_] :> GeometricTransformation[Polygon[x],
        RotationTransform[-30 Degree, {0, 0}]], Green -> LightBlue}];
$\endgroup$
2
  • 2
    $\begingroup$ Appears related to this question from 10 years ago : Why doesn't Normal work on GeometricTransformation? $\endgroup$ Oct 22, 2022 at 9:23
  • $\begingroup$ You can use RotationMatrix on each point of polygon instead of GeometricTransformation with RotationTransform. That way you get explicit coordinates after rotation. $\endgroup$ Oct 22, 2022 at 10:52

2 Answers 2

4
$\begingroup$

It seems easier to me, if you consider the Graphic primitives directly:

poly1 = Polygon[{{0, 0}, {0, 1}, {1, Sqrt[3]}, {2, 1}, {0, 0}}]

poly2 = TransformedRegion[poly1, RotationTransform[-30 Degree, {0, 0}]] 
poly3 = TransformedRegion[poly2, RotationTransform[-30 Degree, {0, 0}]] 

Graphics[{Green, poly1, Lighter[Blue], poly2, Orange, poly3}]

enter image description here

pts1=poly1[[1]]  (*{{0, 0}, {0, 1}, {1, Sqrt[3]}, {2, 1}, {0,0}}*)
pts2=poly2[[1]]  (*{{0, 0}, {1/2, Sqrt[3]/2}, {Sqrt[3],1}, {1/2 + Sqrt[3], -1 + Sqrt[3]/2}} *)
pts3=poly3[[1]] (*{{0, 0}, {Sqrt[3]/2, 1/2}, {2,0},
{1/2 (-1 + Sqrt[3]/2) + 1/2 Sqrt[3] (1/2 + Sqrt[3]),1/2 (-(1/2) - Sqrt[3]) + 1/2 Sqrt[3] (-1 + Sqrt[3]/2)}}*)
$\endgroup$
4
  • $\begingroup$ This still needs Normal in my application, but TransformedRegion does the job with all built-in functions. E.g. g2 = Normal[g1 /. {Polygon[x_] :> TransformedRegion[Polygon[x], ... $\endgroup$ Oct 22, 2022 at 12:22
  • $\begingroup$ What about Graphics[{Green, poly1, Orange, poly2}] ? $\endgroup$ Oct 22, 2022 at 12:27
  • $\begingroup$ Could do, indeed. Thanks $\endgroup$ Oct 22, 2022 at 12:50
  • $\begingroup$ You're welcome! $\endgroup$ Oct 23, 2022 at 10:01
3
$\begingroup$

As I wrote in the comment - you can use RotationMatrix instead.

g1 = Graphics[{Green, 
    Polygon[{{0, 0}, {0, 1}, {1, Sqrt[3]}, {2, 1}, {0, 0}}]}];
g2 = g1 /. {Polygon[x_] :> 
     Polygon[RotationMatrix[-30 Degree] . # & /@ x], Green -> Blue};
g3 = g2 /. {Polygon[x_] :> 
     Polygon[RotationMatrix[-30 Degree] . # & /@ x], Blue -> Orange};
g4 = Graphics[First /@ {g1, g2, g3}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks, this was useful. $\endgroup$ Oct 23, 2022 at 14:30

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.