8
$\begingroup$

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

two regions


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]

r3


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

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

r4

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

Any ideas?

$\endgroup$
6
$\begingroup$

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

ConnectedMeshComponents@DiscretizeRegion@r4

enter image description here

and also obtain algebraic descriptions of each.

Sorry.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ First time I see ConnectedMeshComponents put to use. Nice. $\endgroup$ – DavidC Dec 22 '14 at 12:27
  • $\begingroup$ @David: I just typed Connected into a notebook hoping something related to connected components would show up, and there it was. $\endgroup$ – user484 Dec 22 '14 at 18:16
3
$\begingroup$

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.

region bounds

{{{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.

|improve this answer|||||
$\endgroup$
  • 2
    $\begingroup$ maybe BooleanConvert[Reduce[eq2 && ! eq1, {x, y}]] /. Or -> List? (+1) $\endgroup$ – kglr Dec 21 '14 at 19:33
  • $\begingroup$ kguler, Yes, exactly! Very good. $\endgroup$ – DavidC Dec 21 '14 at 19:58
2
$\begingroup$

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)
|improve this answer|||||
$\endgroup$
  • $\begingroup$ That answers the first question. $\endgroup$ – DavidC Dec 21 '14 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.