6 Replies Latest reply: Jan 23, 2009 11:31 PM by camickr RSS

    Rectangle intersects - with 2 rectangle arraylists

    807588
      The subject says it all, but to be more formal...

      Hi, I'm trying to write a method using ".intersects". I have two arraylists of rectangles and I'm wondering how I will be able to do this. Some code would be great. Here's what I got...
                  for (Rectangle rex : rectangles) {
                       for (Rectangle rex2 : oppshiprecs){
                       if (rex.intersects(rex2)) {
                            System.out.print("INTERSECTION");
                       }
                  }
      Doesn't work though.

      Thanks for reading,
      Nate
        • 1. Re: Rectangle intersects - with 2 rectangle arraylists
          807588
          Doesn't work though.
          Please feel free to elaborate.
          • 2. Re: Rectangle intersects - with 2 rectangle arraylists
            807588
            I have trying to detect collision between two rectangles which are contained inside of an arraylist. I am using Javas built in method called "intersect" which I believe is part of the Java.AWT package. I had it figured out when I was using one arraylist and one "stand alone" rectangle variable, but now that I have added another arraylist I am quite lost. This is what I had working:
                        for (Rectangle rex : rectangles) { // Arraylist
                             if (rex.intersects(standAloneRectangle)) {
                                  System.out.print("INTERSECTION");
                             }
            So my question is, how do I set this up so that I can check collision between, not just one arraylist, but TWO!

            Let me know if I am not making myself clear.

            Thanks,
            Nate
            • 3. Re: Rectangle intersects - with 2 rectangle arraylists
              807588
              Ah yes, thats my "attempt" at what I was trying to achieve
              • 4. Re: Rectangle intersects - with 2 rectangle arraylists
                807588
                how about using a boolean variable to hold info on whether they lists intersect or not. I'm guessing that if any pair of rectangles intersect then this should be true. so:
                  {
                    List<Rectangle> rectangles = new ArrayList<Rectangle>();
                    List<Rectangle> rectangles2 = new ArrayList<Rectangle>();
                    
                    boolean intersect = false;
                    for (int i = 0; i < rectangles.size() && !intersect; i++)
                    {
                      for (int j = 0; j < rectangles2.size() && !intersect; j++)
                      {
                        if (rectangles.get(i).intersects(rectangles2.get(j)))
                        {
                          intersect = true;
                        }
                      }
                    }
                    
                  }
                • 5. Re: Rectangle intersects - with 2 rectangle arraylists
                  807588
                  Hmm thats not working for me, the thing is:

                  What if array1 slot 5 collides array2 slot 3?
                  • 6. Re: Rectangle intersects - with 2 rectangle arraylists
                    camickr
                    What if array1 slot 5 collides array2 slot 3?
                    Thats why you have the two loops.

                    Do you understand the bascis of the code given to you.

                    First you start with slot 1 of the first array and then see if it intersects with any rectangle in the second array. Then you repeat for the next slot in array 1, etc, etc.

                    Try adding your own debug code to understand what is going on. We can't test it for you because we don't have your data in the arrays.

                    If you need further help then you need to create a [Short, Self Contained, Compilable and Executable, Example Program (SSCCE)|http://homepage1.nifty.com/algafield/sscce.html], that demonstrates the incorrect behaviour.

                    Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.