-1

I have 4 points, each of them indicates the middle of sides of rectangle.The 5th(main) point can be placed anywhere. How can I find the nearest of those 4 points to the main point, what is code implementation?

4

1 Answer 1

9

Calculate the difference between the x- and y- coordinates of each point relative to yours. Then use pythagorus theorum to determine the actual distance from your point.

3
  • I was just typing this. You beat me to it.
    – Yuri
    Nov 24, 2011 at 15:17
  • Sorry but I cant understand how should I use pythagorus theorum to determine the actual distance from my point, would you please write more detailed answer
    – Martin
    Nov 24, 2011 at 16:21
  • If your point has coordinates (x,y) and another point is at (p,q) then the vertical distance between them is y-q (i.e y minus q) and the horizontal distance is x-p. These lengths are the shorter two sides of a right-angled triangle (draw it on a piece of paper - it's more obvious), so the hypoteneuse is the distance between the two points. In other words... sqrt( (x-p)^2 + (y-q)^2 ) Nov 24, 2011 at 20:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.