5
$\begingroup$

I want to count number of connected region. For ex I have a region below which has 2 connected region. And also, How to check whether the function RegionDifference[] return empty region

enter image description here

But how can I count computationally with Mathematica ? Is there a function performs automatically ?

V = {{1454.16552734375`, 942.5`}, {337.9480285644531`, 
    407.9453125`}, {318.0152587890625`, 
    428.353515625`}, {342.0124816894531`, 
    403.962646484375`}, {277.94842529296875`, 
    486.03125`}, {1198.7965087890625`, 
    1234.8741455078125`}, {312.4529724121094`, 
    434.356689453125`}, {1142.8619384765625`, 
    1274.6539916992188`}, {1216.492431640625`, 
    1218.9954833984375`}, {1179.011962890625`, 
    1250.852294921875`}, {688.2337646484375`, 
    1314.6601257324219`}, {733.9116821289062`, 
    1330.8984069824219`}, {627.462158203125`, 
    1283.3348999023438`}, {583.695556640625`, 
    1239.4417114257812`}, {1511.9337158203125`, 
    652.8341674804688`}, {1511.9337158203125`, 
    642.1658325195312`}, {651.5089721679688`, 
    1299.1900024414062`}, {1272.906494140625`, 
    1154.5545959472656`}, {1085.2890625`, 
    1306.9449462890625`}, {599.6814575195312`, 
    1258.9429016113281`}, {272.437255859375`, 
    539.4434814453125`}, {287.7309265136719`, 
    461.513427734375`}, {608.1034545898438`, 
    1267.4883422851562`}, {268.2161865234375`, 
    663.7935180664062`}, {1156.6552734375`, 
    397.2230224609375`}, {763.6713256835938`, 
    1335.1801147460938`}, {310.9466857910156`, 
    860.0018310546875`}, {263.71783447265625`, 
    637.33642578125`}, {886.817626953125`, 
    305.4976806640625`}, {366.3626403808594`, 382.657470703125`}};
nv = V[[Last[FindShortestTour[V]]]];
mr1 = Polygon@nv;
hole = mr1;
R2 = Disk[#, 510] & /@ V;
Graphics[{Blue, {PointSize[0.02], Point[nv]}, {Opacity[0.2], 
   EdgeForm[{Thick, Red}], FaceForm[Red], R2}}]
hole2 = mr1;
For[j = 1, j < Length[nv], j++,
  hole2 = RegionDifference[hole2, R2[[j]]];

  ];

m = DiscretizeRegion[hole2]
Area[m]
$\endgroup$
6
  • $\begingroup$ Can you post some code to start with? $\endgroup$
    – anderstood
    Commented May 4, 2018 at 0:56
  • $\begingroup$ Updated with the code $\endgroup$ Commented May 4, 2018 at 2:06
  • $\begingroup$ Also, How to check whether the function RegionDifference[] return empty region $\endgroup$ Commented May 4, 2018 at 2:12
  • $\begingroup$ reg = Region@RegionUnion[Polygon[Sort@RandomReal[{-1,1},{4,2}]],Polygon[RandomReal[{1,2},{3,2}]]]; bdr=BoundaryDiscretizeRegion[reg, {{-3,3}, {-3,3}}]; Cases[bdr["Show"], Polygon[x_]:>Length[x], Infinity]? $\endgroup$
    – kglr
    Commented May 4, 2018 at 2:22
  • $\begingroup$ Seem doesnt work for region in my case. It returns 1 $\endgroup$ Commented May 4, 2018 at 4:50

2 Answers 2

2
$\begingroup$

ConnectedMeshComponents returns a list of the connected regions, and you can count them using Length.

Here is a simple example with 4 components,

DiscretizeRegion@
 RegionDifference[Rectangle[{-1, -1}, {1, 1}], Disk[{0, 0}, 1.1]]

enter image description here

ConnectedMeshComponents@%

enter image description here

Length@%
(* 4 *)

The second question,

How to check whether the function RegionDifference[] return empty region

For simple examples, RegionDifference returns EmptyRegion directly

RegionDifference[Disk[{0, 0}, 0.9], 
 Rectangle[{-1, -1}, {1, 1}]]
(* EmptyRegion[2] *)

If RegionDifference returns a BooleanRegion instead, you could check using RegionEqual.

$\endgroup$
1
$\begingroup$

Here is one way:

reg = RegionUnion[Disk[{0, 0}, 2], Disk[{5, 0}, 2], Disk[{2.5, -4}, 2]];
Region[reg]

Mathematica graphics

We now discretize this region using the FEM package. Then we use the element connectivity data that meshes created with the FEM package have and use that to create a connectivity graph. The number of connected regions can then be computed as the number of connected components in the graph.

Needs["NDSolve`FEM`"]
mesh = ToElementMesh[reg];
connectivity = First[mesh["ElementConnectivity"]];
rules = DeleteCases[{0, _} | {_, 0}]@Flatten[MapIndexed[{First[#2], #} &, connectivity, {2}], 1];
sa = SparseArray[rules -> 1];
Length@ConnectedComponents@AdjacencyGraph[sa]

3

$\endgroup$

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.