Very often I encounter a situation that I want to select some points inside certain areas of a plot. For example,
p1 = Graphics[Point[Table[{Random[], Random[]}, {200}]]]
will give a plot. If I use MMA's drawing tool to draw a region in the plot,
p2=
how I get the plot dynamically updated so that it shows only the points inside that region?
Here is my attempt.
I use the function inPolyQ by rm -rf at here to test whether the points is inside the region or not.
lines = First[Cases[p2, Line[x_] :> x, \[Infinity]]];
pts = First[Cases[p2, Point[x_] :> x, \[Infinity]]];
inPolyQ[poly_, pt_] := Graphics`Mesh`PointWindingNumber[poly, pt] =!= 0
slctPts = Select[pts, inPolyQ[lines, #] &];
Graphics[{Point[slctPts], Line[lines]}, AbsoluteOptions[p1]]
the out put is

I have two problems:
- How can I change it to a efficient function that can do all this dynamically on a plot(or may density plot), and with the option to output the selected data points?
- There is a problem that sometimes I actually select more points than I want. Like in the example, there are two points outside the region are also selected. Is there a way to solve that?
Thanks.
Graphics`Mesh`InPolygonQ(with the same syntax). I mentioned this in a comment under my answer – rm -rf Feb 23 '13 at 20:22linesandptsfrom the plot. Why didn't you post an answer to that question? – matheorem Dec 11 '13 at 05:16