问题描述
我使用Javascript,我知道3点的位置。我希望用这些来找出一个圆圈的中心点。
我找到了这个逻辑(不是选择的答案,而是11个upvotes的答案):
但我似乎无法得到我的头围绕着如何编写它的逻辑。
我不能使用边界框,这必须使用以下三点来完成:)
有什么想法?
我最喜欢的分辨率:
将这三点转换为原点(减去(X0,Y0)
)。 / p>
通过两点和原点的圆的方程可以写成
2X.Xc + 2Y.Yc =X²+Y²
在这两点上,你可以通过两个未知数得到一个两个方程组的简单系统,由Cramer
Xc =(Z1.Y2 - Z2.Y1)/ D
Yc =(X1.Z2 - X2.Z1)/ D
D = 2(X1.Y2 - X2.Y1),Z1 =X1²+Y1² ,Z2 =X2²+Y2²
将被翻译回来(添加(X0, Y0)
)。
当三点对齐时,公式失败由 D = 0
(或与分子相比较小)。
X1- = X0; Y1- = Y0; X2- = X0; Y2- = Y0;
double Z1 = X1 * X1 + Y1 * Y1;
double Z2 = X2 * X2 + Y2 * Y2;
double D = 2 *(X1 * Y2-X2 * Y1);
double Xc =(Z1 * Y2 - Z2 * Y1)/ D + X0;
double Yc =(X1 * Z2-X2 * Z1)/ D + Y0;
I am using Javascript and I know the positions of 3 points. I wish to use these to find out the center point of a circle.
I have found this logic (Not the chosen answer but the one with 11 upvotes) : https://math.stackexchange.com/questions/213658/get-the-equation-of-a-circle-when-given-3-points
But I can't seem to get my head around how to write the logic for it.
I can't use bounding box by the way, this has to be done using the three points :)
Any ideas ?
My favorite resolution:
Translate the three points to bring one of them at the origin (subtract (X0,Y0)
).
The equation of a circle through two points and the origin can be written
2X.Xc + 2Y.Yc = X² + Y²
Plugging the coordinates of the two points, you get an easy system of two equations in two unknowns, and by Cramer
Xc = (Z1.Y2 - Z2.Y1) / D
Yc = (X1.Z2 - X2.Z1) / D
D = 2(X1.Y2 - X2.Y1), Z1 = X1²+Y1², Z2 = X2²+Y2²
to be translated back (add (X0,Y0)
).
The formula fails when the three points are aligned, which is diagnosed by D = 0
(or small in comparison to the numerators).
X1-= X0; Y1-= Y0; X2-= X0; Y2-= Y0;
double Z1= X1 * X1 + Y1 * Y1;
double Z2= X2 * X2 + Y2 * Y2;
double D= 2 * (X1 * Y2 - X2 * Y1);
double Xc= (Z1 * Y2 - Z2 * Y1) / D + X0;
double Yc= (X1 * Z2 - X2 * Z1) / D + Y0;
这篇关于如何计算给定三点的圆的中心点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!