code_junkie

Best way to find a point on a circle closest to a given point

Nov 12th, 2011
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.39 KB | None | 0 0
  1. V = (P - C); Answer = C + V / |V| * R;
  2.  
  3. double vX = pX - cX;
  4. double vY = pY - cY;
  5. double magV = sqrt(vX*vX + vY*vY);
  6. double aX = cX + vX / magV * R;
  7. double aY = cY + vY / magV * R;
  8.  
  9. m=(cY-pY)/(cX-pX); //slope
  10. b=cY-m*cX; //or Py-m*Px. Now you have a line in the form y=m*x+b
  11. X=( (2mcY)*((-2*m*cY)^2-4*(cY^2+cX^2-b^2-2*b*cY-r^2)*(-1-m^2))^(1/2) )/(2*(cY^2+cX^2-b^2-2*bc*Y-r^2));
  12. Y=mX+b;
Add Comment
Please, Sign In to add comment