I am trying to create a rectangular domain containing many curves that start and stop on either the left or right edges of the domain. These curves should not intersect. Additionally, I need to know the areas of the regions created by these curves as shown in this picture.

Desired Possible Layout

I found a couple of other questions that helped me, but I am still working on the problem.

Random Curves in 3D

Diving a Domain into Regions by Straight Lines

Here are the tasks I am still trying to complete:

  1. Define regions with curves rather than straight lines. The second link divides the domain into regions defined by straight lines and I have taken that example and gotten the area of each of those regions, but I can't seem to make the same work for curves.
  2. Create random curves that never intersect and start and stop on the left and right edges of the boundary. I have been trying to tackle this issue from a differential geometry perspective. I have tried to make some mapping that preserves the curve and where it starts and ends. This mapping would go from a square domain on which an initial random curve is defined to a domain that is entirely within other previously placed curves as seen in the picture below. One could then just randomly choose any existing region, map a curve into it, subset that region into two new regions, and repeat.

Mapping of Rectangular Domain to Domain of Arbitrary Shape

If anyone can offer any help with either of these tasks, I would appreciate it. Also please let me know if the question is poorly posed. I have some Mathematica experience, but not a lot.


1 Answer 1


Update: If we extract Polygons (instead of FilledCurves) we can use many Mesh* and Region* functions on extracted polygons:

cp = ContourPlot[Evaluate[Sum[Sin[RandomReal[5, 2] . {x, 5 y}], {10}]], 
  {x, 0, 5}, {y, 0, 5}, Contours -> 20, ContourShading -> None];

polygons = Cases[Normal@cp, 
   Line[x_] /; x[[{1, -1}, 1]] == {0., 0.} || x[[{1, -1}, 1]] == {5., 5.} :> 
    Polygon @ x, All];

Graphics[{RandomColor[], #} & /@ polygons]

enter image description here

largest20 = TakeLargestBy[polygons, Area, 20];

Graphics[{RandomColor[], #} & /@ largest20]

enter image description here

Multicolumn[DiscretizeRegion /@ largest20, 5]

enter image description here

Original answer:

Contour lines of a surface do not intersect.

So, we can take contour lines of a random surface and select the lines that start and end on the left or the right end of the horizontal range:

cp = ContourPlot[Evaluate[Sum[Sin[RandomReal[5, 2] . {x, 5 y}], {10}]], 
  {x, 0, 5}, {y, 0, 5}, Contours -> 20, ContourShading -> None];

Graphics @ Cases[Normal @ cp, 
  l : Line[x_] /; x[[{1, -1}, 1]] == {0., 0.} || x[[{1, -1}, 1]] == {5., 5.} :> 
  {RandomColor[], FilledCurve@l}, All] 

enter image description here

We can also use ListContourPlot:

lcp = ListContourPlot[RandomReal[{-.25, .25}, {20, 15}], 
   InterpolationOrder -> 3, Contours -> 20, ContourShading -> None];

Graphics @ Cases[Normal@lcp, 
  l : Line[x_] /; 
    x[[{1, -1}, 1]] == {1., 1.} || x[[{1, -1}, 1]] == {15., 15.} :>
    {l, RandomColor[], FilledCurve @ l}, All] 

enter image description here

  • $\begingroup$ Wow, that is a great solution. I had thought of contour plots but kept trying to figure out how to ensure the function had no minima or maxima within the domain. I am a mechanical engineering doctoral student working on the optimization of sensor placement in soft robotics. If you are interested in collaborating on our publication, we would love to have you. Regardless, thanks so much for the help. $\endgroup$ May 24, 2023 at 15:20
  • $\begingroup$ One question, is it possible to turn this graphical contour plot into regions that Mathematica can calculate the area of? $\endgroup$ May 24, 2023 at 15:43
  • $\begingroup$ John, sorry for the delayed response. I really appreciate your generous offer for collaboration. I am not able to make new commitments, but I will be more than happy to provide any help through this site. Re turning contour lines to regions, please see the update. I can imagine discretized regions could be useful for your applications. $\endgroup$
    – kglr
    May 30, 2023 at 18:40

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.