我正在写一个G-Code解释器,当给定(X,Y)圆上的两个点和半径时,我很难确定圆心。给定中心点时,我可以从2个点绘制一个圆,但如果给出半径值,我就不能将其用于中心点。
我查找了多个以不同形式的数学(微积分,几何,三角等)编写的示例,但不能将它们中的任何一个转换为代码。我的理解是给出的值产生两个不同的中心/交叉点。这些是我需要弄清楚的。
解释器在Arduino上运行并用C语言编写。如果有人能用伪代码引导我完成它,我将非常感激。
谢谢!
答案 0 :(得分:5)
给出圆的方程和中点的方程:
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
答案 1 :(得分:4)
在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);
}
答案 2 :(得分: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
答案 3 :(得分:0)