3

If I have two System.Drawing.Rectangle objects on a canvas and a Point, what is the best way to calculate which Rectangle (any part of the Rectangle, not just its Location Point) is closest to that Point?

An example from a unit test:

Rectangle one = new Rectangle (0, 0, 10, 10);

Rectangle two = new Rectangle (20, 20, 10, 10);

Point point = new Point(14, 14);

Rectangle actual = ClosestToPoint(point, one, two);

// should be closer to one since one's bottom right is at (10, 10)
Assert.That(actual, Is.SameAs(one));

// method to write
public Rectangle ClosestToPoint(Point p, params Rectangle[] rectangles) { } 
Michael Hedgpeth
  • 7,732
  • 10
  • 47
  • 66
  • 1
    Use a piece of paper and a pencil to sort this out. There are a limited number of scenarios. It is otherwise an ambiguous question, you'll need to specify what happens when a point is *inside* one or more rectangles. – Hans Passant Nov 22 '10 at 22:20
  • Some answers could also be found here: http://stackoverflow.com/questions/5254838/calculating-distance-between-a-point-and-a-rectangular-box-nearest-point/ – Mo0gles Apr 22 '14 at 13:21

3 Answers3

4

distance to rectangle = min (distance to each of the 4 line segments that are the edges of the rectangle)

For distance to line segment, see this question

Community
  • 1
  • 1
mbeckish
  • 10,485
  • 5
  • 30
  • 55
  • Unless I'm missing something, this yields the wrong answer if the point is *inside* the rectangle. – CAFxX Mar 06 '13 at 21:48
  • @CAFxX - Depends on how you want to define "distance to rectangle". If you are thinking of the rectangles as solids, then you would probably want to define the distance as 0 when the point is inside the rectangle. However, I'm sure there are cases when you'd want to keep distance defined as distance to the closest edge, whether or not the point is inside the rectangle. – mbeckish Mar 06 '13 at 21:56
0

This is for measuring distance between 2 points, so take the coordinate point from your rectangle (which is up to you to decide be cause I dont know what closest means for you):

   public int Distance2D(int x1, int y1, int x2, int y2)
   {

    int result = 0;
    double part1 = Math.Pow((x2 - x1), 2);

    double part2 = Math.Pow((y2 - y1), 2);
    double underRadical = part1 + part2;
    result = (int)Math.Sqrt(underRadical);

     return result;
   }
dexter
  • 7,063
  • 9
  • 54
  • 71
  • The real question is "which points i have to choose", not "how to calculate distance" – Ahmet Kakıcı Nov 22 '10 at 22:23
  • I suppose then the question becomes how do I find which point in a rectangle is closest to a given point. Then, as you say, it's just a matter of finding the closest distance among a single point and the closest points to all rectangles. – Michael Hedgpeth Nov 22 '10 at 22:24
0

Hmm. I'm thinking of looping through your rectangle array and storing each X and Y index in a list. You could then loops through the list and do something abs(min(i.getX() - point)). If they are equal then check for min(y).

08Hawkeye
  • 246
  • 2
  • 15