椭圆路径和点之间的距离?

时间:2009-10-14 16:08:19

标签: silverlight math

所以,我在绘图表面上使用椭圆,我需要知道从椭圆路径(线条粗细的中心很好)到给定点的最短距离。

如果需要,我可以使用原始数学运算,因为我知道椭圆的主轴和次轴。据我所知,这将是相当复杂的。

我想知道我的观点是否可以为我计算?

我正在使用EllipseGeometry并设置轴。然后将EllipseGeometry传递给路径(Path.Data)并绘制它。

有什么想法知道路径的最短距离是什么?

5 个答案:

答案 0 :(得分:1)

只需关闭此循环:

我找到了一些C ++代码,用数学做了这个,然后翻译成C#。我不知道它是如何工作的,但确实如此。

最终,当鼠标靠近它时,我正想要突出一个椭圆。我也能用不同的方法完成这个任务(但是坚持使用纯数学方法):

创建第二条路径,其具有与我显示的路径相同的几何和平移,但具有更厚的StrokeThickness和0.1的不透明度。在更大,不透明的路径上做一些测试。

答案 1 :(得分:1)

我读了上面提到的文章。我有数学背景来实现它,但没有耐心。如果你打算使用牛顿方法逼近某些东西,为什么还要去做衍生物,代数以及所有这些呢?

这是我的想法:

1) Assume ellipse is (x/a)^2 + (y/b)^2 = 1
   That is, the origin is at zero and the rotation angle is zero. 
(Transform your ellipse and point of interest P1 by rotation and translation if necessary before beginning.)
2) Convert the ellipse into parametric form.
       x = a cos t  
       y = b sin t
3) Divide the parametric range of "t" into N parts, from 0 to 2*PI.
N = 16 seems like a good number (22.5 degrees).
4) Iterate through the N points along the ellipse, stepping t by 2*PI/N each time.
5) Compute the point P2 on the ellipse.
6) Compute the distance from P1 to P2.
7) Keep track of the closest distance dmin found so far, and the value of the parameter as tmin.
8) Start a second loop, but shrink the range for t to being  (tmin - 2*PI/N, tmin + 2*PI/N).
9) Repeat the search, dividing this smaller range by N again.
10) Add new loops as necessary until the distance between successive points is less than the tolerance that matters to you.

将椭圆近似为具有主半径的圆,因为C = 2 * PI * R,当N = 2 * PI * R时,被测试的连续点将相隔一个像素。

每个循环使用16个步骤,要比较的点数为16 * log8(PI * R)。

作为进一步的改进,我会将该点反映到第一个坐标(正x& y)。这可以节省3/4椭圆的计算量。

上述算法可以实现更简单的代码:更易于编写和调试。效率是另一回事。对于许多应用程序,它应该足够好。

答案 2 :(得分:0)

如果你要做很多关于几何,测量等的事情,我强烈推荐Farseer Physics Engine for Silverlight。我在益智游戏中使用它,它可以很好地为你管理所有数学。

我只是在物理模拟器中创建我需要的形状,然后在物理模拟器中使用它们的位置在画布上渲染它们。

答案 3 :(得分:0)

对保罗切尔诺克的方法(​​事后很久,对于那些可能发生的人)的快速谴责 - 不要陷入虚假的精确感。每次将一个部分分成更多的子部分时,都会继承“父”部分的错误。换句话说,精确的结果永远不会比你在迭代时开始的tmin和tmax值更准确。

如果您需要非常准确的结果,请务必在第一个循环中以较大的N值开始。

答案 4 :(得分:-1)

计算同一平面中一个外点和一个椭圆之间的距离非常简单。需要最少的代数。没有微积分。 同一平面中两个椭圆之间的最小距离稍微复杂一点,但没有那么难。它可以使用一个点和一个椭圆之间的最小距离的简单触发器迭代过程来完成。

相关问题