我的游戏中的城市是随机生成的,但是只能形成矩形的道路和十字路口图:
可以看出,我的地形非常空洞.我想要做的是找到每个空矩形并存储在矩形列表中,形成很多.
正如你在这个插图中看到的那样,我填写了3个’批次’,在1中我展示了它的3个矩形.
我的数据结构是:
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;
}
}
这个城市是道路和十字路口的列表.
是否有某种算法可以生成这些批次及其矩形?
谢谢
最佳答案 我想到的最简单的方法是使用泛洪填充算法来建立您的区域列表.所以基本上
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
最终的结果是你将拥有一个区域列表,然后你可以随心所欲地做任何事情(查看并查看任何包含的方块是否有建筑物,用户的颜色等等).
注意:要确定一个正方形是否是某个区域的一部分,我会在方形数据结构中添加一个标记的标记或某种东西,这样当你开始时,你会通过并清除所有这些标志,然后当你添加一个平方到您设置该标志的区域,当您想要检查方块是否在某个区域时,您需要做的就是检查该标志是否已设置.这样,您最终会使用线性时间算法来构建区域列表.
正如马库斯在这里的评论中指出的那样,这个“旗帜”实际上可能是一个指向Lot对象的指针/引用,该对象包含你的方块列表,无论如何都可能很方便.