CNCでプログラムを書こうとしています。基本的に、 x, y 、半径から始まる円弧があり、 x, y で終了します。また、円弧の方向を時計回りまたは cc も知っています。したがって、特定の x 位置での円弧の y の値を見つける必要があります。それを行う最善の方法は何ですか?このウェブサイトhere で同様の問題を見つけました。しかし、角度aを取得する方法がわかりません。
2 に答える
まず、円の方程式を見つける必要があります。始点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
円の方程式はx^2 + y^2 = r^2
あなたの場合、私たちは知ってx_random
おり、R
得られた知識に代入すると、
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
ここで、a
は に沿った半径x axis
、b
は に沿った半径です。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