6
$\begingroup$

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:

Graphics[Polygon[l2[[ConvexHull[l2]]]]]

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?

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Neuneck Aug 24 '13 at 15:35
9
$\begingroup$

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

$\endgroup$
2
$\begingroup$

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".

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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