6
$\begingroup$

Still a complete noob to Mathematica. How can I create random points on a x,y graph and then enclose all the points with the smallest possible circle?

$\endgroup$
6
$\begingroup$

You can create random points with RandomReal:

pts = RandomReal[10, {100, 2}];

To find the minimal enclosing circle/disk, you can use BoundingRegion:

Graphics[{FaceForm[LightBlue], BoundingRegion[pts, "MinDisk"], Point[pts]}]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Does that contain both x and y values that would determine coordinates? $\endgroup$ – Stefan Rheeders Mar 28 '18 at 20:52
5
$\begingroup$
SeedRandom[0];
pts = RandomReal[10, {100, 2}];

An alternate approach using NMinimize

reg = Disk[{x, y}, r];

Find the circular Disk with the smallest Area that contains all of the points

min = NMinimize[{Area[reg], (Element[#, reg] & /@ pts), r > 0}, {x, y, r}] // 
  Quiet

(* {132.395, {x -> 5.0594, y -> 5.21973, r -> 6.49173}} *)

area = min[[1]]

(* 132.395 *)

Comparing the area with Carl Woll's use of BoundingRegion

regCW = BoundingRegion[pts, "MinDisk"];

RegionCentroid[regCW]

(* {5.0594, 5.21973} *)

areaCW = Area[regCW]

(* 132.395 *)

(area - areaCW) // Chop[#, 10^-6] &

(* 0 *)

Row[{Graphics[{LightBlue, reg /. min[[2]], Black, Point[pts]}, 
   ImageSize -> 324],
  Graphics[{FaceForm[LightBlue], regCW, Point[pts]}, ImageSize -> 324]}]

enter image description here

$\endgroup$
0
$\begingroup$

You can use RandomPoint

pts=RandomPoint[Disk[],500];
Graphics[{PointSize[Tiny],Point[pts]}]

And RandomPoint support Region Objects.

pts=RandomPoint[Region, 500];
Graphics[{PointSize[Tiny],Point[pts]}]

So you can generate random points on a Region which could be a circle and then enclose all the points with the smallest possible Region which could be a circle?

Of course, you can bounding Region these points further like other answers done.

From 10.2 to later versions

$\endgroup$
  • $\begingroup$ I think OP asked to find a disc containing some given points, not to generate random points in a given disc (though the question is pretty badly phrased). $\endgroup$ – Szabolcs Jan 8 at 10:52
  • $\begingroup$ @Szabolcs He says: How can I create random points on...? $\endgroup$ – HyperGroups Jan 8 at 11:04
  • $\begingroup$ Followed by "... on a x,y graph" (whatever that may be), not by "on a disc". $\endgroup$ – Szabolcs Jan 8 at 11:07
  • 1
    $\begingroup$ @Szabolcs Yes, but RandomPoint support any Region, I just think this function is useful, so I add one Code. $\endgroup$ – HyperGroups Jan 8 at 11:09

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.