Java 中的矩形重叠

Rectangle overlapping in 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;
        }

    }
}

然后是房间 class:

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;
}

}

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

您的问题是 overlaps 函数的这一部分:

overlap = false;

您的代码中发生的事情是您不断检查房间是否重叠,但如果您发现重叠的房间,则继续检查。然后,当您找到 重叠的房间时,您会重置标志。实际上,代码等同于检查最后一个房间。

完全删除重叠标志。而不是 overlap = true; 语句,而是 return true; (因为此时我们知道至少有一个房间是重叠的)。当你发现房间没有与其他房间重叠时(在 for 循环中),不要做任何事情。最后,在 for 循环之后 return false; 事实上,代码执行到了那个点意味着没有重叠的空间(否则它会刚刚返回)

注意:我认为条件 !r2.intersects(r1) && !r1.intersects(r2) 是多余的。 .intersects(r) 应该是可交换的,这意味着 r1.intersects(r2)r2.intersects(r1) 给出相同的结果。

对于您已初始化第一个房间的第一个问题,您不必这样做。

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

您只需检查第一个房间,无需检查重叠,因为它是第一个房间。 对于第二个问题,您可以在第一次找到相交时 return true,否则在循环结束时 return false。

供您参考的代码。

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) {
    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)) {
                    return true;
                } 
            }
        }
    }
    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) {
    generateMap();
}

public static void generateMap() {
    Room rooms[] = new Room[10];
    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( i ==0)
        {
            rooms[0] = room;
        }else if (room.overlaps(rooms) == false) {
            rooms[i] = room;
        }
    }
}

private static int randomWithRange(int min, int max) {
    // TODO Auto-generated method stub
    Random r = new Random();
    return r.nextInt((max - min) + 1) + min;
}
}