Slick Forums

Discuss the Slick 2D Library
It is currently Wed Nov 21, 2018 4:27 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Shape Contains Bug?
PostPosted: Sat Apr 21, 2012 6:19 pm 
Offline

Joined: Sun Mar 11, 2012 12:36 pm
Posts: 8
Location: Germany
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


Top
 Profile  
 
 Post subject: Re: Shape Contains Bug?
PostPosted: Sat Apr 21, 2012 8:01 pm 
Offline
Regular
User avatar

Joined: Thu May 05, 2011 8:35 pm
Posts: 231
Location: Somewhere between the bits and bytes
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.

_________________
For every new problem, a new source of solutions has come to exist.


Top
 Profile  
 
 Post subject: Re: Shape Contains Bug?
PostPosted: Sun Apr 22, 2012 11:29 am 
Offline

Joined: Sun Mar 11, 2012 12:36 pm
Posts: 8
Location: Germany
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.


Top
 Profile  
 
 Post subject: Re: Shape Contains Bug?
PostPosted: Mon Apr 23, 2012 11:57 am 
Offline
Regular
User avatar

Joined: Thu May 05, 2011 8:35 pm
Posts: 231
Location: Somewhere between the bits and bytes
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.

_________________
For every new problem, a new source of solutions has come to exist.


Top
 Profile  
 
 Post subject: Re: Shape Contains Bug?
PostPosted: Wed Aug 29, 2012 8:20 am 
Offline

Joined: Wed Aug 29, 2012 7:49 am
Posts: 2
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;
}


Top
 Profile  
 
 Post subject: Re: Shape Contains Bug?
PostPosted: Wed Aug 29, 2012 8:30 am 
Offline

Joined: Wed Aug 29, 2012 7:49 am
Posts: 2
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);
   }


Top
 Profile  
 
 Post subject: Re: Shape Contains Bug?
PostPosted: Wed Aug 29, 2012 9:19 pm 
Offline
Regular
User avatar

Joined: Thu May 05, 2011 8:35 pm
Posts: 231
Location: Somewhere between the bits and bytes
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.

_________________
For every new problem, a new source of solutions has come to exist.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group