8
$Version

"10.4.1 for Microsoft Windows (64-bit) (April 11, 2016)"

I want to match the based point of VoronoiMesh(pts in code) with its polygon(poly in code).This is my code

SeedRandom[2016526]
pts = RandomReal[{-1, 1}, {15, 2}];
mesh = VoronoiMesh[pts];
Show[%, Graphics[{Orange, Point[pts]}]]
poly = MeshPrimitives[mesh, 2];

enter image description here

When using the documentation of RelationGraph's first method:

RelationGraph[RegionMember[#2, #1] &, Join[pts, poly]]

We get a wrong result and a error information like

enter image description here

In this case,I can understand its error information but its wrong result.Then we try its second method in documentation.

RelationGraph[RegionMember[#2, #1] &, pts, poly]

We get some error information and the right result.Confuse me about it error informations like following

enter image description here

But I think I'm using this function in a right way.How to understand these phenomena?And when I use Echo to look this code's process

enter image description here

But as the documentation

enter image description here

I think shouldn't appear the elements of pts to be the parameter of RegionMember at the same time.This is a bug or I miss something?And how to adjust this code?

yode
  • 26,686
  • 4
  • 62
  • 167

1 Answers1

3

Two argument form:

RelationGraph[f, {v1, v2, ...}] gives the graph with vertices vi and edges from vi to vj whenever f[vi, vj] is True.

What's not stated is that there is a check that f[v1, v1] returns explicitly True or False. You can use TrueQ to ensure this, so for example this gives the correct result:

RelationGraph[TrueQ @ RegionMember[#2, #1] &, Join[pts, poly]]

Of course there are still warnings when RegionMember[point, polygon] is called.

Three argument form:

RelationGraph[f, {v1, v2, ...}, {w1, w2, ...}] gives the graph with vertices vi, wj and edges from vi to wj whenever f[vi, wj] is True.

If you check the internal code (for example with GeneralUtilities`PrintDefinitions[RelationGraph]) it is clear that the criterion is tested in both directions, f[vi, wj] and f[wj, vi]. So again there are warnings when RegionMember[point, polygon] is called. It's not clear if this is a bug or incomplete documentation.

Workaround:

In either case you can get the correct result without warnings by using a function that only calls RegionMember if the arguments are the right way round, and returns False otherwise.

rm[q_List, p_Polygon] := RegionMember[p, q]
rm[__] = False;

RelationGraph[rm, pts, poly]

enter image description here

Simon Woods
  • 84,945
  • 8
  • 175
  • 324