亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java中的矩形重疊

Java中的矩形重疊

30秒到達戰場 2022-01-19 17:02:15
我正在嘗試制作一個隨機地圖生成器。它應該在隨機坐標處創建一個隨機大小的房間,并刪除它與其他房間重疊的房間。但是,重疊檢查不起作用。以下是代碼的相關部分: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;}}所以我一直在測試這個,它每次都會刪除幾個房間,但是根據房間的數量,總會有一些重疊的。一定有一些我現在看不到的愚蠢簡單的解決方案......另外,除非我手動添加第一個房間,否則它為什么不生成任何房間?謝謝
查看完整描述

2 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

您的問題是這部分overlaps功能:

overlap = false;

您的代碼中發生的情況是,您會繼續檢查房間是否重疊,但如果您找到重疊的房間,則繼續檢查。然后當你找到一個重疊的房間時,你重置標志。實際上,該代碼等同于僅檢查最后一個房間。

完全刪除重疊標志。而不是overlap = true;put 語句return true;(因為此時我們知道至少有一個房間是重疊的)。當你發現房間沒有與其他房間重疊時不要做任何事情(在 for 循環中)。最后,在 for 循環之后return false;,代碼執行到那個點意味著沒有重疊的空間(否則它會剛剛返回)

注意:我認為這個條件!r2.intersects(r1) && !r1.intersects(r2)是多余的。.intersects(r)應該是可交換的,這意味著r1.intersects(r2)r2.intersects(r1)給出相同的結果。


查看完整回答
反對 回復 2022-01-19
?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

對于您已初始化第一個房間的第一個問題,您不必這樣做。


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) {

    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;

}

}


查看完整回答
反對 回復 2022-01-19
  • 2 回答
  • 0 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號