Rectangle overlapping in Java












2















I'm trying to make a random map generator. It should create a random sized rooms at random coordinates, and remove the room it it overlaps with other rooms. However, the overlap checking isn't working. Here are the relevant parts of code:



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

}
}


And then the 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;
}


}



So I've been testing this, and it removes a few rooms each time, but there's always some that are overlapping depending on the number of rooms of course. There must be some stupid easy solution I just can't see right now... Also, why doesn't it generate any rooms unless I manually add the first one? Thanks










share|improve this question























  • Don't you want to early-out of the for loop when you set overlap to true? In that case, you could set overlap = true and then break out of the loop. Also, isn't it true that if r2 intersects r1, that r1 will intersect r2? Seems redundant to check both.

    – Michael Krause
    Nov 13 '18 at 23:54








  • 1





    1) See Collision detection with complex shapes for tips. 2) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

    – Andrew Thompson
    Nov 14 '18 at 0:31
















2















I'm trying to make a random map generator. It should create a random sized rooms at random coordinates, and remove the room it it overlaps with other rooms. However, the overlap checking isn't working. Here are the relevant parts of code:



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

}
}


And then the 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;
}


}



So I've been testing this, and it removes a few rooms each time, but there's always some that are overlapping depending on the number of rooms of course. There must be some stupid easy solution I just can't see right now... Also, why doesn't it generate any rooms unless I manually add the first one? Thanks










share|improve this question























  • Don't you want to early-out of the for loop when you set overlap to true? In that case, you could set overlap = true and then break out of the loop. Also, isn't it true that if r2 intersects r1, that r1 will intersect r2? Seems redundant to check both.

    – Michael Krause
    Nov 13 '18 at 23:54








  • 1





    1) See Collision detection with complex shapes for tips. 2) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

    – Andrew Thompson
    Nov 14 '18 at 0:31














2












2








2


0






I'm trying to make a random map generator. It should create a random sized rooms at random coordinates, and remove the room it it overlaps with other rooms. However, the overlap checking isn't working. Here are the relevant parts of code:



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

}
}


And then the 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;
}


}



So I've been testing this, and it removes a few rooms each time, but there's always some that are overlapping depending on the number of rooms of course. There must be some stupid easy solution I just can't see right now... Also, why doesn't it generate any rooms unless I manually add the first one? Thanks










share|improve this question














I'm trying to make a random map generator. It should create a random sized rooms at random coordinates, and remove the room it it overlaps with other rooms. However, the overlap checking isn't working. Here are the relevant parts of code:



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

}
}


And then the 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;
}


}



So I've been testing this, and it removes a few rooms each time, but there's always some that are overlapping depending on the number of rooms of course. There must be some stupid easy solution I just can't see right now... Also, why doesn't it generate any rooms unless I manually add the first one? Thanks







java procedural-generation roguelike






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 23:40









bunjobunjo

132




132













  • Don't you want to early-out of the for loop when you set overlap to true? In that case, you could set overlap = true and then break out of the loop. Also, isn't it true that if r2 intersects r1, that r1 will intersect r2? Seems redundant to check both.

    – Michael Krause
    Nov 13 '18 at 23:54








  • 1





    1) See Collision detection with complex shapes for tips. 2) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

    – Andrew Thompson
    Nov 14 '18 at 0:31



















  • Don't you want to early-out of the for loop when you set overlap to true? In that case, you could set overlap = true and then break out of the loop. Also, isn't it true that if r2 intersects r1, that r1 will intersect r2? Seems redundant to check both.

    – Michael Krause
    Nov 13 '18 at 23:54








  • 1





    1) See Collision detection with complex shapes for tips. 2) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

    – Andrew Thompson
    Nov 14 '18 at 0:31

















Don't you want to early-out of the for loop when you set overlap to true? In that case, you could set overlap = true and then break out of the loop. Also, isn't it true that if r2 intersects r1, that r1 will intersect r2? Seems redundant to check both.

– Michael Krause
Nov 13 '18 at 23:54







Don't you want to early-out of the for loop when you set overlap to true? In that case, you could set overlap = true and then break out of the loop. Also, isn't it true that if r2 intersects r1, that r1 will intersect r2? Seems redundant to check both.

– Michael Krause
Nov 13 '18 at 23:54






1




1





1) See Collision detection with complex shapes for tips. 2) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

– Andrew Thompson
Nov 14 '18 at 0:31





1) See Collision detection with complex shapes for tips. 2) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

– Andrew Thompson
Nov 14 '18 at 0:31












2 Answers
2






active

oldest

votes


















0














For your first issue where you have initialized first room, you don't have to do that.



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


You just need to check for first room and no need to check for overlap as it is the first room.
For the second issue, you can return true at the first time you find an intersect otherwise return false at the end of the loop.



Code for your reference.



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





share|improve this answer



















  • 1





    You shouldn't just provide a code, but rather explain why is code written in a way it is, what changed from original question and why. Code without any explanation is no way near as helpful as explanation of problem and solution (with optionally providing some code snippets)

    – John Smith
    Nov 14 '18 at 4:50



















1














Your problem is this part of overlaps function:



overlap = false;


What is happening in your code is that you keep checking rooms if they overlap or not, but if you find one which overlaps, you keep going. And then when you find a room which does not overlap, you reset the flag. Effectively the code is equivalent with just checking the last room.



Remove the overlap flag completely. Instead of overlap = true; statement put return true; (because at this point we know that at least one room is overlapping). Don't do anything when you find out that the room is not overlapping with other room (in the for cycle). At the end, after the for cycle just return false; Fact that code execution got to that point means there is no overlapping room (otherwise it would have just returned already)



Note: I believe that condition !r2.intersects(r1) && !r1.intersects(r2) is redundant. .intersects(r) should be commutative, meaning that that r1.intersects(r2) and r2.intersects(r1) give the same results.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291086%2frectangle-overlapping-in-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    For your first issue where you have initialized first room, you don't have to do that.



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


    You just need to check for first room and no need to check for overlap as it is the first room.
    For the second issue, you can return true at the first time you find an intersect otherwise return false at the end of the loop.



    Code for your reference.



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





    share|improve this answer



















    • 1





      You shouldn't just provide a code, but rather explain why is code written in a way it is, what changed from original question and why. Code without any explanation is no way near as helpful as explanation of problem and solution (with optionally providing some code snippets)

      – John Smith
      Nov 14 '18 at 4:50
















    0














    For your first issue where you have initialized first room, you don't have to do that.



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


    You just need to check for first room and no need to check for overlap as it is the first room.
    For the second issue, you can return true at the first time you find an intersect otherwise return false at the end of the loop.



    Code for your reference.



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





    share|improve this answer



















    • 1





      You shouldn't just provide a code, but rather explain why is code written in a way it is, what changed from original question and why. Code without any explanation is no way near as helpful as explanation of problem and solution (with optionally providing some code snippets)

      – John Smith
      Nov 14 '18 at 4:50














    0












    0








    0







    For your first issue where you have initialized first room, you don't have to do that.



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


    You just need to check for first room and no need to check for overlap as it is the first room.
    For the second issue, you can return true at the first time you find an intersect otherwise return false at the end of the loop.



    Code for your reference.



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





    share|improve this answer













    For your first issue where you have initialized first room, you don't have to do that.



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


    You just need to check for first room and no need to check for overlap as it is the first room.
    For the second issue, you can return true at the first time you find an intersect otherwise return false at the end of the loop.



    Code for your reference.



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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 14 '18 at 2:13









    SK -SK -

    796




    796








    • 1





      You shouldn't just provide a code, but rather explain why is code written in a way it is, what changed from original question and why. Code without any explanation is no way near as helpful as explanation of problem and solution (with optionally providing some code snippets)

      – John Smith
      Nov 14 '18 at 4:50














    • 1





      You shouldn't just provide a code, but rather explain why is code written in a way it is, what changed from original question and why. Code without any explanation is no way near as helpful as explanation of problem and solution (with optionally providing some code snippets)

      – John Smith
      Nov 14 '18 at 4:50








    1




    1





    You shouldn't just provide a code, but rather explain why is code written in a way it is, what changed from original question and why. Code without any explanation is no way near as helpful as explanation of problem and solution (with optionally providing some code snippets)

    – John Smith
    Nov 14 '18 at 4:50





    You shouldn't just provide a code, but rather explain why is code written in a way it is, what changed from original question and why. Code without any explanation is no way near as helpful as explanation of problem and solution (with optionally providing some code snippets)

    – John Smith
    Nov 14 '18 at 4:50













    1














    Your problem is this part of overlaps function:



    overlap = false;


    What is happening in your code is that you keep checking rooms if they overlap or not, but if you find one which overlaps, you keep going. And then when you find a room which does not overlap, you reset the flag. Effectively the code is equivalent with just checking the last room.



    Remove the overlap flag completely. Instead of overlap = true; statement put return true; (because at this point we know that at least one room is overlapping). Don't do anything when you find out that the room is not overlapping with other room (in the for cycle). At the end, after the for cycle just return false; Fact that code execution got to that point means there is no overlapping room (otherwise it would have just returned already)



    Note: I believe that condition !r2.intersects(r1) && !r1.intersects(r2) is redundant. .intersects(r) should be commutative, meaning that that r1.intersects(r2) and r2.intersects(r1) give the same results.






    share|improve this answer




























      1














      Your problem is this part of overlaps function:



      overlap = false;


      What is happening in your code is that you keep checking rooms if they overlap or not, but if you find one which overlaps, you keep going. And then when you find a room which does not overlap, you reset the flag. Effectively the code is equivalent with just checking the last room.



      Remove the overlap flag completely. Instead of overlap = true; statement put return true; (because at this point we know that at least one room is overlapping). Don't do anything when you find out that the room is not overlapping with other room (in the for cycle). At the end, after the for cycle just return false; Fact that code execution got to that point means there is no overlapping room (otherwise it would have just returned already)



      Note: I believe that condition !r2.intersects(r1) && !r1.intersects(r2) is redundant. .intersects(r) should be commutative, meaning that that r1.intersects(r2) and r2.intersects(r1) give the same results.






      share|improve this answer


























        1












        1








        1







        Your problem is this part of overlaps function:



        overlap = false;


        What is happening in your code is that you keep checking rooms if they overlap or not, but if you find one which overlaps, you keep going. And then when you find a room which does not overlap, you reset the flag. Effectively the code is equivalent with just checking the last room.



        Remove the overlap flag completely. Instead of overlap = true; statement put return true; (because at this point we know that at least one room is overlapping). Don't do anything when you find out that the room is not overlapping with other room (in the for cycle). At the end, after the for cycle just return false; Fact that code execution got to that point means there is no overlapping room (otherwise it would have just returned already)



        Note: I believe that condition !r2.intersects(r1) && !r1.intersects(r2) is redundant. .intersects(r) should be commutative, meaning that that r1.intersects(r2) and r2.intersects(r1) give the same results.






        share|improve this answer













        Your problem is this part of overlaps function:



        overlap = false;


        What is happening in your code is that you keep checking rooms if they overlap or not, but if you find one which overlaps, you keep going. And then when you find a room which does not overlap, you reset the flag. Effectively the code is equivalent with just checking the last room.



        Remove the overlap flag completely. Instead of overlap = true; statement put return true; (because at this point we know that at least one room is overlapping). Don't do anything when you find out that the room is not overlapping with other room (in the for cycle). At the end, after the for cycle just return false; Fact that code execution got to that point means there is no overlapping room (otherwise it would have just returned already)



        Note: I believe that condition !r2.intersects(r1) && !r1.intersects(r2) is redundant. .intersects(r) should be commutative, meaning that that r1.intersects(r2) and r2.intersects(r1) give the same results.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 0:19









        John SmithJohn Smith

        1087




        1087






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291086%2frectangle-overlapping-in-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud