Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function h2xy(h)
- if h == 0 then
- return 0,1
- elseif h == 1 then
- return 1,0
- elseif h == 2 then
- return 0,-1
- elseif h == 3 then
- return -1,0
- end
- return 0,0
- end
- function from_to_heading (ch,th)
- if(ch==th) then
- return 0
- end
- if(math.abs(ch-th)==2) then
- turtle.turnLeft()
- turtle.turnLeft()
- elseif (ch-th == -1 or ch-th == 3) then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- end
- function move_towards (cx,cy,cheading,tx,ty )
- moved = false
- if cx != tx then
- if (cx>tx) then
- from_to_heading(cheading,3)
- cheading = 3
- moved = turtle.forward()
- elseif (cx<tx) then
- from_to_heading(cheading,1)
- cheading = 1
- moved = turtle.forward()
- end
- elseif (cy != ty) then
- if(cy>ty) then
- from_to_heading(cheading,2)
- cheading = 2
- moved = turtle.forward()
- elseif (cy<ty) then
- from_to_heading(cheading,0)
- cheading = 0
- moved = turtle.forward()
- end
- end
- dx,dy = h2xy(cheading)
- if(moved) then
- cx = cx+dx
- cy = cy+dy
- end
- return moved,cheading,cx,cy
- end
- function move_from_to(cx,cy,cheading,tx,ty)
- while cx!= tx or cy != ty do
- moved,nheading, nx,ny = move_towards(cx,cy,cheading,tx,ty)
- cx = nx
- cy = ny
- cheading = nheading
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement