[英]Find inside coordinates of polygon in tile based map
如果我在列表中具有多边形的所有外边缘,该如何找到内部坐标? 为简单起见,我绘制了以下图像来表示问题:
我需要在基于图块的游戏中找到“建筑物”的内部。
如果建筑物未在视图中完全显示(正确的建筑物),我已通过将整个绿色部分(-1,-1、0,-1等)添加到列表中来解决了该问题。
没有遵循一些疯狂的if搜索树,我不知道如何解决这个问题。 我在这里发布一些技巧,代码或伪代码。 任何帮助都非常感谢。 非常感谢! :)
编辑
@安德鲁·汤普森:我想我写错了情景法。 这与您链接到的重复项不同。 我没有图像。 我在上面做的Excel绘图只是一个例子。 对于该示例:
我有一个包含棕色值的列表:即。 {“ 1,1”,“ 2,1”,“ 3,1”,“ 1,2”等}
我需要一个对应的蓝色值列表: {“ 2,2”,“ 2,6”,“ 3,6”,“ 4,6”等}
这周我玩弄了A *算法。 您的请求可能还有其他解决方案,但是由于我已经有了代码,因此只需根据您的需求进行调整即可。 但是,对于您的特定要求,您还可以简单地使用原始的泛洪填充算法并以此方式对单元进行分类,请参见算法代码中的方法。
A *算法找到从给定起点到给定目标的路径。 在您的情况下,开始就是目标,这意味着它是对“外部”单元进行分类的参考点。 从那里开始,我们搜索可以遍历的所有内容。
我在示例中为您保留了查找路径的代码,也许它可以满足您的进一步需求。
这是代码:
Demo.java
import java.util.List;
import java.util.Set;
public class Demo {
public static void main(String[] args) {
// create grid like in the example
int cols = 9;
int rows = 9;
Grid grid = new Grid( cols, rows);
// create walls like in the example
grid.getCell( 1, 1).setTraversable( false);
grid.getCell( 2, 1).setTraversable( false);
grid.getCell( 3, 1).setTraversable( false);
grid.getCell( 1, 2).setTraversable( false);
grid.getCell( 3, 2).setTraversable( false);
grid.getCell( 6, 2).setTraversable( false);
grid.getCell( 7, 2).setTraversable( false);
grid.getCell( 8, 2).setTraversable( false);
grid.getCell( 1, 3).setTraversable( false);
grid.getCell( 2, 3).setTraversable( false);
grid.getCell( 3, 3).setTraversable( false);
grid.getCell( 6, 3).setTraversable( false);
grid.getCell( 6, 4).setTraversable( false);
grid.getCell( 7, 4).setTraversable( false);
grid.getCell( 1, 5).setTraversable( false);
grid.getCell( 2, 5).setTraversable( false);
grid.getCell( 3, 5).setTraversable( false);
grid.getCell( 4, 5).setTraversable( false);
grid.getCell( 5, 5).setTraversable( false);
grid.getCell( 7, 5).setTraversable( false);
grid.getCell( 8, 5).setTraversable( false);
grid.getCell( 1, 6).setTraversable( false);
grid.getCell( 5, 6).setTraversable( false);
grid.getCell( 1, 7).setTraversable( false);
grid.getCell( 2, 7).setTraversable( false);
grid.getCell( 3, 7).setTraversable( false);
grid.getCell( 4, 7).setTraversable( false);
grid.getCell( 5, 7).setTraversable( false);
// find traversables
// -------------------------
AStarAlgorithm alg = new AStarAlgorithm();
Cell start;
Cell goal;
// reference point = 0/0
start = grid.getCell(0, 0);
Set<Cell> visited = alg.getFloodFillCells(grid, start, true);
// find inside cells
for( int row=0; row < rows; row++) {
for( int col=0; col < cols; col++) {
Cell cell = grid.getCell(col, row);
if( !cell.traversable) {
cell.setType(Type.WALL);
}
else if( visited.contains( cell)) {
cell.setType(Type.OUTSIDE);
}
else {
cell.setType(Type.INSIDE);
}
}
}
// log inside cells
for( int row=0; row < rows; row++) {
for( int col=0; col < cols; col++) {
Cell cell = grid.getCell(col, row);
if( cell.getType() == Type.INSIDE) {
System.out.println("Inside: " + cell);
}
}
}
// path finding
// -------------------------
// start = top/left, goal = bottom/right
start = grid.getCell(0, 0);
goal = grid.getCell(8, 8);
// find a* path
List<Cell> path = alg.findPath(grid, start, goal, true);
// log path
System.out.println(path);
System.exit(0);
}
}
Type.java
public enum Type {
OUTSIDE,
WALL,
INSIDE,
}
Cell.java
public class Cell implements Cloneable {
int col;
int row;
boolean traversable;
Type type;
double g;
double f;
double h;
Cell cameFrom;
public Cell( int col, int row, boolean traversable) {
this.col=col;
this.row=row;
this.traversable = traversable;
}
public double getF() {
return f;
}
public double getG() {
return g;
}
public double getH() {
return h;
}
public void setTraversable( boolean traversable) {
this.traversable = traversable;
}
public void setType( Type type) {
this.type = type;
}
public Type getType() {
return this.type;
}
public String toString() {
return col + "/" + row;
}
}
Grid.java
public class Grid {
Cell[][] cells;
int cols;
int rows;
public Grid( int cols, int rows) {
this.cols = cols;
this.rows = rows;
cells = new Cell[rows][cols];
for( int row=0; row < rows; row++) {
for( int col=0; col < cols; col++) {
cells[row][col] = new Cell( col, row, true);
}
}
}
public Cell getCell( int col, int row) {
return cells[row][col];
}
/**
* Get neighboring cells relative to the given cell. By default they are top/right/bottom/left.
* If allowDiagonals is enabled, then also top-left, top-right, bottom-left, bottom-right cells are in the results.
* @param cell
* @param allowDiagonals
* @return
*/
public Cell[] getNeighbors(Cell cell, boolean allowDiagonals) {
Cell[] neighbors = new Cell[ allowDiagonals ? 8 : 4];
int currentColumn = cell.col;
int currentRow = cell.row;
int neighborColumn;
int neighborRow;
// top
neighborColumn = currentColumn;
neighborRow = currentRow - 1;
if (neighborRow >= 0) {
if( cells[neighborRow][neighborColumn].traversable) {
neighbors[0] = cells[neighborRow][neighborColumn];
}
}
// bottom
neighborColumn = currentColumn;
neighborRow = currentRow + 1;
if (neighborRow < rows) {
if( cells[neighborRow][neighborColumn].traversable) {
neighbors[1] = cells[neighborRow][neighborColumn];
}
}
// left
neighborColumn = currentColumn - 1;
neighborRow = currentRow;
if ( neighborColumn >= 0) {
if( cells[neighborRow][neighborColumn].traversable) {
neighbors[2] = cells[neighborRow][neighborColumn];
}
}
// right
neighborColumn = currentColumn + 1;
neighborRow = currentRow;
if ( neighborColumn < cols) {
if( cells[neighborRow][neighborColumn].traversable) {
neighbors[3] = cells[neighborRow][neighborColumn];
}
}
if (allowDiagonals) {
// top/left
neighborColumn = currentColumn - 1;
neighborRow = currentRow - 1;
if (neighborRow >= 0 && neighborColumn >= 0) {
if( cells[neighborRow][neighborColumn].traversable) {
neighbors[4] = cells[neighborRow][neighborColumn];
}
}
// bottom/right
neighborColumn = currentColumn + 1;
neighborRow = currentRow + 1;
if (neighborRow < rows && neighborColumn < cols) {
if( cells[neighborRow][neighborColumn].traversable) {
neighbors[5] = cells[neighborRow][neighborColumn];
}
}
// top/right
neighborColumn = currentColumn + 1;
neighborRow = currentRow - 1;
if (neighborRow >= 0 && neighborColumn < cols) {
if( cells[neighborRow][neighborColumn].traversable) {
neighbors[6] = cells[neighborRow][neighborColumn];
}
}
// bottom/left
neighborColumn = currentColumn - 1;
neighborRow = currentRow + 1;
if (neighborRow < rows && neighborColumn >= 0) {
if( cells[neighborRow][neighborColumn].traversable) {
neighbors[7] = cells[neighborRow][neighborColumn];
}
}
}
return neighbors;
}
}
AStarAlgorithm.java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Set;
/**
* A* algorithm from http://en.wikipedia.org/wiki/A*_search_algorithm
*/
public class AStarAlgorithm {
public class CellComparator implements Comparator<Cell>
{
@Override
public int compare(Cell a, Cell b)
{
return Double.compare(a.f, b.f);
}
}
/**
* Find all cells that we can traverse from a given reference start point that's an outside cell.
* Algorithm is like the A* path finding, but we don't stop when we found the goal, neither do we consider the calculation of the distance.
* @param g
* @param start
* @param goal
* @param allowDiagonals
* @return
*/
public Set<Cell> getFloodFillCells(Grid g, Cell start, boolean allowDiagonals) {
Cell current = null;
Set<Cell> closedSet = new HashSet<>();
Set<Cell> openSet = new HashSet<Cell>();
openSet.add(start);
while (!openSet.isEmpty()) {
current = openSet.iterator().next();
openSet.remove(current);
closedSet.add(current);
for (Cell neighbor : g.getNeighbors(current, allowDiagonals)) {
if (neighbor == null) {
continue;
}
if (closedSet.contains(neighbor)) {
continue;
}
openSet.add(neighbor);
}
}
return closedSet;
}
/**
* Find path from start to goal.
* @param g
* @param start
* @param goal
* @param allowDiagonals
* @return
*/
public List<Cell> findPath( Grid g, Cell start, Cell goal, boolean allowDiagonals) {
Cell current = null;
boolean containsNeighbor;
int cellCount = g.rows * g.cols;
Set<Cell> closedSet = new HashSet<>( cellCount);
PriorityQueue<Cell> openSet = new PriorityQueue<Cell>( cellCount, new CellComparator());
openSet.add( start);
start.g = 0d;
start.f = start.g + heuristicCostEstimate(start, goal);
while( !openSet.isEmpty()) {
current = openSet.poll();
if( current == goal) {
return reconstructPath( goal);
}
closedSet.add( current);
for( Cell neighbor: g.getNeighbors( current, allowDiagonals)) {
if( neighbor == null) {
continue;
}
if( closedSet.contains( neighbor)) {
continue;
}
double tentativeScoreG = current.g + distBetween( current, neighbor);
if( !(containsNeighbor=openSet.contains( neighbor)) || Double.compare(tentativeScoreG, neighbor.g) < 0) {
neighbor.cameFrom = current;
neighbor.g = tentativeScoreG;
neighbor.h = heuristicCostEstimate(neighbor, goal);
neighbor.f = neighbor.g + neighbor.h;
if( !containsNeighbor) {
openSet.add( neighbor);
}
}
}
}
return new ArrayList<>();
}
private List<Cell> reconstructPath( Cell current) {
List<Cell> totalPath = new ArrayList<>(200); // arbitrary value, we'll most likely have more than 10 which is default for java
totalPath.add( current);
while( (current = current.cameFrom) != null) {
totalPath.add( current);
}
return totalPath;
}
private double distBetween(Cell current, Cell neighbor) {
return heuristicCostEstimate( current, neighbor); // TODO: dist_between is heuristic_cost_estimate for our use-case; use various other heuristics
}
private double heuristicCostEstimate(Cell from, Cell to) {
return Math.sqrt((from.col-to.col)*(from.col-to.col) + (from.row - to.row)*(from.row-to.row));
}
}
内部单元记录的结果是
Inside: 2/2
Inside: 7/3
Inside: 8/3
Inside: 8/4
Inside: 2/6
Inside: 3/6
Inside: 4/6
从0/0到8/8的路径的结果是
[8/8, 7/7, 7/6, 6/5, 5/4, 5/3, 5/2, 4/1, 3/0, 2/0, 1/0, 0/0]
我在JavaFX中为此编写了一个编辑器,如果您感兴趣的话,将很快发表在博客文章中。 基本上,您的网格将如下所示:
哪里
数字是来自A *算法的数字:
如果您不允许对角线移动,请像这样:
但这只是题外话:-)
我发现这是一个非常有趣的帖子:)我发现可以尝试解决这些看似简单的问题,这确实令人耳目一新。
我想出了一个可以处理矩形区域的简单算法。 它只是在纸上,未经测试,但我会尽力向您解释,以便您可以将其转换为实际代码。
我的想法是对区域进行水平扫描,并使用某种状态机来识别可能的蓝色瓷砖。 该过程大致如下:
我现在有个会议,如果有时间的话,我将编写一个简单的伪代码(只是为了好玩),并在循环的每个点进行检查。 如果有更多时间,我会考虑进行某种改进以检测非正方形区域。 我认为这很简单,就像寻找与标记的列相交的连续棕色瓷砖(标记为,例如,上面示例中的1和3)并相应地调整该区域的新标记限制。
我还将尝试考虑类似山区的区域,在该区域中您可以在地图的上部检测到两个不同的区域,但最终它们在下部合并在一起。 毕竟,我从来没有想过这是一个简单的问题:)同时,我希望能够为您提供一些见识。
这确实是一个洪水泛滥的问题。
您知道多边形顶点的坐标吗? 然后有一些算法可以更快地计算洪水。 您不必决定哪一部分是建筑物,哪一部分不是建筑物。
如果您不了解顶点,则必须进行“真实的”洪水填充:
如果您知道建筑物中没有一块瓷砖,则只需将其填满,剩下的就是建筑物。
如果您不知道,则可以继续进行填充,直到没有更多瓷砖为止,然后将区域划分为多个区域。
那么,实际上,您必须做出一些假设,才能从其余部分中分辨出建筑物。 例如,假设您的区域被一条棕色线分成两部分:建筑物是哪一个?
也许您知道地面上的瓷砖比任何建筑物都要多? 那两座建筑物不能互相碰触吗? 他们是凸的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.