14
$\begingroup$

In Mathematica 9, I was able to use the function Graphics`Mesh`InPolygonQ to determine whether a given point was within a polygon. How do I do this in Mathematica 10?

What I actually want to do is RegionPlot only the regions where my data has support. I used to define

data2 = Flatten[Table[{i, j, Sqrt[i^2 + j^2]}, {i, 1, 20}, {j, i/2, i, i/6}], 1];
supports = data2[[All, 1 ;; 2]];
<< ComputationalGeometry`
pol = supports[[ConvexHull[supports]]];
inPol = Graphics`Mesh`InPolygonQ[pol, #] &;

and could then use

RegionPlot[(Sqrt[x^2 + y^2] < 10) && inPol[{x, y}], {x, 0, 20}, {y, 0, 10}]

in order to plot a function (in the actual application it will be an interpolating function) only on the interesting region.

In Mathematica 10, Graphics`Mesh`InPolygonQ doesn't seem to exist anymore, or works differently.

$\endgroup$
  • 3
    $\begingroup$ That's because the function is in a different context in version 10: Graphics`PolygonUtils`InPolygonQ[]. The context change was already noted in this previous answer. $\endgroup$ – J. M.'s ennui Nov 12 '15 at 3:04
15
$\begingroup$

You can use ConvexHullMesh to generate a geometric region to give as a plotting domain to RegionPlot.

Using your data:

data2 = Flatten[Table[{i, j, Sqrt[i^2 + j^2]}, {i, 1, 20}, {j, i/2, i, i/6}], 1];
supports = data2[[All, 1 ;; 2]];

RegionPlot[Sqrt[x^2 + y^2] < 10, {x, y} ∈ ConvexHullMesh[supports]]

Mathematica graphics

More in detail, here is the relationship between the original RegionPlot with no restrictions (in grey), the convex hull of the supports (green), and the resulting plot shown above (red):

Show[
 {
  ConvexHullMesh[supports, MeshCellStyle -> {{2} -> Opacity[0.5, Green]}],
  RegionPlot[Sqrt[x^2 + y^2] < 10, {x, 0, 20}, {y, 0, 20}, PlotStyle -> LightGray, BoundaryStyle -> Dashed],
  RegionPlot[Sqrt[x^2 + y^2] < 10, {x, y} ∈ ConvexHullMesh[supports], PlotStyle -> Red]
 },
 Axes -> True
]

Mathematica graphics

$\endgroup$
  • $\begingroup$ I like that I can actually define the plotting region as the supported region, saving me a lot of hassle with extrapolation going wrong. $\endgroup$ – Neuneck Nov 12 '15 at 12:15
15
$\begingroup$

In v10 instead of Graphics`Mesh`InPolygonQ, you would use the more versatile RegionMember.

Rewriting your code to use it:

inPol = RegionMember[Polygon@pol, #] &

RegionPlot[(Sqrt[x^2 + y^2] < 10) && inPol[{x, y}], {x, 0, 20}, {y, 0, 10}]

Mathematica graphics

$\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.