# Find Unconnected Subregions

If I have a region, how can I identify sub regions that may be disconnected, if any exist?

region defines a rectangular region given two diagonally opposed vertices.

region[{{x1_,y1_},{x2_,y2_}}]:=Module[{sort1=Sort[{x1,x2}],sort2=Sort[{y1,y2}]},
ImplicitRegion[(sort1[[1]]<= x <= sort1[[2]])\[And](sort2[[1]]<= y <= sort2[[2]]),{x,y}]]


Here we define two rectangular regions, r1 and r2 that intersect.

r1 = region[{{4, -3}, {-7, 6}}];
r2 = region[{{4, -7}, {0, 7}}];
RegionPlot[{r1, r2}, PlotRange -> {-10, 10}, BaseStyle -> 16, PlotLegends -> {r1, r2}]


In the present case, one of region differences, r3 consists of a single region.

r3 = RegionDifference[r1, r2];
RegionPlot[{r3}, PlotRange -> {-10, 10}, PlotLegends -> {r3}, BaseStyle -> 16]


The other, r4, turns out to have two subregions.

r4 = RegionDifference[r2, r1];
RegionPlot[{r4}, PlotRange -> {-10, 10}, PlotLegends -> {r4}, BaseStyle -> 16]


I would like to programmatically determine how many unconnected subregions a region has and also obtain algebraic descriptions of each.

Any ideas?

I would like to programmatically determine how many unconnected subregions a region has

ConnectedMeshComponents@DiscretizeRegion@r4


and also obtain algebraic descriptions of each.

Sorry.

• First time I see ConnectedMeshComponents put to use. Nice. Commented Dec 22, 2014 at 12:27
• @David: I just typed Connected into a notebook hoping something related to connected components would show up, and there it was.
– user484
Commented Dec 22, 2014 at 18:16

## An Algebraic Route

Reduce enables us to answer the second question (How do I obtain algebraic descriptions of each unconnected subregion?) without even creating Regions.

equation[{{x1_,y1_},{x2_,y2_}}]:=Module[{sort1=Sort[{x1,x2}],sort2=Sort[{y1,y2}]},(sort1[[1]]<= x <= sort1[[2]])\[And](sort2[[1]]<= y <= sort2[[2]])]


This is very much like the region function but it returns an equation instead of a region.

eq1 = equation[{{4, -3}, {-7, 6}}]
eq2 = equation[{{4, -7}, {0, 7}}]
Reduce[eq2 && ! eq1, {x, y}]


-7 <= x <= 4 && -3 <= y <= 6

0 <= x <= 4 && -7 <= y <= 7

0 <= x <= 4 && (-7 <= y < -3 || 6 < y <= 7)

I would still need to break up the bottom equation into two separate equations.

Update:

Then, using kguler's suggestion (in comments, below)

BooleanConvert[Reduce[eq2 && ! eq1, {x, y}]] /. Or -> List


one obtains

{-7 <= y < -3 && 0 <= x <= 4, 0 <= x <= 4 && 6 < y <= 7}

## Using MeshRegion

Rahul notes (in another answer) that ConnectedMeshComponents can be used to return the subregions as separate regions. Because the regions are polygonal (specifically, rectangular) we may then obtain the bounds of each subregion using RegionBounds.

{{{0., 4.}, {-7., -3.}}, {{0., 4.}, {6., 7.}}}

Using

equation/@%


we get

{-7. <= x <= 0. && -3. <= y <= 4., 0. <= x <= 6. && 4. <= y <= 7.}

RegionBounds turns out to be very useful. Nonetheless, note that the bounds returned are non-integers.

The algebraic route, described above, returns integer bounds.

• maybe BooleanConvert[Reduce[eq2 && ! eq1, {x, y}]] /. Or -> List? (+1)
– kglr
Commented Dec 21, 2014 at 19:33
• kguler, Yes, exactly! Very good. Commented Dec 21, 2014 at 19:58

Use Image processing functions:

MorphologicalComponents[Binarize[Image[RegionPlot[{r3}, Frame -> None]]]] // Max


1

MorphologicalComponents[Binarize[Image[RegionPlot[{r4}, Frame -> None]]]] // Max


2

As for the second question: if you need the region bounds, use the previous solution and then:

RegionBounds /@ (ConnectedMeshComponents@DiscretizeRegion@r4)

• That answers the first question. Commented Dec 21, 2014 at 18:25