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

Check if one rectangle is completely inside the other Rectangle

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Both rectangles are formed by this test

There is aditional class called Point that is used in rectangle generation, it works completly fine, and i am adding it just for clarity
package net.thumbtack.school.figures.v1;



And there is main class


   

Method that i need should start like this: public boolean isInside(Rectangle rectangle)
I already have a simmilar class that tested if two rectangles Overlap in any part
return true;
   }
I think they should be pretty simmilar but i dont know how to modify it so it only retirn true when one rectangle completly inside the other.
 
Bartender
Posts: 1200
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vlad Timoshuk wrote:
I think they should be pretty simmilar but i dont know how to modify it so it only retirn true when one rectangle completly inside the other.



Try looking at just the x dimension first.  If one rectangle is completely inside another, what does that imply for the x values?




On a side note...
If 2 rectangles share an edge, does that count as inside.  e.g. Is new Rectangle(12, 20, 2, 10) "inside" new Rectangle(20, 20, 10, 15)?  Is a Rectangle "inside" itself?
 
Ryan McGuire
Bartender
Posts: 1200
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vlad Timoshuk wrote:



I'm not sure I'd make Point mutatable.  Since you reimplemented hashCode() and equals(), I suspect that you're going to use the hashCode for something - perhaps storing Point objects in a Collection that uses the hashCode in some way.  For example, let's say you instantiate a new Point(3,4) and store it in a HashSet.  If you then call moveTo(5,6) on that Point to modify the x and y values, and then search for a new Point(5,6) in the HashSet, you won't find it because it'll have a different hashCode() value than when it was inserted.

I'm not saying making Point mutatable is necessarily "wrong" - just that you have to be careful.
 
Ryan McGuire
Bartender
Posts: 1200
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vlad Timoshuk wrote:



You might consider renaming your method so that it reads correctly left-to-right.

Let's say largeRectangle = new Rectangle(20, 30, 20, 20) and smallRectangle = new Rectangle(20, 30, 10, 10).  (Same center, but smaller in both dimensions).

Having largeRectangle.isInside(smallRectangle) return true is non-intuitive.  Something like largeRectangle.contains(smallRectangle) would make things clearer.  Another option is to make "this" inside isInside() the potentially contained rectangle, not the containing one - i.e. so that smallRectangle.isInside(largeRectangle) returns true.

If you picked the isInside() name so that it's similar to your isIntersects() method, you could rename that other method to just intersects().
 
He baked a muffin that stole my car! And this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic