# how to generate random convex quadrilaterals using circles (actually inscribed within circles)?

I would like to generate random convex quadrilaterals using circles, my tutor has suggested to using randomreal to generate 4 numbers and scale the sum of them to 2pi, and then use trigonometry properties to do that, I do not see how that works, can somebody gives some hints about how I should go with it then I can try it out.

• "Use trigonometry" means place them on the unit circle. Then connect the dots. Commented Dec 16, 2018 at 15:18

## 3 Answers

Maybe this way?

n = 1000;
x = RandomReal[{0, 2 Pi}, {n, 5}];
x[[All, 2 ;;]] *= Divide[(2. Pi), (x.{0., 1., 1., 1., 1.})];
x = x.UpperTriangularize[ConstantArray[1., {5, 4}]];
quads = Transpose[{Cos[x], Sin[x]}, {3, 1, 2}];


Convexity test:

And @@ (GraphicsPolygonUtilsPolygonConvexQ /@ quads)


True

• maybe we only use random four points on the circle and to construct a quadrilateral? Commented Dec 16, 2018 at 15:48
• Certainly, this is a trivial problem in the sense that it can be tackled in many ways. I just tried to make it fast by using as much vectorized operations and matrix arithmetic as possible. On my machine, the code above generates about 3 million quadrilaterals per second. Commented Dec 16, 2018 at 15:59
• Thanks, this is very helpful! Commented Dec 16, 2018 at 16:00
• You're welcome! Commented Dec 16, 2018 at 16:00
ClearAll[randomQuad]
randomQuad = SortBy[#, ArcTan @@ # &] & /@ RandomPoint[Circle[], {#, 4}] &;

SeedRandom[777]
Graphics[{Circle[],
{Opacity[.5], EdgeForm[Gray], RandomColor[], Polygon@#} & /@ randomQuad[5]}]


Here's a fun way to use Henrik's stuff (and some other nicely vectorized operations):

n = 550;
x = RandomReal[{0, 2 Pi}, {n, 5}];
x[[All, 2 ;;]] *= Divide[(2. Pi), (x.{0., 1., 1., 1., 1.})];
x = x.UpperTriangularize[ConstantArray[1., {5, 4}]];
quads = Transpose[{Cos[x], Sin[x]}, {3, 1, 2}];
disks = {Range[2, 3], Range[7, 8] , Range[12, 15]};
shiftQuads = quads +
RandomChoice[Flatten@disks, n]*
Transpose[
ConstantArray[{Cos[Subdivide[0., 2. \[Pi], n - 1]],
Sin[Subdivide[0., 2. \[Pi], n - 1]]}, 4], {2, 3, 1}];

{
Opacity[.15],
Annulus[{0, 0}, {-1, 1} + MinMax@#] & /@ disks,
Thread@{Opacity[.5], RandomColor[n], Thread[Polygon@shiftQuads]}
} // Graphics


• Great! I will have a try, so many thanks! Commented Dec 18, 2018 at 2:30