如何计算弧上的y位置?当我有半径时,弧线起点和终点

时间:2016-03-24 21:14:13

标签: geometry automatic-ref-counting cnc

我试图在CNC上编写一个程序。基本上我有圆弧起始x,y,半径和精加工x,y我也知道顺时针或cc的方向。所以我需要在特定的x位置找出弧的y值。最好的方法是什么? 我在这个网站上发现了类似的问题here。但我不知道如何获得角度。

sample

2 个答案:

答案 0 :(得分:0)

圆的等式为x^2 + y^2 = r^2

在您的情况下,我们知道x_randomR

替换in知道我们得到了,

x_random ^ 2 + y_random ^ 2 = R ^ 2

并解决y_random get get

y_random = sqrt( R ^ 2 - x_random ^ 2 )

现在我们有y_random

编辑:仅当弧是圆弧而不是椭圆弧时才会起作用

要将此答案调整为椭圆,您需要使用此等式,而不是圆的等式

( x ^ 2 / a ^ 2 ) + ( y ^ 2 / b ^ 2 ) = 1,其中ax axis的半径,by axis的半径

从名为data.txt的文件中读取数据并计算一系列y_random值并将其写入名为out.txt

的文件的简单脚本
import math                                                                 

def fromFile():                                                             
    fileIn = open('data.txt', 'r')                                          
    output = ''                                                             
    for line in fileIn:                                                     
        data = line.split()                                                 
        # line of data should be in the following format                    
        # x h k r                                                           
        x = float(data[0])                                                  
        h = float(data[1])                                                  
        k = float(data[2])                                                  
        r = float(data[3])                                                  
        y = math.sqrt(r**2 - (x-h)**2)+k                                    
        if ('\n' in line):                                                  
            output += line[:-1] + ' | y = ' + str(y) + '\n'                 
        else:                                                               
            output += line + ' | y = ' + str(y)                             
    print(output)                                                           
    fileOut = open('out.txt', 'w')                                          
    fileOut.write(output)                                                   
    fileIn.close()                                                          
    fileOut.close()                                                         

if __name__ == '__main__':                                                  
    fromFile()

data.txt应格式化为

x0 h0 k0 r0
x1 h1 k1 r1
x2 h2 k2 r2
... for as many lines as required

答案 1 :(得分:0)

首先你必须找到圆方程。让我们开始点Pst = (xs,ys),结束点Pend = (xend,yend)

为简单起见,将所有坐标移动(-xs, -ys),因此起点变为坐标原点。

新的Pend' = (xend-xs,yend-ys) = (xe, ye),新的“随机点”坐标为xr' = xrandom - xs,未知的圆心为(xc, yc)

xc^2 + yc^2 = R^2    {1}
(xc - xe)^2 + (yc-ye)^2 = R^2  {2}  //open the brackets
xc^2 - 2*xc*xe + xe^2 + yc^2 - 2*yc*ye + ye^2 = R^2    {2'}
subtract {2'} from {1}
2*xc*xe - xe^2  + 2*yc*ye - ye^2 = 0    {3}
yc =  (xe^2 + ye^2 - 2*xc*xe) / (2*ye) {4}
substitute {4} in {1}
xc^2 + (xe^2 + ye^2 - 2*xc*xe)^2 / (4*ye^2) = R^2 {5}
solve quadratic equation {5} for xc, choose right root (corresponding to arc direction), find yc

having center coordinates (xc, yc), write
yr' = yc +- Sqrt(R^2 -(xc-xr')^2) //choose right sign if root exists
and finally exclude coordinate shift
yrandom = yr' + ys