0

I was playing arround with hitboxes and ended up with following data:

final double targetSmallestX = targetCenter.getX() - targetHalfWidth;
final double targetSmallestY = targetCenter.getY() - targetHalfHeight;
final double targetHighestX = targetCenter.getX() + targetHalfWidth;
final double targetHighestY = targetCenter.getY() + targetHalfHeight;
final double sourceSmallestX = sourceCenter.getX() - sourceHalfWidth;
final double sourceSmallestY = sourceCenter.getY() - sourceHalfHeight;
final double sourceHighestX = sourceCenter.getX() + sourceHalfWidth;
final double sourceHighestY = sourceCenter.getY() + sourceHalfHeight;

What i want to do is to check, if the two given rectangles target and source are intersecting with each other. Could have done it like this

Rectangle target = new Rectangle(targetSmallestX, target.width, targetSmallestY, target.height);
Rectangle source = new Rectangle(sourceSmallestX, source.width, sourceSmallestY, source.height);
target.intersect(source);

But this would require me to work with integers.
All the algorithms I came up with seemed too long and complicated for such a seemingly simple task. Does anyone have an idea for a smart approach for this?

EDIT:
Current approach

(targetSmallestX < sourceSmallestX + this.width)
&& (sourceSmallestX < (targetSmallestX + target.width))
&& (targetSmallestY < sourceSmallestY + this.height)
&& (sourceSmallestY < targetSmallestY + target.height);

Does this check leave any possible constellations in which it wouldn't work correctly?

7
  • Take a look at the source code for Intersect here: referencesource.microsoft.com/#System.Drawing/commonui/System/… Commented Apr 21, 2018 at 21:35
  • What language is this? Commented Apr 21, 2018 at 22:25
  • Two rectangles intersect if and only if their x-values overlap, and their y-values overlap. You can write an "overlap" function that accepts range1Start, range1Width, range2Start, and range2Width and returns whether those ranges intersect. Then, call that twice for x-values and y-values. Commented Apr 21, 2018 at 22:51
  • Are the rectangles oriented parallel to the x/y axis? Commented Apr 21, 2018 at 23:27
  • @userunknown yes. I am not going to go through the trouble of calculating that stuff with angles Commented Apr 22, 2018 at 15:03

1 Answer 1

1

In Java, you can use java.awt.geom.Rectangle2D which represents coordinates with floating-point precision. Precisely, its inner class java.awt.geom.Rectangle2D.Double uses double to represent the coordinates.

Rectangle2D target = new Rectangle2D.Double(targetSmallestX, targetSmallestY, target.width, target.height);
Rectangle2D source = new Rectangle2D.Double(sourceSmallestX, sourceSmallestY, source.width, source.height);
target.intersect(source);
Sign up to request clarification or add additional context in comments.

1 Comment

Ty. Didn't know Rectangle has a Double implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.