# How to separate the regions enclosed by curves

There are three arbitrary curves that defined by three implicit functions. How to separate the 12 regions(one of them is the outside infinite region)

Although we can use image methods to distinguish it, the effect seem not so good. Here we want to get the vector graphics. Thanks!

curves = ContourPlot[{x^2 + y^2 - 16 == 0,
x^2/36 + y^2 == 1, (x - 2)^2 + (y + 1)^2 - 9 == 0}, {x, -7,
7}, {y, -7, 7},
ContourStyle -> Directive[AbsoluteThickness, Black],PlotRangePadding -> None]
Colorize[MorphologicalComponents[curves]] Test another way.(I don't know how to extract data from BoundaryMeshRegion directly.)

Clear["Global*"];
curves = ContourPlot[{x^2 + y^2 - 16 == 0,
x^2/36 + y^2 == 1, (x - 2)^2 + (y + 1)^2 - 9 == 0}, {x, -7,
7}, {y, -7, 7},
ContourStyle -> Directive[AbsoluteThickness, Black],
Colorize[MorphologicalComponents[curves]];
domains = ImageMesh[curves, Method -> "DualMarchingSquares"];
pts = MeshCoordinates[domains];
lines = MeshCells[domains, "Multicells" -> True][];
regs = BoundaryMeshRegion[pts, #] & /@ lines;
outside = BoundaryMeshRegion[pts, lines[], lines[]];
insides =
BoundaryMeshRegion[pts, lines[[#]]] & /@ Range[3, Length[regs]];
Graphics[{Gray, outside,
Thread[{ColorData /@ Range[Length[regs] - 2], insides}]}] • This link should help, although this is not a typical Venn Diagram scenario. For instance, the light blue region on the left and the deep blue region on the right are in the same set but are supposed to be colored differently. Instead of A, B, C use ImplicitRegion for each of your equations.
– Syed
Dec 22, 2021 at 7:57
• curves = ContourPlot[{x^2 + y^2 - 16 == 0, x^2/36 + y^2 == 1, (x - 2)^2 + (y + 1)^2 - 9 == 0}, {x, -7, 7}, {y, -7, 7}, PlotRangePadding -> None, Frame -> False]; ArrayPlot[MorphologicalComponents[curves, .9], ColorRules -> {0 -> White, 1 -> Green, 2 -> Red, 3 -> Blue, 4 -> Yellow, 5 -> Cyan, 6 -> Orange, 7 -> Brown, 8 -> Pink, 9 -> Purple, 10 -> Magenta, 11 -> Gray, 12 -> LightYellow}] Dec 30, 2021 at 23:49
• curves = ContourPlot[{x^2 + y^2 - 16 == 0, x^2/36 + y^2 == 1, (x - 2)^2 + (y + 1)^2 - 9 == 0}, {x, -7, 7}, {y, -7, 7}, PlotRangePadding -> None, Frame -> False]; regs = ConnectedMeshComponents[ ImageMesh[curves, Method -> "DualMarchingSquares"]]; Graphics[Thread[{ColorData /@ Range[Length[regs]], regs}]] Dec 31, 2021 at 8:28

eqns = {x^2 + y^2 - 16 == 0, x^2/36 + y^2 == 1, (x - 2)^2 + (y + 1)^2 - 9 == 0};

regions = eqns /. Equal -> LessEqual;

rp = RegionPlot[Evaluate@regions, {x, -7, 7}, {y, -7, 7},
PlotPoints -> 100, PlotStyle -> Opacity,
BoundaryStyle -> Directive[AbsoluteThickness, White]] Use a slightly modified version of separatePolygons from this answer:

ClearAll[separatePolygons]
separatePolygons = Module[
{imgmesh =
ImageMesh @
ColorNegate @
Rasterize[Graphics[#[]], ImageResolution -> 200],
cb = CoordinateBounds @ #[[1, 1, 1]], cbm},
cbm = CoordinateBounds @ imgmesh;
MeshPrimitives[imgmesh, 2] /.
p_Polygon :> RescalingTransform[cbm, cb] /@ p] &;

Graphics[MapIndexed[{ColorData @ #2[], #} &,
separatePolygons[rp]], ImageSize -> Large] Note: In versions prior to version 12.0, replace cb = CoordinateBounds @ #[[1, 1, 1]] with cb = CoordinateBounds @ #[[1, 1]].

Using the method in OP's update:

curves = ContourPlot[{x^2 + y^2 - 16 == 0, x^2/36 + y^2 == 1,
(x - 2)^2 + (y + 1)^2 - 9 == 0}, {x, -7, 7}, {y, -7, 7},
ContourStyle -> Directive[AbsoluteThickness, Black],
Frame -> False];

imesh = ImageMesh[curves, Method -> "DualMarchingSquares"];

polygons13 = ReverseSortBy[RegionMeasure] @
(MeshPrimitives[imesh, 1, Multicells -> True] /. Line -> Polygon @* Apply[Join]);

polygons = Join[{Polygon[# -> #2]& @@ (List @@@ #[[;;2]])}, #[[3;;]]]& @ polygons13;

Graphics[{RandomColor[], #}] & /@ polygons// Multicolumn[#, 3]& Graphics[{RandomColor[], #} & /@ polygons, ImageSize -> Large] Note: The first two polygons in polygon13 are the enclosing rectangle and the union of inside polygons:

Graphics[{RandomColor[], #}] & /@ polygons13[[;;2]] Hence to the need to use the Polygon[# -> #2]& @@ (...)` trick to get a polygon with a hole.