Store the map as a list of directed line segments (so that we can determine if we're infront of or behind a segment):
struct Segment {
Pos2 p0;
Pos2 p1;
int holeIndex; //Which hole this segment delimits
};
Then partition the segments into a BSP-tree:
struct BSPNode {
Segment partition;
BSPNode* infront;
BSPNode* behind;
};
Then you can find the hole with this code:
int
findHole(BSPNode* node, Pos2 pos) {
if (!node->behind) { // This node is a leaf
if (isBehind(pos2, node->partition)) {
return node->partition->holeIndex;
} else {
return -1; //Point is not in a hole
}
}
if (isBehind(pos2, node->partition)) {
return findHole(node->behind, pos);
} else {
return findHole(node->infron, pos);
}
}
int hole = findHole(root, somePos);
If it is the case that each hole is a single rectangle you could BSP the set of rectangular holes until you have a single rectangle in each partition.
struct BSPNode {
union {
Rectangle rectangle; //leaf node
DirectedLine splitter; //branch node
};
BSPNode* infront; // If null indicates this is a leaf node
BSPNode* behind;
};
int
findHole(BSPNode* node, Pos2 pos) {
if (!node->behind) { // This node is a leaf
if (isInside(pos2, node->rectangle)) {
return node->rectangle->holeIndex;
} else {
return -1; //Point is not in a hole
}
}
if (isBehind(pos2, node->splitter)) {
return findHole(node->behind, pos);
} else {
return findHole(node->infron, pos);
}
}