如果我在列表中包含多边形的所有外边缘,我将如何找到内部坐标?为简单起见,我画了下面的图像来表示问题:
我需要找到建筑物的内部'在基于拼图的游戏中。
如果建筑物没有完全显示在视图中(右侧建筑物),我通过将整个绿色部分(-1,-1,0,-1等)添加到列表中来解决问题。
如果搜索树没有跟随一些疯狂,我不知道如何解决这个问题。我在这里发布一些提示,代码或伪代码。任何帮助都非常感激。非常感谢! :)
修改
@Andrew Thompson:我想我错误地写了我的情况。这与您链接的副本不同。我没有图像。我上面做的excel绘图只是一个例子。对于那个例子:我有一个包含棕色值的列表: 即。 {" 1,1"," 2,1"," 3,1"," 1,2"等等。}
我需要一个相应的蓝色值列表:即。 {" 2,2"," 2,6"," 3,6"," 4,6"等等。}
答案 0 :(得分:2)
本周我玩弄了A *算法。您的请求可能还有其他解决方案,但由于我已经拥有了代码,因此只需根据您的需求进行调整即可。但是,根据您的具体要求,您还可以简单地使用原始泛洪填充算法并以这种方式对单元格进行分类,请参阅算法代码中的方法。
A* algorithm找到从给定开始到给定目标的路径。在你的情况下,开始是目标,这意味着它是一个参考点,对"外部"进行分类。细胞。从那里开始,我们搜索所有可以遍历的东西。
我在示例中留下了寻找代码的路径,也许它可以满足您的进一步需求。
以下是代码:
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 :(得分:0)
我发现这是一个非常有趣的帖子:)我觉得很清醒,试图解决这些看似简单的问题,它真的很新鲜。
我提出了一个可以处理矩形区域的简单算法。它只是在纸上,未经测试,但我会尽可能地解释它,以便您可以将其转换为实际代码。
我的想法是对该区域执行水平扫描,并使用某种状态机来识别可能的蓝色瓷砖。这个过程或多或少如下:
我现在开会,如果我有时间,我会写一个简单的伪代码(只是为了好玩),并在循环的每个点进行检查。如果我有更多的时间,我会考虑某种改进来检测非方形区域。我认为它可以非常简单,例如寻找与标记列相交的连续棕色瓷砖(通过标记,我的意思是例如上面运行的样品中的1和3)并相应地调整该区域的新标记限制。
我会尝试考虑类似山的区域,在那里您可以检测到地图上半部分的两个不同区域,但最终会在下半部分连接起来。毕竟我从未想过这是一个简单的问题:)同时,我希望我已经能够为你提供一些见解。
答案 2 :(得分:0)
这确实是floodfill问题。
你知道多边形顶点的坐标吗?然后有一些算法可以更快地计算洪水填充。你不必决定哪个部分是建筑物,哪个部分不是。
如果你不知道顶点,你将不得不做一个真实的&#34; floodfill:
如果您知道有一块瓷砖不在建筑物内,只需在那里填充,其余的就是建筑物。
如果您不知道,那么您可以继续填充,直到没有更多的瓷砖,并且您将该区域划分为多个区域。
然后实际上你不能在没有做出一些假设的情况下告诉其他建筑物。例如,假设您的区域被棕色线分成两部分:哪一个是建筑物?
也许你知道地面上的瓷砖比任何建筑都多?那两座建筑物不能相互接触?它们是凸的?