Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

I have problem with code that check if 2 rectangles overlap

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this test form main rectangle and then several other rectangles



I have class point i addin it here for overall clarity but the should not be changed.


And this is main code that include constructor for rectangle AND method that will check if rectangles overlap



Problem is that when i run this code every time i get FALSE
 
Saloon Keeper
Posts: 7386
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your comparisons are backwards. Remember that X and Y increase as they go down and to the right.
 
Sheriff
Posts: 15929
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vlad Timoshuk wrote:

Problem is that when i run this code every time i get FALSE



Have you tried drawing this out and checking your conditions?

This shows a scenario where the conditional on lines 50-51 is true for both subexpressions. Your code will return false for isIntersects() when clearly the two rectangles intersect.

        =========
        =========
***********======
***********======
***********
 
Marshal
Posts: 7785
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Remember that X and Y increase as they go down and to the right.


Not top and right?
 
Marshal
Posts: 70625
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it should be right and down rather than down and right.

The (0, 0) point is at the top left of the screen. Carey is assuming screen pixels.
 
Carey Brown
Saloon Keeper
Posts: 7386
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Actually it should be right and down rather than down and right.

The (0, 0) point is at the top left of the screen. Carey is assuming screen pixels.


Well, I was assuming screen pixels, but it is a coordinate system with the origin in the upper left corner increasing to the right and down. You can verify this by the assertions of the rectangle intersections.
 
Junilu Lacar
Sheriff
Posts: 15929
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You can verify this by the assertions of the rectangle intersections.


You can also infer it from the calculations of TopLeft and BottomRight.
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic