500
500

Reputation: 6619

ConvexHull in Graphics - Mathematica

Trying to plot a ConvexHull Using PlanarGraphPlot from the ComputationalGeometry package, it does not work when used in graphics.

Any Idea on how to plot the ConvexHull using Graphics ?

Upvotes: 5

Views: 1266

Answers (2)

Daniel Lichtblau
Daniel Lichtblau

Reputation: 6884

Not sure exactly what is wanted. Maybe the code below will get you started.

 pts = RandomReal[{-10, 10}, {20, 2}]
(*
Out[1]= {{1.7178, -1.11179}, {-7.10708, -8.1637},
 {8.74461, -2.42551}, {6.64129, -2.87008}, {9.9008, 6.47825},
 {8.27081, 9.94116}, {9.97325, 7.61094}, {-2.7876, 9.70449},
 {-3.69357, 0.0253506}, {-0.503817, -1.98649}, {6.3056, -1.16892},
 {-4.69983, -1.93242}, {-6.09983, 7.49229}, {8.08545, 6.67951},
 {-6.91195, 8.34752}, {-2.63136, 6.0506}, {-0.130006, 2.10929},
 {1.64401, 3.32165}, {0.611335, -8.11364}, {-2.03548, -9.37277}}
*)
With[{hull = pts[[Graphics`Mesh`ConvexHull[pts]]]}, 
  Graphics[Line[Append[hull, First[hull]]]]]

enter image description here

Upvotes: 5

Sjoerd C. de Vries
Sjoerd C. de Vries

Reputation: 16232

Needs["ComputationalGeometry`"]
pts = RandomReal[{0, 10}, {60, 2}];

Graphics[
 {
  Point@pts,
  FaceForm[], EdgeForm[Red],
  Polygon@pts[[ConvexHull[pts]]]
  }
 ]

or

cpts = pts[[ConvexHull[pts]]];
AppendTo[cpts, cpts[[1]]];

Graphics[
 {
  Point@pts,
  Red,
  Line@cpts
  }
 ]

enter image description here

Upvotes: 9

Related Questions