Reputation: 260
I've been working on a game (in JavaFX) where a player draws a path on a playing board and when he closes the path the interior tiles become his controlled area.
I've seen an approach to this for polygons that almost works. I iterate through the tiles to the right of the point in question and count how many times I enter a series of tiles that are part of the perimeter. if it's odd, it's inside (with a count of 1 being a special case).
The problem is points P1 vs P2 in the drawing below. Counting to the right both will have identical counts but one is inside, one is outside the enclosed area. I can't figure out how to define this special case.
Any pointers or thoughts are more than welcome.
Edited in response to the first comment: This example shows how looking above/below and/or left/right for points on both sides to make a determination may not work.
Upvotes: 1
Views: 1654
Reputation: 8068
I do not consider points sitting on a border cell. You have to define if such a point is considered to be inside or outside of the area.
Algorithm:
R
enclosing the area but sharing no cells with the area.P
, for which to determine if it is inside or outside the area, as unvisited
.unvisited
, P
is inside the area.C
marked as unvisited
and mark it as visited
.C
is a border cell of R
, P
is outside the area.C
(top, bottom, left, right) which are not marked as unvisited
or visited
or are a border cell of the area, and mark them as unvisited
.Example 1:
In this example the point P
is outside the area. This gets detected by step 5.
of the algorithm for the cell rendered in green.
Example 2:
In this example the point P
is inside the area. This gets detected by step 3.
of the algorithm if you would continue with the remaining two unvisited cells.
Example 3:
In this example the point P
is outside the area. This gets detected by step 5.
of the algorithm for the cells rendered in green.
Upvotes: 1