0

I have a my rectangle. The application generates another rectangle. It can be more smaller or larger than my rectangle. How can I tell when its rect near of the mine using their X, Y, Weight and Hight?? I do not want to know if is into my rectangle.

Liuk
  • 351
  • 1
  • 13
  • please define "near" – Jan B. Jul 29 '16 at 11:18
  • Perhaps try the algorithm from this question http://stackoverflow.com/questions/4978323/how-to-calculate-distance-between-two-rectangles-context-a-game-in-lua – Bruce Hamilton Jul 29 '16 at 11:23
  • The generated rectangle should not be inside. It may be slightly over or higher or wide. but not very other than! – Liuk Jul 29 '16 at 11:24
  • You are being not exact enough. Please define "slightly". A distance of 1000000 pixels is "near" or not? – Jan B. Jul 29 '16 at 11:30

3 Answers3

0

Draw 1 or more non-visible shapes that are relative to your rectangle's position that fit your definition of "near", then check to see if these shape(s) intersect with the application-generated rectangle in question.

For example, one way you might implement this is drawing a non-visible rectangle that surrounds your rectangle, then checking to see if the surrounding rectangle intersects with the application-generated rectangle.

Zachary
  • 324
  • 1
  • 13
0

I found the solution! I have calculated the middle point of my rectangle. If the rectangle generated have into the point, is near!

Liuk
  • 351
  • 1
  • 13
-1

You can use the Math formula to calculate the distance between two points like this:

double getDistance(int x, int y, int x2, int y2) {
  double distance;
  distance = Math.sqrt( Math.pow( Math.abs(x2 - x) , 2 ) + Math.pow( Math.abs(y2 - y) , 2 ) );
  return distance;   
} 
  • Yes, but he can use this also to calculate the distance between centers or some other two points of the rectangles he decide. – D. Cosentino Jul 29 '16 at 11:37
  • 1
    I admit, your approach may be a part of the solution, but your answer does not solve his problem, at all. You should at least propose an algorithm that computes the distance between rectangles. – Jan B. Jul 29 '16 at 11:40
  • The [Point2D.distance method](https://docs.oracle.com/javase/8/docs/api/java/awt/geom/Point2D.html#distance-double-double-double-double-) already does this. – VGR Jul 31 '16 at 16:11