找到一个给定的点上最接近圆一个点的最佳方法 [英] Best way to find a point on a circle closest to a given point
问题描述
给定一个点(PX,PY),并与已知中心(CX,CY)和半径(R)一圈,什么是code,你可以拿出最短找上了点圈最接近(PX,PY)?
Given a point (pX, pY) and a circle with a known center (cX,cY) and radius (r), what is the shortest amount of code you can come up with to find the point on the circle closest to (pX, pY) ?
我有一些code类的工作,但它涉及的圆圈转化为形式的等式(X - CX)^ 2 +(Y - CY)^ 2 = R ^ 2(r为半径),并使用从点线(PX,PY)至(CX,CY)的方程以创建一个二次方程来解决。
I've got some code kind of working but it involves converting the circle to an equation of the form (x - cX)^2 + (y - cY)^2 = r^2 (where r is radius) and using the equation of the line from point (pX, pY) to (cX, cY) to create a quadratic equation to be solved.
在我烫出的错误,它会做什么,但似乎这样一个不雅的解决方案。
Once I iron out the bugs it'll do, but it seems such an inelegant solution.
推荐答案
其中,P是点,C为中心,R为半径,在合适的mathy语言:
where P is the point, C is the center, and R is the radius, in a suitable "mathy" language:
V = (P - C); Answer = C + V / |V| * R;
其中| V |为V的长度。
where |V| is length of V.
OK,OK
double vX = pX - cX;
double vY = pY - cY;
double magV = sqrt(vX*vX + vY*vY);
double aX = cX + vX / magV * R;
double aY = cY + vY / magV * R;
易于扩展到> 2的尺寸。
easy to extend to >2 dimensions.
这篇关于找到一个给定的点上最接近圆一个点的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!