4

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?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
yode
  • 26,686
  • 4
  • 62
  • 167

2 Answers2

7

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}
Greg Hurst
  • 35,921
  • 1
  • 90
  • 136
0

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}

yode
  • 26,686
  • 4
  • 62
  • 167