数控技术课使用计算机语言编程进行直线插补模拟,分三步:1.仅第一象限直线插补 2.任意象限直线插补 3.任意象限插补及turtle画图
本人python使用时间较短,代码较繁琐,希望对您有帮助!!!
1.仅第一象限直线插补
termination_x,termination_y=input("请输入终点坐标:").split(",")
list1=[int(termination_x),int(termination_y)]
initial=[0,0]
n=list1[0]+list1[1]
termination_x=list1[0]
termination_y=list1[1]
F=0
for i in range(n):
if F>=0:
initial[0]+=1
F=F-termination_y
print("方向:x正向",end=" ")
print("偏差计算 F-x={}-{}={}".\
format(F+termination_y,abs(termination_y),F),end=" ")
else:
initial[1]+=1
F=F+termination_x
print("方向:y正向",end=" ")
print("偏差计算 F-y={}-{}={}".\
format(F+termination_x,abs(termination_x),F),end=" ")
print("坐标 {}".format(initial))
2.任意象限直线插补
import turtle
#判断象限
def quadrant(a,b):
quadrant=0
if list1[0]*list1[1]>=0:
if list1[0]>=0:
quadrant=1
else:
quadrant=3
else:
if list1[0]>=0:
quadrant=4
else:
quadrant=2
return quadrant
#补偿1
def offsetting1(origin,F,termination_y):
origin[0]+=1
F=F-abs(termination_y)
print("方向:x正向",end=" ")
print("偏差计算 F-x={}-{}={}".\
format(F+termination_y,abs(termination_y),F),end=" ")
return F
#补偿2
def offsetting2(origin,F,termination_y):
origin[0]-=1
F=F-abs(termination_y)
print("方向:x负向",end=" ")
print("偏差计算 F-x={}-{}={}".\
format(F+termination_y,abs(termination_y),F),end=" ")
return F
#补偿3
def offsetting3(origin,F,termination_x):
origin[1]+=1
F=F+abs(termination_x)
print("方向:y正向",end=" ")
print("偏差计算 F+y={}+{}={}".\
format(F+termination_x,abs(termination_x),F),end=" ")
return F
#补偿4
def offsetting4(origin,F,termination_x):
origin[1]-=1
F=F+abs(termination_x)
print("方向:y负向",end=" ")
print("偏差计算 F+y={}+{}={}".\
format(F+termination_x,abs(termination_x),F),end=" ")
return F
#主函数
termination_x,termination_y=input("请输入终点坐标:").split(",")
list1=[int(termination_x),int(termination_y)]
origin=[0,0]
n=abs(list1[0])+abs(list1[1])
termination_x=list1[0]
termination_y=list1[1]
F=0
quadrant=quadrant(list1[0],list1[1])
print("第{}象限".format(quadrant))
#coordinate(termination_x,termination_y)
for i in range(n):
if quadrant==1:
if F>=0:
F=offsetting1(origin,F,termination_y)
else:
F=offsetting3(origin,F,termination_x)
print("坐标 {}".format(origin))
elif quadrant==2:
if F>=0:
F=offsetting2(origin,F,termination_y)
else:
F=offsetting3(origin,F,termination_x)
print("坐标 {}".format(origin))
elif quadrant==3:
if F>=0:
F=offsetting2(origin,F,termination_y)
else:
F=offsetting4(origin,F,termination_x)
print("坐标 {}".format(origin))
elif quadrant==4:
if F>=0:
F=offsetting1(origin,F,termination_y)
else:
F=offsetting4(origin,F,termination_x)
print("坐标 {}".format(origin))
3.任意象限插补及turtle画图
import turtle as t
#判断象限
def quadrant(a,b):
quadrant=0
if list1[0]*list1[1]>=0:
if list1[0]>=0:
quadrant=1
else:
quadrant=3
else:
if list1[0]>=0:
quadrant=4
else:
quadrant=2
return quadrant
#补偿1
def offsetting1(origin,F,termination_y,coordinate_x,coordinate_y):
origin[0]+=1
F=F-abs(termination_y)
print("方向:x正向",end=" ")
print("偏差计算 F-x={}-{}={}".\
format(F+termination_y,abs(termination_y),F),end=" ")
coordinate_x=coordinate_x+10
t.goto(coordinate_x,coordinate_y)
return F,coordinate_x
#补偿2
def offsetting2(origin,F,termination_y,coordinate_x,coordinate_y):
origin[0]-=1
F=F-abs(termination_y)
print("方向:x负向",end=" ")
print("偏差计算 F-x={}-{}={}".\
format(F+termination_y,abs(termination_y),F),end=" ")
coordinate_x=coordinate_x-10
t.goto(coordinate_x,coordinate_y)
return F,coordinate_x
#补偿3
def offsetting3(origin,F,termination_x,coordinate_x,coordinate_y):
origin[1]+=1
F=F+abs(termination_x)
print("方向:y正向",end=" ")
print("偏差计算 F+y={}+{}={}".\
format(F+termination_x,abs(termination_x),F),end=" ")
coordinate_y=coordinate_y+10
t.goto(coordinate_x,coordinate_y)
return F,coordinate_y
#补偿4
def offsetting4(origin,F,termination_x,coordinate_x,coordinate_y):
origin[1]-=1
F=F+abs(termination_x)
print("方向:y负向",end=" ")
print("偏差计算 F+y={}+{}={}".\
format(F+termination_x,abs(termination_x),F),end=" ")
coordinate_y=coordinate_y-10
t.goto(coordinate_x,coordinate_y)
return F,coordinate_y
#建立坐标系
def coordinate():
t.penup()
t.goto(-300,0)
t.pendown()
t.goto(200,0)
t.penup()
t.goto(-50,-200)
t.pendown()
t.goto(-50,200)
t.penup()
t.goto(-50,0)
t.pendown()
t.pencolor("red")
#主函数
termination_x,termination_y=input("请输入终点坐标:").split(",")
list1=[int(termination_x),int(termination_y)]
origin=[0,0]
n=abs(list1[0])+abs(list1[1])
termination_x=list1[0]
termination_y=list1[1]
F=0
quadrant=quadrant(list1[0],list1[1])
print("第{}象限".format(quadrant))
coordinate()
coordinate_x=-50
coordinate_y=0
for i in range(n):
if quadrant==1:
if F>=0:
F,coordinate_x=offsetting1\
(origin,F,termination_y,coordinate_x,coordinate_y)
else:
F,coordinate_y=offsetting3\
(origin,F,termination_x,coordinate_x,coordinate_y)
print("坐标 {}".format(origin))
elif quadrant==2:
if F>=0:
F,coordinate_x=offsetting2\
(origin,F,termination_y,coordinate_x,coordinate_y)
else:
F,coordinate_y=offsetting3\
(origin,F,termination_x,coordinate_x,coordinate_y)
print("坐标 {}".format(origin))
elif quadrant==3:
if F>=0:
F,coordinate_x=offsetting2\
(origin,F,termination_y,coordinate_x,coordinate_y)
else:
F,coordinate_y=offsetting4\
(origin,F,termination_x,coordinate_x,coordinate_y)
print("坐标 {}".format(origin))
elif quadrant==4:
if F>=0:
F,coordinate_x=offsetting1\
(origin,F,termination_y,coordinate_x,coordinate_y)
else:
F,coordinate_y=offsetting4\
(origin,F,termination_x,coordinate_x,coordinate_y)
print("坐标 {}".format(origin))
t.pencolor("black")
t.goto(-50,0)
t.done()
最后任意象限加图结果如下: