4
$\begingroup$

It is possible to generate a random Rectilinear Polygon (without holes) in WL? I found this option, but without any possibility to ask for a Rectilinear Polygon.

RandomPolygon["Convex", 2]
$\endgroup$
7
  • 2
    $\begingroup$ Conjection: Each convex rectilinear polygon is an axis-aligned rectangle. Or maybe you have special notion of convexity for rectilinear polygons? If so, please make it precise. $\endgroup$ Commented Jan 12, 2020 at 12:40
  • $\begingroup$ yes, you are right. When I say convex, I mean without holes. $\endgroup$ Commented Jan 12, 2020 at 12:47
  • $\begingroup$ Ah, you mean connected and simply connected. $\endgroup$ Commented Jan 12, 2020 at 12:55
  • $\begingroup$ I'm not sure ... $\endgroup$ Commented Jan 12, 2020 at 13:04
  • $\begingroup$ In other words you ask for a set of numbers $x_i$ and $y_i$ such that $\sum_{i=1}^n x_i=0$ and $\sum_{i=1}^n y_i=0$, right? $\endgroup$
    – yarchik
    Commented Jan 12, 2020 at 13:59

2 Answers 2

2
$\begingroup$

The number of vertices should be even and is given by 2 n below

RRPoly[n_] := Module[{x, y, indx, indy},
  x = Accumulate[RandomReal[{-1, 1}, n]];
  y = Accumulate[RandomReal[{-1, 1}, n]];
  indx = Table[Quotient[i + 1, 2], {i, 2 n}];
  indy = RotateLeft[indx];
  Table[{x[[indx[[i]]]], y[[indy[[i]]]]}, {i, 2 n}]
  ]

Generate 3 random polygons with random colors.

Graphics[{Opacity[0.9], 
  Table[{RGBColor[RandomReal[{0, 1}, 3]], Polygon[RRPoly[5]]}, {k, 
    3}]}]

enter image description here

$\endgroup$
1
$\begingroup$
ClearAll[rectilinearPoly]
rectilinearPoly = Polygon@(Join @@ (Partition[#, 2, 1, 1] /.
    {{a_Real, b_}, {c_, d_}} :> {{a, b}, {c, b}, {c, d}})) &;

Examples:

SeedRandom[1]
coords =  RandomReal[{-1, 1}, {5, 6, 2}];

Row[Graphics[{EdgeForm[Gray], FaceForm[Opacity[.5, RandomColor[]]],  
    rectilinearPoly@#, Red, PointSize[Medium], Point@#}, ImageSize -> 1 -> 100]& /@
     (#[[FindShortestTour[#][[2]]]] & /@ coords), Spacer[5]]

enter image description here

Row[Graphics[{EdgeForm[Gray], FaceForm[Opacity[.5, RandomColor[]]], 
     rectilinearPoly@#, 
     Red, PointSize[Medium], Point@#}, ImageSize -> 1 -> 100] & /@ 
   coords, Spacer[5]]

enter image description here

$\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.