rya*_*sey 6 c geometry arduino g-code
我正在编写一个GCode解释器,当给定(X,Y)圆圈和半径上的2个点时,我很难确定圆心.
给定中心点时,我可以从2个点绘制一个圆,但如果给出了半径值,我无法将其转换为中心点.
我查找了多个以不同形式的数学(微积分,几何,三角等)编写的示例,但不能将它们中的任何一个转换为代码.
我在这里找到了同样的问题,在Visual Basic中只有1个真正的答案,但看起来粘贴的代码部分依赖于未包含的其他代码.
我的理解是给出的值产生2个不同的中心/交叉点.这些是我需要弄清楚的.
解释器在Arduino上运行并用C语言编写.如果有人能用伪代码引导我完成它,我将非常感激.
谢谢!
给出圆的方程和中点的方程:
q = sqrt((x2-x1)^2 + (y2-y1)^2)
y3 = (y1+y2)/2
x3 = (x1+x2)/2
Run Code Online (Sandbox Code Playgroud)
一个答案是:
x = x3 + sqrt(r^2-(q/2)^2)*(y1-y2)/q
y = y3 + sqrt(r^2-(q/2)^2)*(x2-x1)/q
Run Code Online (Sandbox Code Playgroud)
另一个是:
x = x3 - sqrt(r^2-(q/2)^2)*(y1-y2)/q
y = y3 - sqrt(r^2-(q/2)^2)*(x2-x1)/q
Run Code Online (Sandbox Code Playgroud)
假设已经声明了点的变量,您的代码应如下所示:
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
Run Code Online (Sandbox Code Playgroud)
来源:http://mathforum.org/library/drmath/view/53027.html
在 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);
}
Run Code Online (Sandbox Code Playgroud)