博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python turtle 手撸RRT算法
阅读量:3914 次
发布时间:2019-05-23

本文共 6525 字,大约阅读时间需要 21 分钟。

Python RRT源代码

# -*- coding: utf-8 -*-"""Spyder EditorThis is a temporary script file."""import randomimport mathimport turtle as tt.speed(10000)#initial the map    500*500 map +-250t.pu()t.goto(-250,250)t.pd()t.fd(500)for i in range(3):    t.right(90)    t.fd(500)#initial the obstacle  x1     -250
<-100 25
<75t.begin_fill()t.color("black")t.pu()t.goto(-250,75)t.pd()t.right(90)t.fd(150)t.right(90)t.fd(50)t.right(90)t.fd(150)t.right(90)t.fd(50)t.end_fill()#initial the obstacle x2 -150
<-50 -250
<-50t.begin_fill()t.color("black")t.pu()t.goto(-150,-50)t.pd()t.right(90)t.fd(100)t.right(90)t.fd(200)t.right(90)t.fd(100)t.right(90)t.fd(200)t.end_fill()#initial the obstacle x3 0
<250 0
<50t.begin_fill() t.color("black")t.pu()t.goto(0,50)t.pd()t.right(90)t.fd(250)t.right(90)t.fd(50)t.right(90)t.fd(250)t.right(90)t.fd(50)t.end_fill()#initial the start and the goalstart_x = -220start_y = 220goal_x = -220goal_y = -220t.pu()t.goto(start_x,start_y)t.pd()t.circle(2)t.pu()t.goto(goal_x,goal_y)t.pd()t.circle(3)tree=[]foot_length=10tree.append((start_x,start_y))def nearest(new_x,new_y): i=0 Min=math.sqrt((tree[0][0]-new_x)**2+(tree[0][1]-new_y)**2) for j in range(len(tree)): distance=math.sqrt((tree[j][0]-new_x)**2+(tree[j][1]-new_y)**2) if tree[j][0]==new_x and tree[j][1]==new_y: break if distance<=Min and distance!=0: Min=distance i=j return tree[i]px=start_xpy=start_ynew_x=start_xnew_y=start_ywhile math.sqrt((new_x-goal_x)**2+(new_y-goal_y)**2)>=10: if random.random()<0.2: random_x=goal_x random_y=goal_y else: num1=random.random() num2=random.random() random_x=500 * (-0.5 + num1) random_y=500 * (-0.5 + num2) node_x,node_y = nearest(random_x,random_y) new_x=node_x+(random_x-node_x)/foot_length new_y=node_y+(random_y-node_y)/foot_length if 1: if new_x>-250 and new_x<-100 and new_y<75 and new_y>25: flag_1=1 else: flag_1=0 if new_x>-150 and new_x<-50 and new_y<-50 and new_y>-250: flag_11=1 else: flag_11=0 if new_x>0 and new_x<250 and new_y<50 and new_y>0: flag_111=1 else: flag_111=0 if flag_1==0 and flag_11==0 and flag_111==0: tree.append((new_x,new_y)) t.pu() t.goto(node_x,node_y) t.pd() t.goto(new_x,new_y) t.circle(1)final_path = []node_x,node_y = goal_x,goal_ywhile True: node_x,node_y=nearest(node_x,node_y) t.begin_fill() t.goto(node_x,node_y) final_path.append((node_x,node_y)) t.color("red") t.end_fill() if node_x == start_x and node_y == start_y: break;print(final_path)

最终输出结果如下图

在这里插入图片描述

RRT connect 算法源代码

# -*- coding: utf-8 -*-"""Created on Sun Jan 19 20:27:02 2020@author: 1918358"""# -*- coding: utf-8 -*-"""Spyder EditorThis is a temporary script file."""import randomimport mathimport turtle as timport datetimestarttime = datetime.datetime.now()t.speed(10000)#initial the map    500*500 map +-250t.pu()t.goto(-250,250)t.pd()t.fd(500)for i in range(3):    t.right(90)    t.fd(500)#initial the obstacle  x1     -250
<-100 25
<75t.begin_fill()t.color("black")t.pu()t.goto(-250,75)t.pd()t.right(90)t.fd(150)t.right(90)t.fd(50)t.right(90)t.fd(150)t.right(90)t.fd(50)t.end_fill()#initial the obstacle x2 -150
<-50 -250
<-50t.begin_fill()t.color("black")t.pu()t.goto(-150,-50)t.pd()t.right(90)t.fd(100)t.right(90)t.fd(200)t.right(90)t.fd(100)t.right(90)t.fd(200)t.end_fill()#initial the obstacle x3 0
<250 0
<50t.begin_fill() t.color("black")t.pu()t.goto(0,50)t.pd()t.right(90)t.fd(250)t.right(90)t.fd(50)t.right(90)t.fd(250)t.right(90)t.fd(50)t.end_fill()#initial the start and the goalstart_x = -220start_y = 220goal_x = 220goal_y = -220t.pu()t.goto(start_x,start_y)t.pd()t.circle(2)t.pu()t.goto(goal_x,goal_y)t.pd()t.circle(3)tree1=[]tree2=[]foot_length=15tree1.append((start_x,start_y))tree2.append((goal_x,goal_y))def nearest(new_x,new_y,tree): i=0 Min=math.sqrt((tree[0][0]-new_x)**2+(tree[0][1]-new_y)**2) for j in range(len(tree)): distance=math.sqrt((tree[j][0]-new_x)**2+(tree[j][1]-new_y)**2) if tree[j][0]==new_x and tree[j][1]==new_y: break if distance<=Min and distance!=0: Min=distance i=j return tree[i]px=start_xpy=start_ynew_x=start_xnew_y=start_ynew2_x = goal_xnew2_y = goal_yTree = []x2,y2 = nearest(new_x,new_y,tree2)x1,y1 = nearest(new2_x,new2_y,tree1)while math.sqrt((new_x-x2)**2+(new_y-y2)**2)>=25 or math.sqrt((new2_x-x1)**2+(new2_y-y1)**2)>=25: if random.random()<0.1: random_x=new2_x random_y=new2_y else: num1=random.random() num2=random.random() random_x=500 * (-0.5 + num1) random_y=500 * (-0.5 + num2) node_x,node_y = nearest(random_x,random_y,tree1) new_x=node_x+(random_x-node_x)/foot_length new_y=node_y+(random_y-node_y)/foot_length if 1: if new_x>-250 and new_x<-100 and new_y<75 and new_y>25: flag_1=1 else: flag_1=0 if new_x>-150 and new_x<-50 and new_y<-50 and new_y>-250: flag_11=1 else: flag_11=0 if new_x>0 and new_x<250 and new_y<50 and new_y>0: flag_111=1 else: flag_111=0 if flag_1==0 and flag_11==0 and flag_111==0: tree1.append((new_x,new_y)) t.pu() t.goto(node_x,node_y) t.pd() t.goto(new_x,new_y) t.circle(1) if random.random()<0.1: random2_x=new_x random2_y=new_y else: num11=random.random() num22=random.random() random2_x=500 * (-0.5 + num11) random2_y=500 * (-0.5 + num22) node2_x,node2_y = nearest(random2_x,random2_y,tree2) new2_x=node2_x+(random2_x-node2_x)/foot_length new2_y=node2_y+(random2_y-node2_y)/foot_length if 1: if new2_x>-250 and new2_x<-100 and new2_y<75 and new2_y>25: flag_1=1 else: flag_1=0 if new2_x>-150 and new2_x<-50 and new2_y<-50 and new2_y>-250: flag_11=1 else: flag_11=0 if new2_x>0 and new2_x<250 and new2_y<50 and new2_y>0: flag_111=1 else: flag_111=0 if flag_1==0 and flag_11==0 and flag_111==0: tree2.append((new2_x,new2_y)) t.pu() t.goto(node2_x,node2_y) t.pd() t.goto(new2_x,new2_y) t.circle(1) x2,y2 = nearest(new_x,new_y,tree2) x1,y1 = nearest(new2_x,new2_y,tree1) #tree2 = tree2[::-1]Tree = tree1 + tree2node_x,node_y = new2_x,new2_ynode2_x,node2_y = new_x,new_ywhile True: node_x,node_y=nearest(node_x,node_y,tree1) t.begin_fill() t.goto(node_x,node_y) t.color("red") t.end_fill() if node_x == start_x and node_y == start_y: breakt.pu()t.goto(new_x,new_y)t.pd()t.begin_fill()t.goto(new2_x,new2_y)t.color("red")t.end_fill()while True: node2_x,node2_y=nearest(node2_x,node2_y,tree2) t.begin_fill() t.goto(node2_x,node2_y) t.color("red") t.end_fill() if node2_x == goal_x and node2_y == goal_y: break endtime = datetime.datetime.now()print (endtime - starttime).seconds

结果如下:

在这里插入图片描述
在这里插入图片描述

这个阶段的学习存档,请大家互相批评指正,共同学习!

转载地址:http://maprn.baihongyu.com/

你可能感兴趣的文章
.NET Core 中生成验证码
查看>>
.NET Core 中导入导出Excel
查看>>
初识ABP vNext(8):ABP特征管理
查看>>
WPF 消息框 TextBox 绑定新数据时让光标和滚动条跳到最下面
查看>>
【BCVP】实现基于 Redis 的消息队列
查看>>
网络安全逐渐成为程序员的必备技能
查看>>
在Docker中配置ASP.NETCore的HTTPS模式
查看>>
统信发布UOS V20 进军个人市场 生态日益完善
查看>>
【追加功能】OFFICE插件管理工具重整后再上路,更好用易用。
查看>>
Confluent官博:Kafka最牛队列,性能15倍于RabbitMQ!
查看>>
使用SWAGGER和ASP.NET CORE设置可选路由参数
查看>>
C#刷剑指Offer | 二叉搜索树的后序遍历序列
查看>>
新版 C# 高效率编程指南
查看>>
跟我一起学.NetCore之文件系统应用及核心浅析
查看>>
初识ABP vNext(11):聚合根、仓储、领域服务、应用服务、Blob储存
查看>>
chrome禁止三方cookie,网站登录不了怎么办
查看>>
Git 图形化操作之合并提交记录
查看>>
Istio Pilot 源码分析(二)
查看>>
BeetleX框架详解-小结
查看>>
打造钉钉事件分发平台之钉钉审批等事件处理
查看>>