0

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

样本

4

2 回答 2

0

圆的方程是x^2 + y^2 = r^2

在您的情况下,我们知道x_random并且R

代入知道我们得到,

x_random ^ 2 + y_random ^ 2 = R ^ 2

并解决y_randomget get

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

现在我们有y_random

编辑:这仅在您的弧线是圆弧而不是椭圆弧时才有效

要将这个答案调整为椭圆,您需要使用这个方程,而不是圆的方程

( x ^ 2 / a ^ 2 ) + ( y ^ 2 / b ^ 2 ) = 1, 其中a是沿 的半径 是沿x axisb半径y 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
于 2016-03-24T21:25:39.417 回答
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
于 2016-03-25T06:58:31.203 回答