gpt4 book ai didi

java - 在网格图中查找空白地形矩形

转载 作者:搜寻专家 更新时间:2023-11-01 03:26:23 24 4
gpt4 key购买 nike

我游戏中的城市是随机生成的,但它是一张只能形成矩形的道路和十字路口图:

enter image description here

可以看出,我的地形非常空旷。我想要做的是找到每个空矩形并存储在矩形列表中,形成 Lots。

enter image description here

正如您在这张插图中看到的,我填写了 3 个“地 block ”,在 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;
}
}

城市是道路和交叉路口的列表。

是否有某种算法可以生成这些批处理及其矩形?

谢谢

最佳答案

我想到的最简单的方法是使用 flood-fill 算法来构建区域列表。所以基本上

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

最终结果将是您将拥有一个区域列表,然后您可以用它做任何您想做的事情(查看是否有任何包含的方 block 上有建筑物,是否为用户着色,等等) .

注意:为了确定一个正方形是否是区域的一部分,我会在正方形数据结构中添加一个标记的标志或其他东西,这样当你开始时,你会遍历并清除所有这些标志,然后作为您将一个正方形添加到您设置该标志的区域,当您想要检查一个正方形是否在一个区域中时,您需要做的就是检查该标志是否已设置。这样你最终会得到一个线性时间算法来构建你的区域列表。

正如 Markus 在这里的评论中指出的那样,这个“标志”实际上可能是一个指向包含您的方 block 列表的 Lot 对象的指针/引用,无论如何拿在手边可能会很方便。

关于java - 在网格图中查找空白地形矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12922158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com