Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm struggling to understand the behaviour of ConvexHullMesh (or rather the non-behaviour). I have a polynom p

p = { {0, 0}, {1, 0}, {1/2, 1/2}, {1, 1}, {0, 1}}

which I can display in a nice way like by e.g.:

Show[Graphics[{LightBlue, EdgeForm[Gray], Polygon[p]}],  Graphics[ {PointSize[Large], Red, Point[#]}] & /@ p]

Now I construct the convex hull

q = ConvexHullMesh[p];

which displays a somewhat featureless rectangle. Having a closer look at q

q // InputForm

I get

BoundaryMeshRegion[{{0., 0.}, {0., 1.}, {1., 0.}, {1., 1.}}, {Line[{{1, 3}, {3, 4}, {4, 2}, {2, 1}}]}, Method -> {"EliminateUnusedCoordinates" -> True, DeleteDuplicateCoordinates" -> Automatic, "VertexAlias" -> Identity, "CheckOrientation" -> True, "CoplanarityTolerance" -> Automatic, "CheckIntersections" -> Automatic, "BoundaryNesting" -> {{0, 0}}, "SeparateBoundaries" -> False, "PropagateMarkers" -> True, "Hash" -> 1136472811504667718}]

so the obvious (?) idea is to replace the head of q (i.e. BoundaryMeshRegion) by a suitably defined function f that uses the argument to display the convex hull in a 'nicer' way. Ignoring the exact implementation of f, the 'usual' way fails, as

f @@ q 

just redisplays the rectangle. Head[q] gives the expected BoundaryMeshRegion, but Apply (or @@) somehow fails to replace the head. Similarly, the alternative q[[1]] to access the argument of the head fails.

What am I missing?

Or to be more precise:

  • How do I change the appearance of ConvexHullMesh?

  • Why does the above solution not work?

share|improve this question
up vote 10 down vote accepted

While the answers provided so far are nice, it seems like there should be easier ways to achieve this. And there are, I will show two ways: 1) Keeping your ConvexHullMesh as a BoundaryMeshRegion object and (2 converting to a Graphics object.

SeedRandom[0]
pts = RandomReal[4, {200, 3}];
chull = ConvexHullMesh[pts];

First we use HighlightMesh with no need to recreate the MeshRegion:

HighlightMesh[chull, {Style[0, Directive[PointSize[0.015], Red]], 
  Style[1, Thin, Blue], Style[2, Opacity[0.5], Yellow]}]

Mathematica graphics

Note the use of Style to style the various dimensions (0 for vertices, 1 for edges and 2 for facets. Also note that this object is still a BoundaryMeshRegion and you can compute other nice mesh-related properties from it.

Now the Graphics object approach.

Graphics3D[GraphicsComplex[MeshCoordinates[chull], {PointSize[0.02], Red,       
  MeshCells[chull, 0], Blue, MeshCells[chull, 1], Green, MeshCells[chull, 2]}], 
  Boxed -> False]

Mathematica graphics

Note the use of the already available properties of the MeshRegion. No need to use Show here.

share|improve this answer
    
@RunnyKine...thanks for this wonderful and illustrative, general (and best answer) +1...mine was a hack on the fly... – ubpdqn Aug 24 '14 at 19:13
    
@ubpdqn, Thanks for the up-vote. I thought this was worth sharing since it's not obvious at first how to style these Mesh objects directly. – RunnyKine Aug 24 '14 at 19:19
    
I am still negotiating MMA10 and the computational geometry features...so very happy to learn more.,,so excellent answer – ubpdqn Aug 24 '14 at 19:24
    
Thank you, this is exactly what I was looking for. +1 – Oliver Jennrich Sep 12 '14 at 15:34

Show allows you to convert to Graphics object:

Graphics[Show[ConvexHullMesh[p]][[1]] /. {Directive[x_] :> 
    Directive[{Red, EdgeForm[{Black, Thickness[0.02]}]}]}]

enter image description here

Or perhaps a little more interesting:

pts = RandomReal[{0, 1}, {50, 2}];
g = Graphics[{Red, PointSize[0.03], Point[pts]}];
ch = Show[ConvexHullMesh[pts]][[1]] /. {Directive[x_] :> 
     Directive[{Yellow, EdgeForm[{Black, Thickness[0.02]}]}]};
Show[Graphics[ch], g]

enter image description here

share|improve this answer
    
Now that is cool. Thanks a lot. Now, any idea why my 'Apply'-idea doesn't work? – Oliver Jennrich Aug 24 '14 at 14:39
p = {{0, 0}, {1, 0}, {1/2, 1/2}, {1, 1}, {0, 1}};

Show[

 ConvexHullMesh[p],

 HighlightMesh[
  MeshRegion[p, Polygon[Range@Length@p],
   MeshCellStyle -> {2, All} -> Opacity[0.5, Orange]],
  Labeled[0, "Index"]]]

enter image description here

share|improve this answer

Why the above solution (f@@q) doesn't work?

Because a MeshRegion is an AtomQ.

How do I change the appearance of ConvexHullMesh?

Because

The convex hull region is the smallest convex region that includes the points

the computed and returned ConvexHullMesh is of course correct. Because you are computing the convex hull, maybe it's because you are interested to show the input list of points and their convex hull. You can use Show to combine directly a MeshRegion with another Graphics.

Maybe you are interested to DelaunayMesh, related with but different from ConvexHullMesh (returns a MeshRegion instead of a BoundaryMeshRegion).

p = {{0, 0}, {1, 0}, {1/2, 1/2}, {1, 1}, {0, 1}};
q = ConvexHullMesh[p];
{Show[q, Graphics@Point@p], HighlightMesh[DelaunayMesh[p], Style[0, Red]]}

Mathematica graphics

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.