# Clipping graphics of ellipsoids

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:

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

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}}]


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}}]


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

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]


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

• 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 Nov 1, 2023 at 15:56