I am trying to write small program to check if one rectangle contains second rectangle and distance betwwen their borders should be less than some specific number like 100 or 50. When I use Rectangle.contains method it doesnt care about about distnce between borders of both rectangle. Is there any way this can be achieved?
-
Are the rectangles always orthogonal? – nhahtdh Jun 09 '12 at 12:09
-
Yes their axises are always aligned. – user613114 Jun 09 '12 at 12:14
4 Answers
You could use the contains(Rectancle r) method twice: first to check if the inner rectangle is inside the outer rectangle at all, then temporarily enlarge the inner rectangle by half the threshold into every direction and make the same check again. This time it shouldn't be inside the outer rectangle anymore. So basically something like this:
//Rectangle outer; // Do some proper setup for these two
//Rectangle inner;
int limit = 50;
boolean containsWithinLimits = outer.contains(inner);
inner.setLocation(inner.getX()-limit/2, inner.getY()-limit/2);
inner.setSize(inner.getWidth()+limit, inner.getHeight()+limit);
boolean containsWithinLimits = containsWithinLimits && !outer.contains(inner);
// Now reset the bounds:
inner.setLocation(inner.getX()+limit/2, inner.getY()+limit/2);
inner.setSize(inner.getWidth()-limit, inner.getHeight()-limit);

- 3,041
- 3
- 31
- 52
-
Xes it solves this issue. Thanks. But my major problem on which I am working is not solved. And I have created a separate thread for the same. [http://stackoverflow.com/questions/10959703/swing-jxmapviewer-focus-on-a-object-and-set-corresponding-zoom-level]. If you all have any expertise in this issue, you can help me. – user613114 Jun 09 '12 at 13:59
example code etc?
Even so, you'll want to do the following: unless im mistaken.
pseudo code:
rectA = outside, rectB = inside
if rectB.left - rectA.left < x then distance is ok else do something
if rectB.top - rectA.top < y then distance is ok else do something
etc etc

- 993
- 2
- 13
- 29
If the rectangles are orthogonal, and I assume that you have called contains(Rectangle rect)
. Then you only have to do extra comparisons:
inside.x - outside.x >= LEFT_BORDER &&
outside.x + outside.width - inside.x - inside.width >= RIGHT_BORDER &&
inside.y - outside.y >= TOP_BORDER &&
outside.y + outside.height - inside.y - inside.height >= BOTTOM_BORDER
LEFT_BORDER
, RIGHT_BORDER
, TOP_BORDER
, BOTTOM_BORDER
are for you to define.

- 55,989
- 15
- 126
- 162
-
How do you calculate values for leftBorder, rightBorder etc. I do not see any API for this inside rectnagle class – user613114 Jun 09 '12 at 12:30
-
In Rectangle2d ,we are having
boolean contains(double x, double y)
--- Tests if a specified coordinate is inside the boundary of this Rectangle2D.
boolean contains(double x, double y, double w, double h)
--- Tests if the interior of this Rectangle2D entirely contains the specified set of rectangular coordinates.
So it is not possible to check about the distance to my knowledge...

- 432
- 2
- 9