1
$\begingroup$

This question already has an answer here:

I have to say the RegionMember is very slow

poly = Polygon[{{1, 0}, {0, 1}, {0, 0}}];
SeedRandom[1234];
pts = RandomReal[{0, 1}, {10^4, 2}];
RegionMember[poly, #] & /@ pts // Tr // AbsoluteTiming

{18.1054,5073 False+4927 True}

How to speed up it?

$\endgroup$

marked as duplicate by J. M. is away Dec 18 '16 at 19:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4
$\begingroup$

You can also reduce overhead by passing all points at once or by creating a RegionMemberFunction:

RegionMember[poly, pts] // Tr // AbsoluteTiming
{0.00396, 5073 False + 4927 True}
rm = RegionMember[poly];
rm[pts] // Tr // AbsoluteTiming
{0.002928, 5073 False + 4927 True}
rm = RegionMember[poly];
rm /@ pts // Tr // AbsoluteTiming
{0.06013, 5073 False + 4927 True}
$\endgroup$
  • $\begingroup$ Thanks,this is good lesson to me. :) $\endgroup$ – yode Dec 18 '16 at 18:00
0
$\begingroup$

I found a kernel function,which will not be abandon and be change arbitrarily in future version I think.So I want to share it in here.

Graphics`PolygonUtils`InPolygonQ

poly = Polygon[{{1, 0}, {0, 1}, {0, 0}}];
SeedRandom[1234];
pts = RandomReal[{0, 1}, {10^4, 2}];
Graphics`PolygonUtils`InPolygonQ[poly, #] & /@ pts // 
  Tr // AbsoluteTiming

{0.0582609,5073 False+4927 True}

$\endgroup$
  • $\begingroup$ @corey979 It always give False confusing me still.And it is a kernel function,too. $\endgroup$ – yode Dec 18 '16 at 18:33

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