给定三点如何计算抛物线的顶点 [英] How to calculate the vertex of a parabola given three points

查看:561
本文介绍了给定三点如何计算抛物线的顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个X / Y点构成一个抛物线。我只需要计算穿过这三个点的抛物线的顶点。最好是一种快速的方法,因为我必须进行大量这些计算!

I have three X/Y points that form a parabola. I simply need to calculate what the vertex of the parabola is that goes through these three points. Preferably a quick way as I have to do a LOT of these calculations!

问科学家网站提供了此答案


抛物线的一般形式由等式给出:A * x ^ 2 + B * x + C = y其中,A,B和C是任意实常数。您有三对点,它们是(x,y)有序对。将每个点的x和y值代入抛物线方程。您将在三个未知数(三个常数)中获得三个LINEAR方程。然后,您可以轻松地将这个由三个方程组成的系统求解为A,B和C的值,并且您将得到与3个点相交的抛物线方程。顶点是一阶导数为0的位置,一点代数给出:(-B / 2A,C-B ^ 2 / 4A)表示顶点。

The general form of a parabola is given by the equation: A * x^2 + B * x + C = y where A, B, and C are arbitrary Real constants. You have three pairs of points that are (x,y) ordered pairs. Substitute the x and y values of each point into the equation for a parabola. You will get three LINEAR equations in three unknowns, the three constants. You can then easily solve this system of three equations for the values of A, B, and C, and you'll have the equation of the parabola that intersects your 3 points. The vertex is where the first derivative is 0, a little algebra gives: ( -B/2A , C - B^2/4A ) for the vertex.

很高兴看到用C#或C ++进行此计算的实际代码。有人吗?

It would be nice to see actual code that does this calculation in C# or C++. Anybody?

推荐答案

这实际上只是一个简单的线性代数问题,因此您可以象征性地进行计算。当用三个点的x和y值替代时,您将在三个未知数中得到三个线性方程。

This is really just a simple linear algebra problem, so you can do the calculation symbolically. When you substitute in the x and y values of your three points, you'll get three linear equations in three unknowns.

A x1^2 + B x1 + C = y1
A x2^2 + B x2 + C = y2
A x3^2 + B x3 + C = y3

解决此问题的直接方法是将矩阵求逆

The straightforward way to solve this is to invert the matrix

x1^2  x1  1
x2^2  x2  1
x3^2  x3  1

并乘以向量

y1
y2
y3

这样的结果是……好吧,不完全是这么简单;-)我在Mathematica中做到了,这是伪代码中的公式:

The result of this is... okay, not exactly all that simple ;-) I did it in Mathematica, and here are the formulas in pseudocode:

denom = (x1 - x2)(x1 - x3)(x2 - x3)
A = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2)) / denom
B = (x3^2 * (y1 - y2) + x2^2 * (y3 - y1) + x1^2 * (y2 - y3)) / denom
C = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3) / denom

或者,如果您想对数字进行矩阵数学运算,则通常会转向线性代数系统(例如 ATLAS ,尽管我不确定它是否具有C#/ C ++绑定。)

Alternatively, if you wanted to do the matrix math numerically, you'd typically turn to a linear algebra system (like ATLAS, though I'm not sure if it has C#/C++ bindings).

无论如何,一旦您拥有<$ c的值按照这些公式计算的$ c> A , B C 必须将它们插入问题中给出的表达式 -B / 2A C-B ^ 2 / 4A 1

In any case, once you have the values of A, B, and C as calculated by these formulas, you just have to plug them into the expressions given in the question, -B / 2A and C - B^2/4A, to calculate the coordinates of the vertex.1

请注意,如果原始三个点的坐标构成 denom 一个非常大或非常小的数字,直接进行计算可能会受到明显的数值误差的影响。在那种情况下,最好对其进行一些修改,以避免被分母相除,而分母将抵消它们:

Note that if the original three points have coordinates that make denom a very large or very small number, doing the calculation directly might be susceptible to significant numerical error. In that case it might be better to modify it a bit, to avoid dividing by the denominators where they would cancel out anyway:

denom = (x1 - x2)(x1 - x3)(x2 - x3)
a = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2))
b = (x3^2 * (y1 - y2) + x2^2 * (y3 - y1) + x1^2 * (y2 - y3))
c = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3)

,则顶点的坐标为 -b / 2a (c-b ^ 2 / 4a)/单位。像 A 很大或非常小,或者 C 这样的技巧还可以使其他各种情况受益。 code>几乎等于 B ^ 2 / 4A ,因此它们之间的差异很小,但我认为这些情况相差很大,因此最好进行完整的讨论

and then the coordinates of the vertex are -b / 2a and (c - b^2 / 4a) / denom. There are various other situations that might benefit from "tricks" like this, such as if A is very large or very small, or if C is nearly equal to B^2 / 4A so that their difference is very small, but I think those situations vary enough that a full discussion would be better left for case-by-case followup questions.

将所有这些转换为您选择的语言的代码是读者的练习。 (在使用标准中缀运算符语法的任何语言中,这应该都是微不足道的,例如 C#中显示的AZDean 。)

Converting all of this to code in the language of your choice is left as an exercise for the reader. (It should be pretty trivial in any language that uses standard infix operator syntax, e.g. as AZDean showed in C#.)

1 在答案的初始版本中,我认为这很明显,但似乎有很多人喜欢明确提到它。

1In the initial version of the answer I thought this would be obvious, but it seems there are a lot of people who like having it mentioned explicitly.

这篇关于给定三点如何计算抛物线的顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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