给定两个点和半径,寻找圆的中心

13
我正在编写一个G代码解释器,当给出圆上两个点的(X,Y)和半径时,我无法确定圆心。如果已知圆的半径,可以从两个点绘制圆,但我不能用它来确定圆心点。您需要找到两个不同的圆心/相交点。您可以尝试使用几何学公式进行计算,然后将其转换为适用于C语言的伪代码实现。

5
以一定半径穿过两点的圆有两个解。 - user2249683
请查看这个回答,但是不要只使用sqrt,改用Math.Sqrt。 - Kateract
4个回答

13

给定一个圆的方程和中点的方程:

q = sqrt((x2-x1)^2 + (y2-y1)^2)

y3 = (y1+y2)/2

x3 = (x1+x2)/2

一个答案将会是:

x = x3 + sqrt(r^2-(q/2)^2)*(y1-y2)/q 

y = y3 + sqrt(r^2-(q/2)^2)*(x2-x1)/q  

另一个将是:

x = x3 - sqrt(r^2-(q/2)^2)*(y1-y2)/q

y = y3 - sqrt(r^2-(q/2)^2)*(x2-x1)/q  

假设点的变量已经被声明,你的代码应该长成这样:

double q = Math.Sqrt(Math.Pow((x2-x1),2) + Math.Pow((y2-y1),2));

double y3 = (y1+y2)/2;

double x3 = (x1+x2)/2;

double basex = Math.Sqrt(Math.Pow(r,2)-Math.Pow((q/2),2))*(y1-y2)/q; //calculate once
double basey = Math.Sqrt(Math.Pow(r,2)-Math.Pow((q/2),2))*(x2-x1)/q; //calculate once

double centerx1 = x3 + basex; //center x of circle 1
double centery1 = y3 + basey; //center y of circle 1
double centerx2 = x3 - basex; //center x of circle 2
double centery2 = y3 - basey; //center y of circle 2
源自:链接

我会在几个小时内提供 C# 版本。 - Chris Hawley
谢谢你的回复,Chris。我可能非常局限于线性思维方式。当X3和Y3在等式的右侧时,我很难解决它们。 - ryan lindsey
我尝试将所有内容放入方程简化器(Wolfram)中,只是为了看看它是否能够更好地呈现。像我所做的那样,拆分方程可能是最好的选择。我将在我的答案中提供C#版本的方程式。 - Chris Hawley
太棒了。我完全理解了!谢谢! - ryan lindsey

6
这将返回一个中心点。你需要为另一个点进行调整。
在c#中:
 private double CenterX(double x1,double y1, double x2, double y2,double radius)
    {
        double radsq = radius * radius;
        double q = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
        double x3 = (x1 + x2) / 2;


     return x3 + Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((y1 - y2) / q);


    }

    private double CenterY(double x1, double y1, double x2, double y2, double radius)
    {
    double radsq = radius * radius;
    double q = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));

     double y3 = (y1 + y2) / 2;

      return y3 + Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((x2-x1) / q);


    }

4

当给定两个点和一个半径时,并不总是能够找到唯一的圆心。事实上,有三种不同的情况:

        

案例1:

                                  
当给定的直径小于给定点之间的距离时,就会出现这种情况。在这种情况下,没有解决方案。

情况2:

                                  
当给定的直径恰好等于两点之间的距离时,会发生什么。在这种情况下,有一个微不足道的解法。
                                  

案例三:

当给定的直径大于两点之间的距离时,会出现此情况。在这种情况下,方程式中有两个解:

                                  

您可以在此页面中找到解决方案。

                               

其中q是两点之间的距离,[x3, y3]是中点。

   


这个Gist中,我正在尝试用C语言实现这些内容,但还没有完成。随意从我离开的地方继续。


0

这是同样代码的 Ruby 版本,如果有人需要的话(感谢 rookie1024 提供的 C# 代码)

def chord
  @chord ||= begin
    a =  (point_1.x.to_f - point_2.x.to_f).abs ** 2
    b =  (point_1.y.to_f - point_2.y.to_f).abs ** 2
    Math.sqrt(a + b)
  end
end

def radius
  @radius ||= begin
    s = (chord / 2) * bulge
    ((chord/2) ** 2 + (s ** 2))/(2*s)
  end.to_f
end

def center
  x1 = point_1.x
  y1 = point_1.y

  x2 = point_2.x
  y2 = point_2.y

  x3 = (x1+x2)/2
  y3 = (y1+y2)/2

  basex = Math.sqrt((radius ** 2) - ((chord/2) ** 2)) * (y1-y2)/chord

  basey = Math.sqrt((radius ** 2) - ((chord/2) ** 2)) * (x2-x1)/chord

  centerx1 = x3 + basex
  centery1 = y3 + basey
  centerx2 = x3 - basex
  centery2 = y3 - basey

  bulge > 0 ? [centerx1, centery1] : [centerx2, centery2]
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接