最近做CNC插补大作业的时候,写的一段小程序,能够实现跨象限、起点(或中心)不在原点的直线、圆弧、椭圆的插补。希望与各位大佬探讨学习!
import turtle
from time import*
point = []
turtle.penup();turtle.fd(-320);turtle.pendown()
turtle.write(" 插补大作业\n\n姓名:***\t学号:**********\n\n5秒钟后在对话框中输入插补曲线类型...",font=("Arial",25,"normal"))
sleep(5)
turtle.reset()
def draw_coordinate(): #画坐标轴(直线、圆弧、椭圆类型)
turtle.penup();turtle.goto(0,300);turtle.pendown();turtle.goto(0,-300);turtle.penup();turtle.goto(300,0)
turtle.pendown();turtle.goto(-300, 0);turtle.penup();turtle.goto(0, 0)
def judge_quadrantl(x0, y0, x1, y1): #判断起始点所在象限(直线类型)
x = x1 - x0;y = y1 - y0#坐标变换
if(x > 0) and (y > 0): #第一象限
quadrant = 1
elif(x < 0) and (y > 0): #第二象限
quadrant = 2
elif(x < 0) and (y < 0): #第三象限
quadrant = 3
elif(x > 0) and (y < 0): #第四象限
quadrant = 4
elif x > 0 and y == 0:
quadrant = 5 #X轴正方向
elif x < 0 and y == 0:
quadrant = 6 #X轴负方向
elif x == 0 and y > 0:
quadrant = 7 #Y轴正方向
elif x == 0 and y < 0:
quadrant = 8 #Y轴负方向
return quadrant
def judge_quadrantcv(x0,y0,x1,y1,ox,oy): #将圆心(或椭圆中心)平移到原点后,判断起始点、终点所在象限(圆弧、椭圆类型)
x_0=x0-ox;y_0=y0-oy;x_1=x1-ox;y_1=y1-oy #坐标变换
if(x_0 > 0) and (y_0 >= 0): # 第一象限
if (x_1 > 0) and (y_1 >= 0):
quadrant1=1
quadrant2=1
elif (x_1 <= 0) and (y_1 > 0):
quadrant1=1
quadrant2=2
elif (x_1 < 0) and (y_1 <= 0):
quadrant1=1
quadrant2=3
elif(x_1 >= 0) and (y_1 < 0):
quadrant1=1
quadrant2=4
elif(x_0 <= 0) and (y_0 > 0): # 第二象限
if (x_1 > 0) and (y_1 >= 0):
quadrant1=2
quadrant2=1
elif (x_1 <= 0) and (y_1 > 0):
quadrant1=2
quadrant2=2
elif (x_1 < 0) and (y_1 <= 0):
quadrant1=2
quadrant2=3
elif(x_1 >= 0) and (y_1 < 0):
quadrant1=2
quadrant2=4
elif(x_0 < 0) and (y_0 <= 0): # 第三象限
if (x_1 > 0) and (y_1 >= 0):
quadrant1=3
quadrant2=1
elif (x_1 <= 0) and (y_1 > 0):
quadrant1=3
quadrant2=2
elif (x_1 < 0) and (y_1 <= 0):
quadrant1=3
quadrant2=3
elif(x_1 >= 0) and (y_1 < 0):
quadrant1=3
quadrant2=4
elif(x_0 >= 0) and (y_0 < 0): # 第四象限
if (x_1 > 0) and (y_1 >= 0):
quadrant1=4
quadrant2=1
elif (x_1 <= 0) and (y_1 > 0):
quadrant1=4
quadrant2=2
elif (x_1 < 0) and (y_1 <= 0):
quadrant1=4
quadrant2=3
elif(x_1 >= 0) and (y_1 < 0):
quadrant1=4
quadrant2=4
return quadrant1,quadrant2
def interpolationl(quadrant, x0, y0, x1, y1): #获取由插补点组成的数组(直线类型)
fm = 0;x2 = abs(x1 - x0);y2 = abs(y1 - y0);total = x2 + y2;count = 0
while count < total:
if quadrant < 5:
if fm >= 0:
if quadrant == 1 or quadrant == 4:
x0 += 1
elif quadrant == 2 or quadrant == 3:
x0 -= 1
fm = fm - y2
elif fm <0:
if quadrant == 1 or quadrant == 2:
y0 += 1
elif quadrant == 3 or quadrant == 4:
y0 -= 1
fm = fm + x2
else:
if quadrant == 5:
x0 += 1
elif quadrant == 6:
x0 -=1
elif quadrant == 7:
y0 += 1
elif quadrant == 8:
y0 -=1
p = (2*x0,2*y0) #此处为了便于观察插补过程,坐标扩大了2倍,读者可自行修调,下同,不再赘述!
point.append(p)
count += 1
def quadrantcv_1(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode): #获取由第一象限插补点组成的数组(圆弧、椭圆类型)
while totalsteps >= 0:
if direction == 2: #2 逆时针圆(椭圆)
if fm >= 0:
if mode==1:
fm=fm-2*(x0)+1
x0-=1
elif mode==2:
x0-=1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
else:
if mode==1:
fm=fm +2*(y0)+1
y0+=1
elif mode==2:
y0+=1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
else: #1 顺时针圆(椭圆)
if fm >= 0:
if mode==1:
fm = fm -2*(y0) +1
y0 -= 1
elif mode==2:
y0 -= 1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
else:
if mode==1:
fm = fm +2*(x0) +1
x0 += 1
elif mode==2:
x0 += 1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
point.append((2*x0+2*ox, 2*y0+2*oy))
totalsteps=totalsteps-1
if (x0<0) or (y0<0): #超出第一象限,则第一象限函数终止,转至下一象限,下同,不再赘述!
break
return x0,y0,totalsteps,fm
def quadrantcv_2(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode): #获取由第二象限插补点组成的数组(圆弧、椭圆类型)
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
while totalsteps >= 0:
if direction == 2: #2 逆时针圆(椭圆)
if fm >= 0:
if mode==1:
fm = fm -2*(y0) +1
y0 -= 1
elif mode==2:
y0 -= 1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
else:
if mode==1:
fm = fm -2*(x0) +1
x0 -= 1
elif mode==2:
x0-=1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
else: #1 顺时针圆(椭圆)
if fm >= 0:
if mode==1:
fm = fm +2*(x0) +1
x0 += 1
elif mode==2:
x0+=1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
else:
if mode==1:
fm = fm +2*(y0) +1
y0 += 1
elif mode==2:
y0+=1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
point.append((2*x0+2*ox, 2*y0+2*oy))
totalsteps=totalsteps-1
if (x0>0) or(y0<0):
break
return x0,y0,totalsteps,fm
def quadrantcv_3(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode): #获取由第三象限插补点组成的数组(圆弧、椭圆类型)
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
while totalsteps >= 0:
if direction == 2: #2 逆时针圆
if fm >= 0:
if mode==1:
fm = fm +2*(x0) +1
x0 += 1
elif mode==2:
x0+=1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
else:
if mode==1:
fm = fm -2*(y0) +1
y0 -= 1
elif mode==2:
y0-=1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
else: #1 顺时针圆
if fm >= 0:
if mode==1:
fm = fm +2*(y0) +1
y0 += 1
elif mode==2:
y0+=1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
else:
if mode==1:
fm = fm -2*(x0) +1
x0 -= 1
elif mode==2:
x0-=1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
point.append((2*x0+2*ox, 2*y0+2*oy))
totalsteps=totalsteps-1
if (x0>0) or (y0>0):
break
return x0,y0,totalsteps,fm
def quadrantcv_4(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode): #获取由第四象限插补点组成的数组(圆弧、椭圆类型)
fm = 0
while totalsteps >= 0:
if direction == 2: #2 逆时针圆
if fm >= 0:
if mode==1:
fm = fm +2*(y0) +1
y0 += 1
else:
if mode==1:
fm = fm +2*(x0) +1
x0 += 1
elif mode==2:
x0+=1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
else: #1 顺时针圆
if fm >= 0:
if mode==1:
fm = fm -2*(x0) +1
x0 -= 1
elif mode==2:
x0-=1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
else:
if mode==1:
fm = fm -2*(y0) +1
y0 -= 1
elif mode==2:
y0-=1
fm=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5-l
point.append((2*x0+2*ox, 2*y0+2*oy))
totalsteps=totalsteps-1
if (x0<0) or (y0>0):
break
return x0,y0,totalsteps,fm
def quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i): #将以上获取四个象限插补点数组的函数合为一个,方便后面简化程序。(圆弧、椭圆类型)
if i==1:
x0,y0,totalsteps,fm=quadrantcv_1(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode)
elif i==2:
x0,y0,totalsteps,fm=quadrantcv_2(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode)
elif i==3:
x0,y0,totalsteps,fm=quadrantcv_3(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode)
elif i==4:
x0,y0,totalsteps,fm=quadrantcv_4(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode)
return x0,y0,totalsteps,fm
def interpolationcv(direction,l,quadrant1,quadrant2,x0,y0,x1,y1,ox,oy,j1x,j1y,j2x,j2y,mode): #获取由插补点组成的数组(圆弧、椭圆类型)
r=int(((x0-ox)**2+(y0-oy)**2)**0.5)
a=(((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5)/2 #椭圆长半轴
b=(a**2-(abs(j2x-ox))**2)**0.5 #椭圆短半轴
point.append((2*x0,2*y0)) #此处系数2是为了与上述步骤中坐标放大倍数保持一致。
x0=x0-ox;y0=y0-oy;x1=x1-ox;y1=y1-oy;j1x=j1x-ox;j1y=j1y-oy;j2x=j2x-ox;j2y=j2y-oy;fm=0#坐标变换
if (quadrant1==1) and (quadrant2==1):
if direction==1:#顺时针圆弧
totalsteps=abs(x0-x1)+abs(y0-y1)#总步数totalsteps计算公式,为作者通过分类讨论得出(圆弧:顺时针16类,逆时针16类。椭圆:顺时针16类,逆时针16类。)以下63类不再赘述。
for i in [1,4,3,2,1]:#列表中数字,为从起始点顺时针走到终点所需经过的象限顺序,以下31类不再赘述。
if totalsteps!=0:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else:
break
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(abs(x0-x1)+abs(y0-y1))
elif mode==2:
totalsteps=4*a+4*b-(abs(x0-x1)+abs(y0-y1))
for i in [1,2,3,4,1]:
if totalsteps!=0:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else:
break
elif (quadrant1==1) and (quadrant2==2):
if direction==1: #顺时针圆弧
if mode==1:
totalsteps=6*r-x0+x1+y0+y1
elif mode==2:
totalsteps=4*a+2*b-x0+x1+y0+y1
for i in [1,4,3,2]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(6*r-x0+x1+y0+y1)
elif mode==2:
totalsteps=4*a+4*b-(4*a+2*b-x0+x1+y0+y1)
for i in [1,2]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
elif (quadrant1==1) and (quadrant2==3):
if direction==1: #顺时针圆弧
if mode==1:
totalsteps=4*r-x0-x1+y0+y1
elif mode==2:
totalsteps=2*a+2*b-x0-x1+y0+y1
for i in [1,4,3]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(4*r-x0-x1+y0+y1)
elif mode==2:
totalsteps=4*a+4*b-(2*a+2*b-x0-x1+y0+y1)
for i in [1,2,3]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
elif (quadrant1==1) and (quadrant2==4):
if direction==1: #顺时针圆弧
if mode==1:
totalsteps=2*r-x0-x1+y0-y1
elif mode==2:
totalsteps=2*a-x0-x1+y0-y1
for i in [1,4]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(2*r-x0-x1+y0-y1)
elif mode==2:
totalsteps=4*a+4*b-(2*a-x0-x1+y0-y1)
for i in [1,2,3,4]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
elif (quadrant1==2) and (quadrant2==1):
if direction==1: #顺时针圆弧
if mode==1:
totalsteps=2*r-x0+x1-y0-y1
elif mode==2:
totalsteps=2*b-x0+x1-y0-y1
for i in [2,1]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(2*r-x0+x1-y0-y1)
elif mode==2:
totalsteps=4*a+4*b-(2*b-x0+x1-y0-y1)
for i in [2,3,4,1]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
elif (quadrant1==2) and (quadrant2==2):
if direction==1: #顺时针圆弧
totalsteps=abs(x0-x1)+abs(y0-y1)
for i in [2,1,4,3,2]:
if totalsteps!=0:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else:
break
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(abs(x0-x1)+abs(y0-y1))
elif mode==2:
totalsteps=4*a+4*b-(abs(x0-x1)+abs(y0-y1))
for i in [2,3,4,1,2]:
if totalsteps!=0:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else:
break
elif (quadrant1==2) and (quadrant2==3):
if direction==1: #顺时针圆弧
if mode==1:
totalsteps=6*r-x0-x1-y0+y1
elif mode==2:
totalsteps=2*a+4*b-x0-x1-y0+y1
for i in [2,1,4,3]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(6*r-x0-x1-y0+y1)
elif mode==2:
totalsteps=4*a+4*b-(2*a+4*b-x0-x1-y0+y1)
for i in [2,3]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
elif (quadrant1==2) and (quadrant2==4):
if direction==1: #顺时针圆弧
if mode==1:
totalsteps=4*r-x0-x1-y0-y1
elif mode==2:
totalsteps=2*a+2*b-x0-x1-y0-y1
for i in [2,1,4]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(4*r-x0-x1-y0-y1)
elif mode==2:
totalsteps=4*a+4*b-(2*a+2*b-x0-x1-y0-y1)
for i in [2,3,4]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
elif (quadrant1==3) and (quadrant2==1):
if direction==1: #顺时针圆弧
if mode==1:
totalsteps=4*r+x0+x1-y0-y1
elif mode==2:
totalsteps=2*a+2*b+x0+x1-y0-y1
for i in [3,2,1]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(4*r+x0+x1-y0-y1)
elif mode==2:
totalsteps=4*a+4*b-(2*a+2*b+x0+x1-y0-y1)
for i in [3,4,1]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
elif (quadrant1==3) and (quadrant2==2):
if direction==1: #顺时针圆弧
if mode==1:
totalsteps=2*r+x0+x1-y0+y1
elif mode==2:
totalsteps=2*a+x0+x1-y0+y1
for i in [3,2]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(2*r+x0+x1-y0+y1)
elif mode==2:
totalsteps=4*a+4*b-(2*a+x0+x1-y0+y1)
for i in [3,4,1,2]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
elif (quadrant1==3) and (quadrant2==3):
if direction==1: #顺时针圆弧
totalsteps=abs(x0-x1)+abs(y0-y1)
for i in [3,2,1,4,3]:
if totalsteps!=0:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else:
break
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(abs(x0-x1)+abs(y0-y1))
elif mode==2:
totalsteps=4*a+4*b-(abs(x0-x1)+abs(y0-y1))
for i in [3,4,1,2,3]:
if totalsteps!=0:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else:
break
elif (quadrant1==3) and (quadrant2==4):
if direction==1: #顺时针圆弧
if mode==1:
totalsteps=6*r+x0-x1-y0-y1
elif mode==2:
totalstsps=4*a+2*b+x0-x1-y0-y1
for i in [3,2,1,4]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(6*r+x0-x1-y0-y1)
elif mode==2:
totalsteps=4*a+4*b-(4*a+2*b+x0-x1-y0-y1)
for i in [3,4]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
elif (quadrant1==4) and (quadrant2==1):
if direction==1: #顺时针圆弧
if mode==1:
totalsteps=2*r-x0-x1-y0+y1
elif mode==2:
totalsteps=2*a-x0-x1-y0+y1
for i in [4,3,2,1]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(2*r-x0-x1-y0+y1)
elif mode==2:
totalsteps=4*a+4*b-(2*a-x0-x1-y0+y1)
for i in [4,1]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
elif (quadrant1==4) and (quadrant2==2):
if direction==1: #顺时针圆弧
if mode==1:
totalsteps=4*r+x0+x1+y0+y1
elif mode==2:
totalsteps=2*a+2*b+x0+x1+y0+y1
for i in [4,3,2]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(4*r+x0+x1+y0+y1)
elif mode==2:
totalsteps=4*a+4*b-(2*a+2*b+x0+x1+y0+y1)
for i in [4,1,2]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
elif (quadrant1==4) and (quadrant2==3):
if direction==1: #顺时针圆弧
if mode==1:
totalsteps=2*r+x0-x1+y0+y1
elif mode==2:
totalsteps=2*a+x0-x1+y0+y1
for i in [4,3]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(2*r+x0-x1+y0+y1)
elif mode==2:
totalsteps=4*a+4*b-(2*a+x0-x1+y0+y1)
for i in [4,1,2,3]:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
elif (quadrant1==4) and (quadrant2==4):
if direction==1: #顺时针圆弧
totalsteps=abs(x0-x1)+abs(y0-y1)
for i in [4,3,2,1,4]:
if totalsteps!=0:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else:
break
else: #逆时针圆弧
if mode==1:
totalsteps=8*r-(abs(x0-x1)+abs(y0-y1))
elif mode==2:
totalsteps=4*a+4*b-(abs(x0-x1)+abs(y0-y1))
for i in [4,1,2,3,4]:
if totalsteps!=0:
x0,y0,totalsteps,fm=quadrant_all(direction,totalsteps,l,fm,x0,y0,ox,oy,j1x,j1y,j2x,j2y,mode,i)
else:
break
def get_pointsl(): #从输入端获取起始点、终点的函数(直线类型)
x0,y0,x1,y1=eval(input("请按顺序输入起点横纵坐标与终点横纵坐标,并以英文逗号间隔:"))
quadrant = judge_quadrantl(x0, y0, x1, y1)
interpolationl(quadrant,x0, y0, x1, y1)
def get_pointsc(): #从输入端获取起始点、终点、圆心的函数(圆弧类型)
direction=eval(input("请输入插补方向(1:顺时针,2:逆时针):"))
x0,y0,x1,y1,ox,oy=eval(input("请按顺序输入起点横纵坐标、终点横纵坐标、圆心横纵坐标,并以英文逗号间隔:"))
while (round((x0-ox)**2+(y0-oy)**2,5)!=round((x1-ox)**2+(y1-oy)**2,5)): #判断起点、终点到圆心坐标是否相等,考虑到二进制存储存在的误差,使用round函数截取5位小数进行比较。
print("ERROR, pls input again.\n")
x0,y0,x1,y1,ox,oy=eval(input("请按顺序输入起点横纵坐标、终点横纵坐标、圆心横纵坐标,并以英文逗号间隔:"))
quadrant1,quadrant2=judge_quadrantcv(x0,y0,x1,y1,ox,oy)
l=0;j1x=0;j1y=0;j2x=0;j2y=0;mode=1
interpolationcv(direction,l,quadrant1,quadrant2,x0,y0,x1,y1,ox,oy,j1x,j1y,j2x,j2y,mode) #将插补函数mode设为1,即选择圆弧插补类型,因为圆没有焦点和弦长,故令l=0,j1x=0,j1y=0,j2x=0,j2y=0。
def get_pointsv(): #从输入端获取焦点、弦长的函数(椭圆类型)
direction=eval(input("请输入插补方向(1:顺时针,2:逆时针):"))
j1x,j1y,j2x,j2y,x0,y0,x1,y1=eval(input("请按顺序输入焦点1横纵坐标、焦点2横纵坐标、起点横纵坐标、终点横纵坐标,并以英文逗号间隔:"))
while (j1y!=j2y):
print("ERROR, the long axis of oval isn't horizontal. Pls input again.\n")
j1x,j1y,j2x,j2y,x0,y0,x1,y1=eval(input("请按顺序输入焦点1横纵坐标、焦点2横纵坐标、起点横纵坐标、终点横纵坐标,并以英文逗号间隔:"))
while (round((((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5),5)!=round((((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5),5)):#判断起点、终点到两焦点距离之和是否相等,处理同上。
print("ERROR, pls input again.\n")
j1x,j1y,j2x,j2y,x0,y0,x1,y1=eval(input("请按顺序输入焦点1横纵坐标、焦点2横纵坐标、起点横纵坐标、终点横纵坐标,并以英文逗号间隔:"))
ox=(j1x+j2x)/2;oy=(j1y+j2y)/2 #椭圆中心横、纵坐标
l=((x0-j1x)**2+(y0-j1y)**2)**0.5+((x0-j2x)**2+(y0-j2y)**2)**0.5
quadrant1,quadrant2=judge_quadrantcv(x0,y0,x1,y1,ox,oy)
mode=2 #将插补函数mode设为2,即选择椭圆插补类型。
interpolationcv(direction,l,quadrant1,quadrant2,x0,y0,x1,y1,ox,oy,j1x,j1y,j2x,j2y,mode)
def draw_line(): #遍历插补点组成的数组中的每一个元素,绘制出插补线段(直线、圆弧、椭圆通用)
print(point)
turtle.goto(point[0])
turtle.pendown()
for item in point:
turtle.goto(item)
def line(): #画直线
draw_coordinate()
get_pointsl()
draw_line()
def circle(): #画圆弧
draw_coordinate()
get_pointsc()
draw_line()
def oval(): #画椭圆
draw_coordinate()
get_pointsv()
draw_line()
mode=eval(input("请输入插补曲线类型对应的数字(1:直线类型,2:圆弧类型,3:椭圆类型):"))
if(mode==1): #选择插补曲线类型
line()
elif(mode==2):
circle()
elif(mode==3):
oval()
turtle.exitonclick()