如何找到的点和在代码抛物线之间的距离 [英] How to find the distance between a point and a parabola in code

查看:202
本文介绍了如何找到的点和在代码抛物线之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个抛物线的最近点在2D的任意点,为的DirectX像素着色器。



谷歌搜索量很大透露给我,这是一种常见的演算预作业的问题。不幸的是,数以百计的相关解答所有这样说:一旦你有这样的公式,用你的图形计算器的最低功能,它会告诉你答案是6。



我承认,我记得没有预先演算。我承认,我所寻求的等式可能是坐在那里维基百科,但我无法弄清楚如何将这些希腊字母转换为HLSL的功能。 C中的一个解决方案,C ++,C#或任何其他语言也将不胜感激。



编辑:每到看到输入曲线的格式要求:

 <$ C抛物线为Y = AX ^ 2 + BX + C 
//的p $ C> //方程是我们试图找到抛物线的最近点的任意点。
FLOAT2 GetClosestPointOnParabola(浮动,浮动B,浮C,FLOAT2 P)
{
//东西涉及到距离公式...
//有涉及最小化。 ..
返回FLOAT2(X,Y);
}


解决方案

您可以利用这个

  Pmin的=(XMIN,YMIN)〜点上抛物线
p =(PX,PY)〜点通过= A * X ^ 2 + BX + C〜抛物线

P(X)=(X-PX)^ 2 +(Y-PY)^ 2 =(X-2D $ b $ PX)^ 2 +(A * X ^ 2 + BX + C - PY)^ 2

您需要计算P(x)的导数,这并不难。例如。
如果你得到: P(x)= X ^ 4 + 4X ^ 2 - 3倍+ 10 导数是:



  P'(x)= 4X ^ 3 + 8X  -  3 

我想你如何计算。然后比较P'(x)至零,以找到它crossess X轴。你会发现从一个XMIN,然后你从YMIN:

  Y = A * X ^ 2 + BX + C 

这就是它。


I am trying to find the closest point on a parabola to an arbitrary point in 2d, for a DirectX pixel shader.

A great amount of googling has revealed to me that this is a common pre-calculus homework problem. Unfortunately, the hundreds of relevant answers all say things like "Once you have this equation, use your graphing calculator's minimum function and it will tell you the answer is 6."

I confess that I recall nothing of pre-calculus. I recognize that the equation I seek is probably sitting right there on wikipedia, but I can't figure out how to convert these greek symbols into an HLSL function. A solution in C, C++, C#, or any other language would also be greatly appreciated.

edit: Per a request to see the format of the input curve:

//Equation of parabola being y = ax^2 + bx + c
//p is the arbitrary point we're trying to find the closest point on the parabola for.
float2 GetClosestPointOnParabola(float a, float b, float c, float2 p)
{
    //Something involving the distance formula...
    //Something involving "minimization"...
    return float2(x, y);
} 

解决方案

You could make use of this:

Pmin = (xmin, ymin) ~ point on a parabola
P = (px, py) ~ point in 2d    
y = a*x^2 + bx + c ~ parabola

P(x) = (x-px)^2 + (y-py)^2 = (x-px)^2 + (a*x^2 + bx + c - py)^2

You need to calculate the P(x) derivative, it's not that difficult. E.g. If you get: P(x) = x^4 + 4x^2 - 3x + 10 the derivative would be:

P'(x) = 4x^3 + 8x - 3

I think you get how to calculate that. Then compare P'(x) to zero to find where it crossess an X-axis. You find an xmin from that and then you have ymin from:

y = a*x^2 + bx + c

That's it.

这篇关于如何找到的点和在代码抛物线之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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