Author Topic: Math Question  (Read 431 times)

0 Members and 1 Guest are viewing this topic.

Offline Lunerfox

  • Major
  • *
  • Posts: 69
  • Lol in the Box.
Math Question
« on: December 31, 2007, 03:34:27 pm »
Okay, so here is what I'm having trouble figuring out:



Given the points (x1, y1) and (x2,y2), as well as the radius R, how do you figure out what the closest point to the rectangle is?
-[ILOA]-
---
Project Survival: An independent game development project.
http://forums.soldat.pl/index.php?topic=23390.0

Offline sai`ke

  • Camper
  • ***
  • Posts: 318
  • Can't be arsed to remove christmas avatar
Re: Math Question
« Reply #1 on: December 31, 2007, 04:16:15 pm »
By drawing it like that you've already given yourself the right answer.

The line from the circle to the rectangle is given by [ x2 - x1, y2 - y1 ]
The direction is then this quantity normalized to unity so [x2-x1, y2-y1] / ( (x2-x1)^2 + (y2-y1)^2 ). In other words a vector in the right direction with length 1.
Since we have to stay on the circle we have to go R into the direction of the rectangle so

[ x1 + R * ( (x2-x1) / ( (x2-x1)^2 + (y2-y1)^2 ) ), y1 + R * ( (y2-y1) / ( (x2-x1)^2 + (y2-y1)^2 ) ) ]

You can probably simplify the equation but I have to be off now xD. This assumes that the rectangle is always perpendicular to the line mentioned before (the way you've drawn it). If it's not, you will need a vector defining it's angle and it's width as well.
« Last Edit: December 31, 2007, 04:21:40 pm by sai`ke »
#soldat.ttw #ttw.gather --- Quakenet!
http://ttwforums.com

Offline Lunerfox

  • Major
  • *
  • Posts: 69
  • Lol in the Box.
Re: Math Question
« Reply #2 on: December 31, 2007, 04:27:11 pm »
ahh, so you're simply converting the Vector drawn on the picture into a unit vector, then multiplying it by R. Got it. The rectangle should always be pointed perpendicular to the circle, since that would always give the point closest to the circle. Lemme try and see if that works out.

Thanks for your help.
-[ILOA]-
---
Project Survival: An independent game development project.
http://forums.soldat.pl/index.php?topic=23390.0