Java 在栅格图中寻找空的地形矩形
我的游戏中的城市是随机生成的,但它是一个只能形成矩形的道路和十字路口的图形: 可以看出,我的地形相当空旷。我想做的是找到每个空矩形并存储在矩形列表中,形成批次 正如你在这幅图中看到的,我填了3个“批次”,在1中我展示了它由3个矩形组成 我的数据结构是:Java 在栅格图中寻找空的地形矩形,java,algorithm,Java,Algorithm,我的游戏中的城市是随机生成的,但它是一个只能形成矩形的道路和十字路口的图形: 可以看出,我的地形相当空旷。我想做的是找到每个空矩形并存储在矩形列表中,形成批次 正如你在这幅图中看到的,我填了3个“批次”,在1中我展示了它由3个矩形组成 我的数据结构是: package com.jkgames.gta; import android.graphics.Bitmap; import android.graphics.RectF; public class Intersection exten
package com.jkgames.gta;
import android.graphics.Bitmap;
import android.graphics.RectF;
public class Intersection extends Entity
{
Road topRoad;
Road leftRoad;
Road bottomRoad;
Road rightRoad;
Bitmap image;
public Bitmap getImage()
{
return image;
}
public void setImage(Bitmap image)
{
this.image = image;
}
public Intersection(RectF rect, Bitmap image)
{
setRect(rect);
setImage(image);
}
public Road getTopRoad()
{
return topRoad;
}
public void setTopRoad(Road topRoad)
{
this.topRoad = topRoad;
}
public Road getLeftRoad()
{
return leftRoad;
}
public void setLeftRoad(Road leftRoad)
{
this.leftRoad = leftRoad;
}
public Road getBottomRoad()
{
return bottomRoad;
}
public void setBottomRoad(Road bottomRoad)
{
this.bottomRoad = bottomRoad;
}
public Road getRightRoad()
{
return rightRoad;
}
public void setRightRoad(Road rightRoad)
{
this.rightRoad = rightRoad;
}
@Override
public void draw(GraphicsContext c)
{
c.drawRotatedScaledBitmap(image, getCenterX(), getCenterY(),
getWidth(), getHeight(), getAngle());
}
}
public class Road extends Entity
{
private Bitmap image = null;
private Intersection startIntersection;
private Intersection endIntersection;
private boolean topBottom;
public Road(RectF rect, Intersection start, Intersection end,
Bitmap image, boolean topBottom)
{
setRect(rect);
setStartIntersection(start);
setEndIntersection(end);
setImage(image);
setTopBottom(topBottom);
}
@Override
public void draw(GraphicsContext c)
{
//Rect clipRect = c.getCanvas().getClipBounds();
//c.getCanvas().clipRect(getRect());
float sizeW;
float sizeH;
if(isTopBottom())
{
sizeW = getWidth();
sizeH = (sizeW / image.getWidth()) * image.getHeight();
}
else
{
sizeW = getHeight();
sizeH = (sizeW / image.getWidth()) * image.getHeight();
}
int numTiles = isTopBottom() ? (int)Math.ceil(getHeight() / sizeH) :
(int)Math.ceil(getWidth() / sizeW);
for(int i = 0; i < numTiles; ++i)
{
if(isTopBottom())
{
c.drawRotatedScaledBitmap(
image,
getRect().left + (sizeW / 2.0f),
(getRect().top + (sizeH / 2.0f)) + (sizeH * i),
sizeW, sizeH, 0.0f);
}
else
{
c.drawRotatedScaledBitmap(
image,
getRect().left + (sizeH / 2.0f) + (sizeH * i),
getRect().top + (sizeH / 2.0f),
sizeW, sizeH, (float)Math.PI / 2.0f);
}
}
// c.getCanvas().clipRect(clipRect);
}
public Bitmap getImage()
{
return image;
}
public void setImage(Bitmap image)
{
this.image = image;
}
public Intersection getStartIntersection()
{
return startIntersection;
}
public void setStartIntersection(Intersection startIntersection)
{
this.startIntersection = startIntersection;
}
public Intersection getEndIntersection()
{
return endIntersection;
}
public void setEndIntersection(Intersection endIntersection)
{
this.endIntersection = endIntersection;
}
public boolean isTopBottom()
{
return topBottom;
}
public void setTopBottom(boolean topBottom)
{
this.topBottom = topBottom;
}
}
package com.jkgames.gta;
导入android.graphics.Bitmap;
导入android.graphics.RectF;
公共类交叉扩展实体
{
上路;;
路![3]路;;
道底道;;
路权路;;
位图图像;
公共位图getImage()
{
返回图像;
}
公共void setImage(位图图像)
{
这个图像=图像;
}
公共交叉点(RectF rect、位图图像)
{
setRect(rect);
设置图像(图像);
}
公共道路通行费()
{
返回公路;
}
公共空间赛托普路(赛托普路)
{
this.topRoad=topRoad;
}
公共道路(
{
返回左路;
}
公共道路(左路)
{
this.leftRoad=leftRoad;
}
公共道路(
{
返回道;;
}
公众休憩用地及博顿道(道底道)
{
this.bottomRoad=bottomRoad;
}
公共道路getRightRoad()
{
返回右侧道路;
}
公共道路(右路)
{
this.rightRoad=rightRoad;
}
@凌驾
公共空白绘图(GraphicsContext c)
{
c、 drawRotatedScaledBitmap(图像,getCenterX(),getCenterY(),
getWidth(),getHeight(),getAngle());
}
}
公共级道路延伸实体
{
私有位图图像=空;
专用交叉口起点断面;
私人交叉路口;
私有布尔上下;
公共道路(RectF rect、交叉口起点、交叉口终点、,
位图图像,布尔值(上下)
{
setRect(rect);
设置交叉口(起点);
setEndIntersection(结束);
设置图像(图像);
setTopBottom(上下);
}
@凌驾
公共空白绘图(GraphicsContext c)
{
//Rect clipRect=c.getCanvas().getClipBounds();
//c、 getCanvas().clipRect(getRect());
浮萍;
浮子;
if(isTopBottom())
{
sizeW=getWidth();
sizeH=(sizeW/image.getWidth())*image.getHeight();
}
其他的
{
sizeW=getHeight();
sizeH=(sizeW/image.getWidth())*image.getHeight();
}
int numTiles=isTopBottom()?(int)Math.ceil(getHeight()/sizeH):
(int)Math.ceil(getWidth()/sizeW);
对于(int i=0;i
这个城市是一个道路和十字路口的列表
是否有某种算法可以生成这些地块及其矩形
谢谢我想到的最简单的方法是使用洪水填充算法来建立区域列表。所以基本上
foreach square:
if the square isn't part of a region:
create a new empty region list
add the square to it
recursivly add all neighboring squares to the region
最终的结果将是,你将有一个区域列表,你可以做任何你想做的事情(看看是否有任何包含的广场上有建筑物,颜色为用户,等等)
注意:为了确定一个正方形是否是一个区域的一部分,我会在正方形数据结构中添加一个标记的标志或其他东西,这样当你开始时,你会遍历并清除所有这些标志,然后当你向一个区域添加一个正方形时,你会设置这个标志,当你想检查一个正方形是否在一个区域中时,您所需要做的就是检查是否设置了该标志。这样,您就可以使用线性时间算法来构建区域列表
正如马库斯在这里的评论中指出的那样,这个“标志”实际上可能是一个指向保存正方形列表的地块对象的指针/引用,不管怎样,它可能很方便使用。for(int y=0;yfor (int y = 0; y < height; y++)
for (int x = 0; x < width; x++) {
if (x > 0 && squares[y][x].isConnectedTo(squares[y][x-1]) {
// Connected from the left
squares[y][x-1].getLot().addSquare(squares[y][x]);
if (y > 0 && squares[y][x].isConnectedTo(squares[y-1][x]) {
// Connected from both the left and above
squares[y-1][x].getLot().mergeWith(squares[y][x].getLot());
}
}
else if (y > 0 && squares[y][x].isConnectedTo(squares[y-1][x]) {
// Connected from above
squares[y-1][x].getLot().addSquare(squares[y][x]);
}
else {
// Not connected from either
createNewLot().addSquare(squares[y][x]);
}
}
对于(int x=0;x