3
$\begingroup$

Say I have the following code to generate a graphics object

data = {
   {{0.46, 0.43}, 0.73},
   {{0.46, 0.90}, 0.63},
   {{0.97, 0.93}, 0.83},
   {{0.97, 0.43}, 0.65},
   {{0.46, -0.06}, 0.83},
   {{0.97, -0.06}, 0.70},
   {{-0.07, 0.90}, 0.50},
   {{-0.03, 0.43}, 0.90},
   {{-0.05, -0.06}, 0.55}
   };
n = Length[data];
p = data[[All, 1]];
\[Theta] = data[[All, 2]];
Graphics[
 {
  Table[Rotate[{
     LightGray,
     EdgeForm[Black],
     Ellipsoid[p[[k]], {0.3, 0.2}]
     }, \[Theta][[k]], p[[k]]], {k, n}],
  Line[{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}]
  }
 , PlotRange -> {{-0.4, 1.3}, {-0.4, 1.2}}
 ]

The representation of the generated object is as follows:

enter image description here

How can I clip out the gray part of the ellipsoids external to the square box?

$\endgroup$
0

2 Answers 2

3
$\begingroup$
ellipsoids = Table[TransformedRegion[Ellipsoid[p[[k]], {0.3, 0.2}], 
    RotationTransform[θ[[k]], p[[k]]]], {k, n}];

insiderectangle = DeleteCases[_EmptyRegion] @
  Map[BoundaryDiscretizeRegion @ RegionIntersection[#, Rectangle[]] &]@
  ellipsoids;

Graphics[{{EdgeForm[Black], FaceForm[], elipsoids, 
   FaceForm[LightGray], insiderectangle },
 Line[{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}]}, 
 PlotRange -> {{-0.4, 1.3}, {-0.4, 1.2}}]

enter image description here

To get the portions of elliposids that lie outside the unit rectangle use RegionDifference in place of RegionIntersection:

outsiderectangle = DeleteCases[_EmptyRegion] @
   Map[BoundaryDiscretizeRegion @ RegionDifference[#, Rectangle[]] &]@
    ellipsoids;

Graphics[{{EdgeForm[Black], 
   FaceForm[LightBlue], outsiderectangle, 
   FaceForm[LightRed], insiderectangle}, 
  Line[{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}]}, 
 PlotRange -> {{-0.4, 1.3}, {-0.4, 1.2}}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you, but please read my comment to the other answer $\endgroup$
    – enzotib
    Nov 1, 2023 at 16:04
  • $\begingroup$ please see the updated version. $\endgroup$
    – kglr
    Nov 1, 2023 at 16:11
2
$\begingroup$

Try this

Graphics[{Table[
   Rotate[{LightGray, EdgeForm[Black], 
     Ellipsoid[p[[k]], {0.3, 0.2}]}, \[Theta][[k]], p[[k]]], {k, n}], 
  Line[{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}]}, 
 PlotRange -> {{0, 1}, {0, 1}},
 Frame -> False, PlotRangePadding -> None]

enter image description here

If you switch to Frame-> True then you can see the PlotRange you need. For some reason you had too wide a plot range.

$\endgroup$
1
  • $\begingroup$ Not exactly what I am looking for, I would like to have also the external part of the image, with ellipsoid's border but without gray $\endgroup$
    – enzotib
    Nov 1, 2023 at 15:56

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.