I would like to get points that are only inside a region. Here are the region and unfiltered points:

reg = Polygon[{{-(π/3), -(π/Sqrt[3])}, {-((2 π)/3),
     0}, {-(π/3), π/Sqrt[3]}, {π/3, π/Sqrt[3]}, {(
     2 π)/3, 0}, {π/3, -(π/Sqrt[3])}, {π/
     3, -(π/Sqrt[3])}, {-(π/3), -(π/Sqrt[3])}}];
allPoints= Flatten[
   ParallelTable[{x, y}, {x, -2.5, 2.5, 0.01}, {y, -2.5, 2.5, 0.01}], 1];

Now, to get the points inside the Polygon I used

npont = DeleteCases[allPoints, ({x_, y_} /; {x, y} ∉ reg)];

but it takes too long. I waited for 5 minutes without output. How can I speed this up?

  • 2
    $\begingroup$ try Select[RegionMember@reg]@allPoints? $\endgroup$
    – kglr
    Jan 8 at 1:01

As kglr says, you should use RegionMember. However, instead of mapping RegionMember (which is basically what his Select code does), you should provide all the points at once to the RegionMemberFunction. For example:

rmf = RegionMember[reg];

r1 = Select[rmf] @ allPoints; //AbsoluteTiming (* kglr *)
r2 = Pick[allPoints, rmf[allPoints]]; //AbsoluteTiming

r1 === r2

{3.96721, Null}

{0.083231, Null}



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.