猿问

Java中的矩形重叠

我正在尝试制作一个随机地图生成器。它应该在随机坐标处创建一个随机大小的房间,并删除它与其他房间重叠的房间。但是,重叠检查不起作用。以下是代码的相关部分:


public static void generateMap() {

    rooms[0] = new Room(0,10,10,5); // For some reason doesn't work without this?

    for (int i=0;i<ROOMS;i++) {

        int x = randomWithRange(0,WIDTH);

        int y = randomWithRange(0,HEIGHT);

        int height = randomWithRange(MINROOMSIZE,MAXROOMSIZE);

        int width = randomWithRange(MINROOMSIZE,MAXROOMSIZE);

        while (x+width > WIDTH) {

            x--;

        }

        while (y+height > HEIGHT) {

            y--;

        }

        Room room = new Room(x,y,width,height);

        if (room.overlaps(rooms) == false) {

            rooms[i] = room;

        }


    }

}

然后是 Room 类:


import java.awt.*;


public class Room {

    int x;

    int y;

    int height;

    int width;


public Room(int rx, int ry, int rwidth, int rheight) {

    x = rx;

    y = ry;

    height = rheight;

    width = rwidth;

}

boolean overlaps(Room[] roomlist) {

    boolean overlap = true;

    Rectangle r1 = new Rectangle(x,y,width,height);

    if (roomlist != null) {

        for (int i=0;i<roomlist.length;i++) {

            if (roomlist[i] != null) {

                Rectangle r2 = new Rectangle(roomlist[i].x,roomlist[i].y,roomlist[i].width,roomlist[i].height);

                if (!r2.intersects(r1) && !r1.intersects(r2)) {

                    overlap = false;

                }

                else {

                    overlap = true;

                }

            }                

        }

    }

    return overlap;

}

}


所以我一直在测试这个,它每次都会删除几个房间,但是根据房间的数量,总会有一些重叠的。一定有一些我现在看不到的愚蠢简单的解决方案......另外,除非我手动添加第一个房间,否则它为什么不生成任何房间?谢谢


30秒到达战场
浏览 180回答 2
2回答

holdtom

您的问题是这部分overlaps功能:overlap&nbsp;=&nbsp;false;您的代码中发生的情况是,您会继续检查房间是否重叠,但如果您找到重叠的房间,则继续检查。然后当你找到一个不重叠的房间时,你重置标志。实际上,该代码等同于仅检查最后一个房间。完全删除重叠标志。而不是overlap = true;put 语句return true;(因为此时我们知道至少有一个房间是重叠的)。当你发现房间没有与其他房间重叠时不要做任何事情(在 for 循环中)。最后,在 for 循环之后return false;,代码执行到那个点意味着没有重叠的空间(否则它会刚刚返回)注意:我认为这个条件!r2.intersects(r1) && !r1.intersects(r2)是多余的。.intersects(r)应该是可交换的,这意味着r1.intersects(r2)并r2.intersects(r1)给出相同的结果。

幕布斯6054654

对于您已初始化第一个房间的第一个问题,您不必这样做。rooms[0] = new Room(0,10,10,5); // For some reason doesn't work without this?您只需要检查第一个房间,无需检查重叠,因为它是第一个房间。对于第二个问题,您可以在第一次找到相交时返回 true,否则在循环结束时返回 false。代码供您参考。class Room {int x;int y;int height;int width;public Room(int rx, int ry, int rwidth, int rheight) {&nbsp; &nbsp; x = rx;&nbsp; &nbsp; y = ry;&nbsp; &nbsp; height = rheight;&nbsp; &nbsp; width = rwidth;}boolean overlaps(Room[] roomlist) {&nbsp; &nbsp; Rectangle r1 = new Rectangle(x, y, width, height);&nbsp; &nbsp; if (roomlist != null) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < roomlist.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (roomlist[i] != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle r2 = new Rectangle(roomlist[i].x, roomlist[i].y, roomlist[i].width, roomlist[i].height);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (r2.intersects(r1)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false;}}public class RoomGenerator {private static final int ROOMS = 10;private static final int WIDTH = 1200;private static final int HEIGHT = 1000;private static final int MINROOMSIZE = 10;private static final int MAXROOMSIZE = 120;public static void main(String[] args) {&nbsp; &nbsp; generateMap();}public static void generateMap() {&nbsp; &nbsp; Room rooms[] = new Room[10];&nbsp; &nbsp; for (int i = 0; i < ROOMS; i++) {&nbsp; &nbsp; &nbsp; &nbsp; int x = randomWithRange(0, WIDTH);&nbsp; &nbsp; &nbsp; &nbsp; int y = randomWithRange(0, HEIGHT);&nbsp; &nbsp; &nbsp; &nbsp; int height = randomWithRange(MINROOMSIZE, MAXROOMSIZE);&nbsp; &nbsp; &nbsp; &nbsp; int width = randomWithRange(MINROOMSIZE, MAXROOMSIZE);&nbsp; &nbsp; &nbsp; &nbsp; while (x + width > WIDTH) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x--;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; while (y + height > HEIGHT) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y--;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Room room = new Room(x, y, width, height);&nbsp; &nbsp; &nbsp; &nbsp; if( i ==0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rooms[0] = room;&nbsp; &nbsp; &nbsp; &nbsp; }else if (room.overlaps(rooms) == false) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rooms[i] = room;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}private static int randomWithRange(int min, int max) {&nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; Random r = new Random();&nbsp; &nbsp; return r.nextInt((max - min) + 1) + min;}}
随时随地看视频慕课网APP

相关分类

Java
我要回答