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 have a set of data supported over a region in 2D. I want to use RegionPlot and ContourPlot on interpolations of my data, but want to restrict the Plots to the region over which my data is supported.

Paste of the supports of my data.

I easily find the region with ConvexHull from the ComputationalGeometry package:


enter image description here

Now I want to turn the information into something I can use as RegionFunction for the ContourPlots and as restrictions in RegionPlot.

Can you show me a way?

share|improve this question
Thank you, I agree, it is almost a duplicate. Only new thing here is the use as RegionFunction, but the efficiency of the anwers provided at your link should allow that. – Neuneck Aug 24 '13 at 15:35
up vote 8 down vote accepted

Version 10 (now available publicly through the Programming Cloud) supports an alternative way for restricting functions to a certain region. This method can be directly used with Polygon objects, as well as any other geometrical region.

Here's an example:

poly=Polygon[{{1, 0}, {0, Sqrt[3]}, {-1, 0}}]

ContourPlot[x^2+y^2, {x,y} ∈ poly]

enter image description here

There's also new functionality for finding convex hulls, and the resulting object is a region, so it can be used in ContourPlot as the function domain:

points = RandomReal[{-1, 1}, {6, 2}]

ContourPlot[x^2+y^2, {x,y} ∈ ConvexHullMesh[points]]

enter image description here

Warning: using this syntax will cause Mathematica to use a different sampling algorithm during plotting, as evidenced by the mesh that gets generated:

enter image description here

share|improve this answer

I just found a solution in a comment by J.M.

He offers the function

inPolyQ[pt_?VectorQ, poly_?MatrixQ] := 
Chop[Total[VectorAngle @@@ Partition[(# - pt) & /@ poly, 2, 1, 
{1, 1 - 2 Boole[TrueQ[First[poly] == Last[poly]]]}]] - 2 π] == 0

to do exactly what I need to be done.

If l2 is the List of the supports of my data, I can use

pol = l2[[ConvexHull[l2]]];
inPol = inPolyQ2[#, pol] &;

to have a constraint for RegionPlot:

RegionPlot[otherStuff && inPol[{x, y}], {x, 0, 100}, {y, 0, 60}]

and to have a RegionFunction

ContourPlot[int[x, y], {x, 0, 100}, {y, 0, 60}, RegionFunction -> (inPol[#1, #2] &)]

Edit: There even is the built-in function GraphicsMeshInPolygonQ[poly, point] that checks if "point" is in the polygon "poly".

share|improve this answer

Your Answer


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.