Skip to content

Instantly share code, notes, and snippets.

@pratikac
Last active August 29, 2015 14:21
Show Gist options
  • Save pratikac/e3fbd4e12f9bac865d13 to your computer and use it in GitHub Desktop.
Save pratikac/e3fbd4e12f9bac865d13 to your computer and use it in GitHub Desktop.
Steve Lavalle's RRT code (stripped off pygame)
import sys, random, math
from math import sqrt,cos,sin,atan2
#constants
xdim = 640
ydim = 480
winsize = [xdim, ydim]
epsilon = 10.0
num_nodes = 1000
goal = (300, 400)
def dist(p1,p2):
return sqrt((p1[0]-p2[0])*(p1[0]-p2[0])+(p1[1]-p2[1])*(p1[1]-p2[1]))
def step_from_to(p1,p2):
if dist(p1,p2) < epsilon:
return p2
else:
theta = atan2(p2[1]-p1[1],p2[0]-p1[0])
return p1[0] + epsilon*cos(theta), p1[1] + epsilon*sin(theta)
def main():
nodes = []
n0 = ((xdim/2.0,ydim/2.0), 0)
nodes.append(n0) # Start in the center
goal_dist = 1000
i = 0
while goal_dist > epsilon:
rand = random.random()*640.0, random.random()*480.0
nn = nodes[0][0]
cc = nodes[0][1]
for p,c in nodes:
if dist(p,rand) < dist(nn,rand):
nn = p
cc = c
new_x = step_from_to(nn,rand)
new_c = epsilon + cc
nodes.append((new_x, new_c))
goal_dist = dist(new_x, goal)
i = i+1
print i, " ", (new_x, new_c)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment