在最接近给定点的圆上找到点的最佳方法 [英] Best way to find a point on a circle closest to a given point

查看:19
本文介绍了在最接近给定点的圆上找到点的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个点 (pX, pY) 和一个圆心 (cX,cY) 和半径 (r)(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) ?

我有一些代码可以工作,但它涉及将圆转换为形式为 (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 是半径,用合适的数学"语言:

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.

好的,好的

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆