c# - 如何在代码中找到点和抛物线之间的距离

标签 c# c++ math hlsl calculus

我正在尝试为 DirectX 像素着色器找到抛物线上与 2d 中任意点的最近点。

通过大量谷歌搜索,我发现这是一个常见的预微积分作业问题。不幸的是,数百个相关答案都说“一旦你有了这个等式,使用你的图形计算器的最小值函数,它会告诉你答案是 6。”

我承认我完全不记得预微积分。我知道我寻找的方程式可能就在维基百科上,但我不知道如何将这些希腊符号转换为 HLSL 函数。 C、C++、C# 或任何其他语言的解决方案也将不胜感激。

编辑:根据请求查看输入曲线的格式:

//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);
} 

最佳答案

你可以利用这个:

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

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

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

我想你已经知道如何计算了。然后将 P'(x) 与零进行比较,找出它与 X 轴的交点。你从中找到一个 xmin,然后你从中找到 ymin:

y = a*x^2 + bx + c

就是这样。

关于c# - 如何在代码中找到点和抛物线之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9800324/

相关文章:

algorithm - 在近似树皮刻度上获取等距间隔

c - Google Calculator Glitch,float vs. double 可能是一个可能的原因吗?

c# - 查找字典中奇数/偶数的数量

c# - DateTime 的 Razor DisplayFor = 输入字符串的格式不正确

c# - 调用 Dynamic Links Analytics API 时调用者没有权限 [403]

c++ - X509_get0_notBefore 和 X509_get0_noAfter 在此范围内?

c# - C# 中的嵌套实例

c++ - 使用继承时未获得文件的正确输出

C++ : multiple namespaces inside a function

c++ - 如何在 C++ 中使用字母进行简单的数学操作数?