从三点求圆心的算法是什么?
我在圆的圆周上有三个点:
pt A = (A.x, A.y);
pt B = (B.x, B.y);
pt C = (C.x, C.y);
如何计算圆心?
在Processing (Java) 中实现它。
我找到了答案并实施了一个可行的解决方案:
pt circleCenter(pt A, pt B, pt C) {
float yDelta_a = B.y - A.y;
float xDelta_a = B.x - A.x;
float yDelta_b = C.y - B.y;
float xDelta_b = C.x - B.x;
pt center = P(0,0);
float aSlope = yDelta_a/xDelta_a;
float bSlope = yDelta_b/xDelta_b;
center.x = (aSlope*bSlope*(A.y - C.y) + bSlope*(A.x + B.x)
- aSlope*(B.x+C.x) )/(2* (bSlope-aSlope) );
center.y = -1*(center.x - (A.x+B.x)/2)/aSlope + (A.y+B.y)/2;
return center;
}
I have three points on the circumference of a circle:
pt A = (A.x, A.y);
pt B = (B.x, B.y);
pt C = (C.x, C.y);
How do I calculate the center of the circle?
Implementing it in Processing (Java).
I found the answer and implemented a working solution:
pt circleCenter(pt A, pt B, pt C) {
float yDelta_a = B.y - A.y;
float xDelta_a = B.x - A.x;
float yDelta_b = C.y - B.y;
float xDelta_b = C.x - B.x;
pt center = P(0,0);
float aSlope = yDelta_a/xDelta_a;
float bSlope = yDelta_b/xDelta_b;
center.x = (aSlope*bSlope*(A.y - C.y) + bSlope*(A.x + B.x)
- aSlope*(B.x+C.x) )/(2* (bSlope-aSlope) );
center.y = -1*(center.x - (A.x+B.x)/2)/aSlope + (A.y+B.y)/2;
return center;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是我的 Java 端口,当行列式消失时,用一个非常优雅的
IllegalArgumentException
来避免错误条件,这是我处理“两个点相距很远”或“点位于一条线上”条件的方法。此外,这会计算半径(并应对特殊条件),而您的相交斜坡方法将无法做到这一点。请参阅数学论坛的算法:
Here's my Java port, dodging the error condition when the determinant disappears with a very elegant
IllegalArgumentException
, my approach to coping with the "points are two far apart" or "points lie on a line" conditions. Also, this computes the radius (and copes with exceptional conditions) which your intersecting-slopes approach will not do.See algorithm from The Math Forum:
这可能是一个相当深入的计算。这里有一个简单的分步步骤: http://paulbourke.net/geometry/circlesphere/。一旦你有了圆的方程,你可以简单地把它写成包含 H 和 K 的形式。点 (h,k) 将是圆心。
(在链接处向下滚动一点即可找到方程式)
It can be a rather in depth calculation. There is a simple step-by-step here: http://paulbourke.net/geometry/circlesphere/. Once you have the equation of the circle, you can simply put it in a form involving H and K. The point (h,k) will be the center.
(scroll down a little ways at the link to get to the equations)
当我将鼠标悬停在这个问题上时,我正在寻找类似的算法。
获取了您的代码,但发现这在斜率为 0 或无穷大的情况下不起作用(当 xDelta_a 或 xDelta_b 为 0 时可能为真)。
我更正了算法,这是我的代码。
注意:我使用objective-c编程语言,只是更改点值初始化的代码,所以如果这是错误的,我相信使用java工作的程序员可以纠正它。然而,所有的逻辑都是相同的(上帝保佑算法!!:))
就我自己的功能测试而言,工作得很好。
如果逻辑有任何错误,请告诉我。
I was looking for a similar algorithm when I hovered over this question.
Took your code but found that this will not work in cases when where either of the slope is 0 or infinity (can be true when xDelta_a or xDelta_b is 0).
I corrected the algorithm and here is my code.
Note: I used objective-c programming language and am just changing the code for point value initialization, so if that is wrong, I am sure programmer working in java can correct it. The logic, however, is the same for all (God bless algorithms!! :))
Works perfectly fine as far as my own functional testing is concerned.
Please let me know if logic is wrong at any point.
很抱歉我的回答迟了。当两个点形成一条垂直线时,任何使用“斜率”的解决方案都会失败,因为斜率将是无限的。
这是 2019 年的一个简单稳健的解决方案,始终正常工作:
当且仅当 3 个点共线时,上述代码才会返回“false”。
I am sorry my answer is late. Any solution using "slope" will fail when two of the points form a vertical line, because the slope will be infinite.
Here is a simple robust solution for 2019 that always works correctly:
The above code will return "false" if and only if the 3 points are collinear.
了解Circumcircle
特殊情况Apollonius 问题:类型 1:三点
求外心和半径:
示例(结果:x、y、半径):
Read about Circumcircle
Special cases of Apollonius' problem: Type 1: Three points
Finding circumcenter and radius:
Examples (result: x, y, radius):