7

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?

Carl Woll
  • 130,679
  • 6
  • 243
  • 355

3 Answers3

7

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

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
5
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

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
0

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

HyperGroups
  • 8,619
  • 1
  • 26
  • 63