最终绘制效果如图
坐标文本如下
data.txt
300,0,144,1,0,0
300,0,144,0,1,0
300,0,144,0,0,1
300,0,144,1,1,0
300,0,108,0,1,1
184,0, 72,1,0,1
184,0, 72,1,0,0
184,0, 72,1,0,0
184,0, 72,1,0,0
程序代码如下
300,0,144,1,0,0
300,0,144,0,1,0
300,0,144,0,0,1
300,0,144,1,1,0
300,0,108,0,1,1
184,0, 72,1,0,1
184,0, 72,1,0,0
184,0, 72,1,0,0
184,0, 72,1,0,0
程序代码如下
#根据数据文件在窗口中动态路径绘制
import turtle
def main():
#设置窗口信息
turtle.title('数据驱动的动态路径绘制')
turtle.setup(800, 600, 0, 0)
#设置画笔
pen = turtle.Turtle()
pen.color("red")
pen.width(5)
pen.shape("turtle")
pen.speed(5)
#读取文件
result=[]
file = open("data.txt","r")
for line in file:
result.append(list(map(float, line.split(',')))) # map函数 map(function, iterable, ...)
print(result) # 对可迭代函数'iterable'中的每一个元素应用‘function’方法,将结果作为list返回。
#动态绘制
for i in range(len(result)):
pen.color((result[i][3],result[i][4],result[i][5]))
pen.forward(result[i][0])
if result[i][1]:
pen.rt(result[i][2])
else:
pen.lt(result[i][2])
pen.goto(0,0)
if __name__ == '__main__':
main()
map函数 https://my.oschina.net/zyzzy/blog/115096
split函数的两种用法:
#1 先read打开的文件再使用split分割
f=open("data.txt","r")
file=f.read()
para=file.split(" ")
print(para[3])
#2 用for...in 形式关联line和打开的文件file,再用split分割
file=open("data.txt","r")
for line in file:
para=line.split(" ")
print(para[2])