Slick Forums
http://slick.ninjacave.com/forum/

Shape Contains Bug?
http://slick.ninjacave.com/forum/viewtopic.php?f=1&t=4862
Page 1 of 1

Author:  Morpheus [ Sat Apr 21, 2012 6:19 pm ]
Post subject:  Shape Contains Bug?

Hi,

I was using Rectangle and noticed that the method contains(Shape other) does not work. I looked up the
source code and found this.

Code:
  public boolean contains(Shape other) {
       if (other.intersects(this)) {
          return false;
       }
       
       for (int i=0;i<other.getPointCount();i++) {
          float[] pt = other.getPoint(i);
          if (!contains(pt[0], pt[1])) {
             return false;
          }
       }
       
       return true;
    }


This will always return false as other.intersects(this) will also return true when the shape contains the other shape.
Greetings

Morpheus

Author:  Magn919 [ Sat Apr 21, 2012 8:01 pm ]
Post subject:  Re: Shape Contains Bug?

contains() should only return true if the other shape is completely inside the first shape, therefore if any of the sides intersects, it returns false.

Author:  Morpheus [ Sun Apr 22, 2012 11:29 am ]
Post subject:  Re: Shape Contains Bug?

But intersects returns true if the shape contains the other. This

Code:
System.out.println(new Rectangle(0, 0, 20, 20)
            .intersects(new Rectangle(5, 5, 5, 5)));


returns true. Therefore contains will never return true.
Maybe it's just me or my computer, but for me contains does not work. Maybe anybody else could try it.

Author:  Magn919 [ Mon Apr 23, 2012 11:57 am ]
Post subject:  Re: Shape Contains Bug?

IIRC there's something about Rectangle.contains(Rectangle) not working, because of the way the Rectangle test intersection against Rectangle, but in exchange this way is extremely fast compared to the other shape.intersects(other) combinations.

Author:  JWill [ Wed Aug 29, 2012 8:20 am ]
Post subject:  Re: Shape Contains Bug?

Magn919 wrote:
IIRC there's something about Rectangle.contains(Rectangle) not working, because of the way the Rectangle test intersection against Rectangle, but in exchange this way is extremely fast compared to the other shape.intersects(other) combinations.


It's really fast, but it doesn't work? That's a problem.

someRectangle.contains(otherRectangle) always returns false.
This is because someRect.intersects(otherRect) returns true if otherRect is completely contained by someRect. Now, you could say this is how intersect should work. But if so, it should be specified in the javadoc. And contains shouldn't use it.

See below for the code in question. Why it doesn't work, and what would.

Assume input is bigRect.contains(smallRect), where small rect is contained within bigRect.
Code:
class Shape
public boolean contains(Shape other) {
      if (other.intersects(this)) { //returns true if rect is inside other rect
          return false;
       }
       
// execution never gets this far
<snip>}


Code:
class Rectangle
public boolean intersects(Shape shape) {
      if(shape instanceof Rectangle) {
            Rectangle other = (Rectangle)shape;
          if ((x > (other.x + other.width)) || ((x + width) < other.x)) {
             return false;
          }
          if ((y > (other.y + other.height)) || ((y + height) < other.y)) {
             return false;
          }
         return true;
   }
<snip>

If both the above if statements are false, the two rects must be intersecting, right?

No, this misses the case where one rect is inside another.

This logic needs to be in contains, or intersects.
Code:
// this rect is on inside of other
if (x > other.x && (x+width) < (other.x + other.width) && y > other.y && (y + height) < (other.y + other.height) {
return false;
}
//other rect is on inside of this
if (x < other.x && (x+width) > (other.x + other.width) && y < other.y && (y + height) > (other.y + other.height) {
return false;
}

Author:  JWill [ Wed Aug 29, 2012 8:30 am ]
Post subject:  Re: Shape Contains Bug?

Working around this bug at the moment with this:
Code:
/** Doesn't count identical (intersecting) rects as containing each other*/
   public static boolean contains(Rectangle a, Rectangle b) {
      float ax = a.getX();
      float ay = a.getY();
      float aw = a.getWidth();
      float ah = a.getHeight();

      float bx = b.getX();
      float by = b.getY();
      float bw = b.getWidth();
      float bh = b.getHeight();

      return ax < bx && (bx + bw) < (ax + aw) &&
            ay < by && (by + bh) < (ay + ah);
   }

Author:  Magn919 [ Wed Aug 29, 2012 9:19 pm ]
Post subject:  Re: Shape Contains Bug?

The best solution I see in this, would be to simply override the Shape.contains with one in the Rectangle class, in the overridden method it should handle the check correctly for rectangles, but use the old method for non-rectangles, sort of like what Rectangle.intersects do.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/