Getting ordered coordinates out of ConvexHullRegion
For graphing, my preferred approach is posted already by user21.
To get the ordered coordinates you can reorder MeshCoordinates[ConvexHullMesh[d]
using FindCurvePath
:
With[{d = RandomReal[{0, 1}, {20, 2}]}, mc=MeshCoordinates[ConvexHullMesh[d]];
ListPlot[ d, AspectRatio -> 1, Prolog -> {Yellow,Polygon[mc[[FindCurvePath[mc][[1]]]]]}]]
An alternative way to get the ordering of the coordinates is to use the "BoundaryVertices"
property of ConvexHullMesh[d]
:
ConvexHullMesh[d]["BoundaryVertices"]
{1,8,4,5,6,3,10,9,2,7,11,1}
which is a rotated version of
FindCurvePath[MeshCoordinates[ConvexHullMesh[d]]][[1]]
{4,5,6,3,10,9,2,7,11,1,8,4}
And, the property "Coordinates"
can be used instead of MeshCoordinates
; so
#["Coordinates"][[#["BoundaryVertices"][[1]]]]&@ConvexHullMesh[d]
gives the ordered coordinates.
d = RandomReal[{0, 1}, {20, 2}];
chcoords=#["Coordinates"][[#["BoundaryVertices"][[1]]]]&@ConvexHullMesh[d];
ListPlot[ d, AspectRatio -> 1, Prolog -> {Yellow,Polygon[chcoords]}]
Yet another way to get the ordered coordinates is to get the "GraphicsComplex"
property and extract
Cases[Normal@ConvexHullMesh[d]["GraphicsComplex"],Polygon[x_]:>x,Infinity][[1]]
Assuming the goal is to create the graphics:
With[{d = RandomReal[{0, 1}, {20, 2}]},
Show[ListPlot[d, AspectRatio -> 1], ConvexHullMesh[d]]]
And here is a more efficient version than FindPath
:
With[{d = RandomReal[{0, 1}, {20, 2}]},
ListPlot[d, AspectRatio -> 1,
Epilog ->
GraphicsComplex[
MeshCoordinates[#], {Opacity[0.2], MeshCells[#, {2, All}]}] &[
ConvexHullMesh[d]]]]
d = RandomReal[{0, 1}, {20, 2}];
hull = ConvexHullMesh[d];
The ordering can be obtained directly using MeshCells
:
MeshCells[hull, 2]
(* {Polygon[{2, 6, 5, 4, 3, 1}]} *)
So:
points = MeshCoordinates[hull];
order = MeshCells[hull, 2][[1, 1]];
Graphics[{Yellow, Polygon[points[[order]]], Black, Point[d]}]