Python TurtleGraphics - Smoothing random walks?

I need help with this question related to TurtleGraphics in Python:

A small detail of tipy_turtle () is that when the turtle turns 90 degrees, it immediately "jumps" in a new direction. This makes his movement appear jagged. It might look better if the turtle moved smoothly when turning. So for this question, write a smooth_tipsy_turtle () function that is the same as tipy_turtle (), except using turtle.right (d), write a completely new smooth_right (d) function that works like this:

 - If d is negative then
      - repeat the following -d times:
            - turn left 1 using the ordinary turtle.left command

  - Otherwise, repeat the following d times:
          - turn right 1 using the ordinary turtle.right command

      

Here is my original function to get the random movement of the turtle:

def tipsy_turtle(num_steps):
    turtle.reset()
    for step in range(num_steps):
       rand_num = random.randint(-1, 1)
       turtle.right(rand_num * 90)
       turtle.forward(5 * random.randint(1, 3))

      

So how am I going to do this job? I tried to add:

   if rand_num*90 < 0:
       for step in range(rand_num*90):
           turtle.left(rand_num*90)
   else:
       turtle.right(rand_num*90)

      

But it didn't work, and I don't know what I did wrong. Thank!

+2


source to share


3 answers


Hopefully this sample will clarify what went wrong in your example - you performed either rand_num*90*rand_num*90

left turns or rand_num*90

right turns!

if rand_num < 0: # don't need to multiply by 90 here - it either +ve or -ve.
    for step in xrange(90): # xrange is preferred over range in situations like this
         turtle.left(rand_num) # net result is 90 left turns in rand_num direction
else:
    for step in xrange(90):
         turtle.right(rand_num)

      

Or you could write it like:



for step in xrange(90):
    if rand_num < 0:
        turtle.left(rand_num)
    else:
        turtle.right(rand_num)

      

For code like this, it's really a matter of preference.

+2


source


You may be able to do without the conditional for left-vs-right. I don't have python syntax, so here's the pseudocode

turtle left randomly generated value 0 to 90
turtle right randomly generated value 0 to 90
turtle forward some amount

      



Ie, generate a random angle and turn left that much, then generate another random angle and turn right that much. This way you don't have to worry about generating or dealing with negative randomness. You can keep all the random angles positive, and the combination of left followed by right effectively does the subtraction for you, which gives a nice Gaussian distribution for direction changes.

0


source


I guess I'll dare to answer, although I'm not entirely sure what you want (see my comment on the question, and don't be surprised if I edit this answer as needed!).

Assuming you want the turtle to rotate a certain number of degrees at each step, not necessarily 90, but not more than 90, just use rand_num = random.randint(-90, 90)

and then turtle.right(rand_num)

.

0


source







All Articles