5
$\begingroup$

While I was trying to solve this problem, I noticed a strange result of RegionDistance. The following is a contour plot of the distance from the tilted square. Is it a bug? Should I avoid using Translate to specify a region? It seems Rotate is OK in this case.

I'm using "14.1.0 for Mac OS X ARM (64-bit) (July 16, 2024)".

Clear["Global`*"];
L = 10;
square = Rotate[Translate[Rectangle[-{L,L}/2, {L,L}/2], {3, -3}], 0.3];

dist = RegionDistance[square];

Show[ContourPlot[dist[{x, y}], {x, -L, 2 L}, {y, -2 L, L}, 
  Exclusions -> None], Graphics[{LightYellow, square}]]

square

$\endgroup$
2
  • 1
    $\begingroup$ I am using 14.1 Mac OS Sonoma and see the same thing. Interestingly if you increase the rotation the difference gets even bigger up to a maximum at Pi $\endgroup$
    – Dunlop
    Commented Nov 23 at 5:21
  • $\begingroup$ Not a bug since rotate not always get a proper region ( for later version maybe fully support all the graphics object be an region) $\endgroup$
    – cvgmt
    Commented Nov 23 at 5:39

3 Answers 3

3
$\begingroup$

Edit

Finally we found that add the center {0, 0} in the Rotate do the job.

Clear["Global`*"];
L = 10;
square = 
  Rotate[Translate[Rectangle[-{L, L}/2, {L, L}/2], {3, -3}], 
   0.3, {0, 0} (*add here *)];

dist = RegionDistance[square];

Show[ContourPlot[dist[{x, y}], {x, -L, 2 L}, {y, -2 L, L}, 
  Exclusions -> None], Graphics[{LightYellow, square}]]

enter image description here

Edit

  • Compare with the five results, I confirm that upto 14.1 version, it is recommend that not use the Rotate as the last transformation.
Clear["Global`*"];
L = 10;
rectangle = Rectangle[-{L, L}/2, {L, L}/2];
square = Rotate[Translate[rectangle, {3, -3}], 0.3];
square2 = 
  GeometricTransformation[rectangle, 
   RotationTransform[.3]@*TranslationTransform[{3, -3}]];
square3 = 
  GeometricTransformation[rectangle, 
   TranslationTransform[RotationMatrix[.3] . {3, -3}]@*
    RotationTransform[.3]];
square4 = 
  TransformedRegion[rectangle, 
   RotationTransform[.3]@*TranslationTransform[{3, -3}]];
square5 = 
  TransformedRegion[rectangle, 
   RotationTransform[.3] . TranslationTransform[{3, -3}]];
dist = RegionDistance[square];
dist2 = RegionDistance[square2];
dist3 = RegionDistance[square3];
dist4 = RegionDistance[square4];
dist5 = RegionDistance[square5];
{ContourPlot[dist[{x, y}], {x, -L, 2 L}, {y, -2 L, L}, 
  Exclusions -> None, Epilog -> {LightYellow, square}],
 ContourPlot[dist2[{x, y}], {x, -L, 2 L}, {y, -2 L, L}, 
  Exclusions -> None, Epilog -> {LightYellow, square2}],
 ContourPlot[dist3[{x, y}], {x, -L, 2 L}, {y, -2 L, L}, 
  Exclusions -> None, Epilog -> {LightYellow, square3}], 
 ContourPlot[dist4[{x, y}], {x, -L, 2 L}, {y, -2 L, L}, 
  Exclusions -> None, Epilog -> {LightYellow, square4}], 
 ContourPlot[dist5[{x, y}], {x, -L, 2 L}, {y, -2 L, L}, 
  Exclusions -> None, Epilog -> {LightYellow, square5}]}

enter image description here

Original

  • Rotate at first.
Clear[square];
square = 
  Translate[Rotate[Rectangle[-{L, L}/2, {L, L}/2], .3], {3, -3}];
RegionQ[square]

enter image description here

$\endgroup$
5
  • $\begingroup$ Thank you. I will Rotate first for such cases. @Syed's answer suggests that TransformedRegion works as I expected (i.e. translate first, and then rotate). I wonder if Rotate is designed for Graphic Primitives but not for Regions --- as you commented.... $\endgroup$
    – A. Kato
    Commented Nov 23 at 6:28
  • $\begingroup$ Very nice! Could you explain why just adding the center in the Rotate solves this problem? $\endgroup$
    – A. Kato
    Commented Nov 23 at 13:05
  • 1
    $\begingroup$ @A.Kato I still do not know why, but I found that Clear[a, b, t, square, squareX, dist, distX]; {a, b} = {4, 5}; t = .5; square = Rotate[Translate[rectangle, {a, b}], t, {a, b}]; squareX = Translate[Rotate[rectangle, t], {a, b}]; dist = RegionDistance[square]; distX = RegionDistance[squareX]; {ContourPlot[dist[{x, y}], {x, -L, 2 L}, {y, -2 L, L}, Exclusions -> None, Epilog -> {LightYellow, square}], ContourPlot[distX[{x, y}], {x, -L, 2 L}, {y, -2 L, L}, Exclusions -> None, Epilog -> {LightYellow, squareX}]} are the same. $\endgroup$
    – cvgmt
    Commented Nov 23 at 13:48
  • $\begingroup$ Thank you for testing various codes. I ran your code (in your comment) with rectangle = Rectangle[{0, 0}, {10, 1}]. Then the result was different. It seems that RegionDistance ignores/forgets the fact that Rotate[g,t] will rotate graphics around the center of g, not the origin {0,0} of the coordinate system. $\endgroup$
    – A. Kato
    Commented Nov 24 at 7:31
  • 1
    $\begingroup$ @A.Kato Thanks, you are right, we still need to add {0,0} in Rotate. L = 10; {a, b} = {4, 5}; t = .5; rectangle = Rectangle[{0, 0}, {10, 1}]; square = Rotate[Translate[rectangle, {a, b}], t, {a, b}]; squareX = Translate[Rotate[rectangle, t, {0, 0}], {a, b}]; dist = RegionDistance[square]; distX = RegionDistance[squareX]; {ContourPlot[ dist[{x, y}], {x, -L, 2 L}, {y, -2 L, L}, Exclusions -> None, Epilog -> {LightYellow, square}], ContourPlot[distX[{x, y}], {x, -L, 2 L}, {y, -2 L, L}, Exclusions -> None, Epilog -> {LightYellow, squareX}]} $\endgroup$
    – cvgmt
    Commented Nov 24 at 8:07
3
$\begingroup$

I am using v12.2.0.

Clear["Global`*"];
L = 10;
square = TransformedRegion[Rectangle[-{L, L}/2, {L, L}/2], 
   RotationTransform[0.3] . TranslationTransform[{3, -3}]];
dist = RegionDistance[square];
Show[ContourPlot[dist[{x, y}], {x, -L, 2 L}, {y, -2 L, L}, 
  Exclusions -> None], Graphics[{LightYellow, square}]]

RegionDistance to transformed region

$\endgroup$
4
  • $\begingroup$ Thank you very much. I learned that TransformdRegion is the appropriate tool here. Just to make sure, TransformdRegion[reg, A.B] applies first B, and then A. Am I right? $\endgroup$
    – A. Kato
    Commented Nov 23 at 6:18
  • $\begingroup$ I can't say for sure. I deleted my previous comment as translation is not linear. $\endgroup$
    – Syed
    Commented Nov 23 at 7:21
  • 1
    $\begingroup$ @A.Kato RotationTransform[0.3] . TranslationTransform[{3, -3}] == AffineTransform[{RotationMatrix[.3], RotationMatrix[.3] . {3, -3}}] , it means that RotationTransform[0.3] . TranslationTransform[{3, -3}] is also a AffineTransform $\endgroup$
    – cvgmt
    Commented Nov 23 at 12:11
  • $\begingroup$ @cvgmt, Thanks for the explanation and effort. $\endgroup$
    – Syed
    Commented Nov 25 at 6:57
2
$\begingroup$

An attention should be payed on what the output of Rotate is. Rotate acts immediately on the expression no matter what the expression is.

So I recommend to use GeometricTransformation with RotationMatrix or RotationTransform for a serious manipulation of geometric objects.

Rotate[Translate[Rectangle[-{L, L}/2, {L, L}/2], {3, -3}], 0.3]
Translate[Rotate[Rectangle[-{L, L}/2, {L, L}/2], 0.3], {3, -3}]

enter image description here

$\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.