Get Python turtle to face in direction of line being plotted - python

I'm trying to get the turtle shape to follow the direction of a line.
I have a simple parabola and I want the turtle shape to follow the direction of the line - when the graph goes up, the turtle faces up and when the graph comes down, the turtle faces down.
I am using goto() for the position of the turtle and x=x+1 for the x position on the graph:
t.goto(x,y)
t.right(??) - this?
t.left(??) - this?
t.setheading(??) or this?
What is the best method to achieve this? When I have tried using t.right() in a while loop (I am looping until x is complete), the turtle continues to spin in a circle as it moves, which is not what I want.
Still not getting this. I added the extra code that was suggested - here is the EDIT and the full code for what I am trying to achieve...
I am using the physics formula for trajectory (I used this so I know my values outputted are correct).
http://www.softschools.com/formulas/physics/trajectory_formula/162/
import math
import turtle
import time
w=turtle.Turtle()
i=0
angle=66.4
velocity=45.0
g=9.8
t=math.tan(math.radians(angle))
c=math.cos(math.radians(angle))
turtle.delay(9)
w.shape("turtle")
w.setheading(90)
while i < 150:
start = i * t
middle = g*(i**2)
bottom =(2*(velocity**2)*c**2)
total = start-middle/bottom
print(total)
w.setheading(turtle.towards(i,total))
w.goto(i,total)
i=i+1
turtle.exitonclick()

The orientation of the turtle can be determined from the derivative of your function at the current position.
If you have the function as a sympy function, you can ask Python to do the differentiation. Or you could just do it on your own. If your function is
y = x^2
, then the derivative is
dy = 2 * x
Given that derivative at the current position, its arc tangent gives you the turtle's heading:
t.setheading(math.atan(dy))
Make sure that the angle mode of the turtle is set to radians or convert them to degrees
t.setheading(math.degrees(math.atan(dy)))

I agree with #NicoSchertler that the arc tangent of the derivative is the way to go mathematically. But if it's just for good visuals, there's a simpler way. We can combine turtle's setheading() and towards() methods, constantly setting the turtle's heading towards the next position just before we go there:
from turtle import Screen, Turtle
turtle = Turtle(shape='turtle', visible=False)
turtle.penup()
turtle.goto(-20, -400)
turtle.pendown()
turtle.setheading(90)
turtle.showturtle()
for x in range(-20, 20):
y = -x ** 2
turtle.setheading(turtle.towards(x, y))
turtle.goto(x, y)
screen = Screen()
screen.exitonclick()

Related

How do I create any regular polygon using turtle?

So I have an assignment that asked me to draw any regular polygon using Turtle and I created the code. It works but my mentor said to try again. I would like to know what I did wrong, Thank you!
The requirements for this assignment are:
The program should take in input from the user.
The program should have a function that:
takes in the number of sides as a parameter.
calculates the angle
uses the appropriate angle to draw the polygon
from turtle import Turtle
turtle = Turtle()
side = int(input("Enter the number of the sides: "))
def poly():
for i in range(side):
turtle.forward(100)
turtle.right(360 / side)
poly()
I think this might be better suited on math stackexchange.
A regular polygon has interior angles (n−2) × 180 / n. Theres a good blog post on that here.
You just need to change the angle by which you rotate every time:
from turtle import Turtle
turtle = Turtle()
num_sides = int(input("Enter the number of the sides: "))
def poly():
for i in range(num_sides):
turtle.forward(100)
# change this bit
turtle.right((num_sides - 2) * 180 / num_sides)
poly()
This is the function I used to draw a polygon using Turtle:
Draws an n-sided polygon of a given length. t is a turtle.
def polygon(t, n, length):
angle = 360.0 / n
polyline(t, n, length, angle)

How can I rotate a shape from the bottom in turtle

I am trying to make a turtle analog clock, and to make the second, minute, an hour hands I am making a shape with turtle, and using .tilt() to tilt it 6 degrees every second.
The thing is, when I run .tilt() it rotates the ship from the middle, whereas I want to have it rotate from the bottom point (like an analog clock).
Is there a way to do that, or do I need to find another way to make this program?
Here is my code:
from turtle import *
import time
turtle = Turtle()
turtle.shape("square")
turtle.shapesize(6, .1)
tilt_amnt = 0
for x in range (60):
turtle.tilt(tilt_amnt)
tilt_amnt = tilt_amnt + 6
time.sleep(1)
I don't think there is a way to tilt the turtle in such a way where it turns from the bottom but you can rewrite the code to essentially do the same thing but using forward instead. Check this out.
from turtle import *
import time
turtle = Turtle()
turtle.pensize(2)
turtle.hideturtle()
turtle.speed(0)
tilt_amnt = 0
for x in range (60):
turtle.right(tilt_amnt)
turtle.forward(100)
turtle.backward(100)
tilt_amnt = tilt_amnt + 6
time.sleep(1)
turtle.clear()
Some thoughts: first, I wouldn't use tilt() (high overhead, forces an update), even if you do use turtles as hands, consider using right() or setheading(); second, I would use mode('logo') as this makes 0 degrees the top of the screen and makes headings clockwise (like, say a 'clock') instead of counterclockwise; third, if you want accuracy, don't use sleep() but rather extract the current time from the system and set your hands accordingly.
Large real world clocks do turn their hands from the middle, or more specifially, the center of gravity. This keeps the hands from slowing the mechanism on the way up, or rushing it on the way down. The trick is to make the hands look visually asymmetric but keep the weight symmetric. Here's a solution I came up with earlier that does something like this with turtles as the hands, turning from their centers.
Finally, here's a rewrite of the example #AlexJoslin provided to use realtime accuracy, potentially updating hand positions multiple times within a second:
from turtle import Screen, Turtle
from time import localtime as time
def tick():
second.setheading(6 * time().tm_sec)
second.clear()
second.forward(150)
second.backward(150)
screen.update()
screen.ontimer(tick)
screen = Screen()
screen.mode('logo')
screen.tracer(False)
second = Turtle()
second.hideturtle()
second.pensize(2)
tick()
screen.exitonclick()

Drawing circles based on user input

I am writing a Python code for a school project. The program is to draw a number of circles based on the user input, (2-6 circles). I have done that but now need to draw multiple rows of circles based on user input. For example, if a user picks 3 circles I need to draw 3 circles by 3 circles.
#import tkinter and turtle
import tkinter as tk
from turtle import *
#ask for user input using tkinter
from tkinter import simpledialog
application_window = tk.Tk()
answer = simpledialog.askinteger('Request', 'number of circles(2-6)?',
parent=application_window, minvalue=2, maxvalue=6)
#set position of turtle and define turtle
t = Turtle()
t.penup()
t.setpos(-200,-200)
t.pendown()
#draw appropriate number of circles using loop
i = 2
for i in range (answer):
circle(30)
penup()
forward(60)
pendown()
i = +1
Don't make us do your homework. Tell us the problem, tell us what you've tried, then ask a question. For example, "How do I make the turtle draw three rows of circles?"
Surround the for loop you already have with another for loop. Every time the outer loop runs, the inner loop will run to create one row, and then move the turtle down before the next iteration.
#import tkinter and turtle
import tkinter as tk
from turtle import *
#ask for user input using tkinter
from tkinter import simpledialog
application_window = tk.Tk()
answer = simpledialog.askinteger('Request', 'number of circles(2-6)?',
parent=application_window, minvalue=2, maxvalue=6)
#set position of turtle and define turtle
t = Turtle()
t.penup()
t.setpos(-200,-200)
t.pendown()
#draw appropriate number of circles using loop
for row in range(answer):
for col in range(answer):
circle(30)
penup()
forward(60) # must be undone after loop
pendown()
penup()
backward(60) # undo the backward on line 20
backward(60 * col) # go backwards the length of the diameter times the number of circles
right(90) # turn right 90 degrees
forward(60) # forward the length of one diameter
left(90) # turn back left 90 degrees
pendown()
I'm offering an alternate solution to that of #JacksonH for two reasons: first, if you're using Python 3 (and you should be), then you don't need to grab simpledialog out of tkinter, use Python 3 turtle's numinput() (and textinput()) instead; second, an aesthetic goal should be to have the turtle move as little as possible to achieve your result, even if that requires more, smarter code. Watch the difference in how this solution draws the picture as opposed to the other:
from turtle import Screen, Turtle
# Ask for user input
DEFAULT_COUNT = 3
screen = Screen()
answer = screen.numinput('Request', 'number of circles(2-6)?', default=DEFAULT_COUNT, minval=2, maxval=6)
if answer is None:
answer = DEFAULT_COUNT
else:
answer = int(answer) # int for range()
# define turtle and set position of turtle
turtle = Turtle()
turtle.speed('fast') # because I have little patience
turtle.penup()
turtle.setpos(-200, -200)
direction = 1
# draw appropriate number of circles using nested loops
for row in range(answer - 1, -1, -1): # loop backward to use last iteration as a flag
for column in range(answer - 1, -1, -1):
turtle.pendown()
turtle.circle(30 * direction)
turtle.penup()
if column:
turtle.forward(60) # not done final time
if row: # every time but the last
turtle.left(90 * direction) # turn +/-90 degrees
turtle.forward(60) # forward the length of one diameter
turtle.left(90 * direction) # turn +/-90 degrees
direction *= -1 # reverse our sense of direction
turtle.hideturtle()
screen.exitonclick()

Python while statement turtle.distance

It is my understanding that the following code
turtle.forward(50)
means moving the turtle on the screen.
Here is a code I am trying to learn:
def forward(distance):
while distance > 0:
if turtle.distance(0,0) > 100:
angle = turtle.towards(0,0)
turtle.setheading(angle)
turtle.forward(1)
distance = distance - 1
I don't really understand how
turtle.forward(1)
distance = distance -1
works here. I understand that if the distance is greater than 100, then the turtle turns back and moves from the end position to the position less than 100. I experimented with it but it is still not clear to me what the code turtle.forward(1) means. Is the turtle supposed to move by 1 pixel? And the final distance is less by another pixel? When I put in different numbers instead of 1, I get strange results. Sorry for asking this question - I am learning very slowly.
Thanks.
turtle.forward(1) is telling the turtle to move forward in the current direction by one pixel.
if turtle.distance(0,0) > 100 checks how far away the turtle is from the origin (center of the pane) and see if it's greater than 100.
angle = turtle.towards(0,0) turtle.setheading(angle) turns the turtle towards the origin
Here's how it comes together:
def forward(distance):
while distance > 0: # loop this while there's still distance
if turtle.distance(0,0) > 100: # check if the distance from the origin is greater that 100
angle = turtle.towards(0,0) # find the angle for the origin from turtle's current location
turtle.setheading(angle) # turns the turtle toward the angle
turtle.forward(1) # move title forward in the current angle by one pixel
distance = distance - 1 # act as a counter for the while loop, so when distance is finally 0, the while loop breaks
The code is kind of contradictory to what it tries to achieve. It looks like the forward function is trying to move towards position (0, 0) in distance number of steps.
def forward(distance):
while distance > 0:
# if the turtle is more than 100 distance away from (0, 0)
# change orientation to face (0, 0)
if turtle.distance(0,0) > 100:
angle = turtle.towards(0,0)
turtle.setheading(angle)
# Make the turtle move one step towards (0, 0)
turtle.forward(1)
# IMO, distance should've been called steps_left
distance = distance - 1
LOGICAL INCONSISTENCY
The code seems like it is trying to make the turtle move towards the origin (0, 0) BUT at the same time it has a counter of sorts called distance - that acts more like steps_allowed_to_take - which might cause the turtle to reach the origin but still attempt to move towards the origin. The turtle.forward(1) in this scenario would actually cause the turtle to move one step away from the origin and in the next loop, the turtle would move back to the origin. Doesn't make sense.
This function is a replacement for turtle.forward() that keeps a turtle that's on a random walk within a circular boundary.
Consider a simplified version of the function:
def forward(distance):
while distance > 0:
turtle.forward(1)
distance = distance - 1
It does exactly what turtle.forward(distance) does, though less efficiently. It moves the turtle forward one pixel at a time until it goes the entire distance, rather then go the whole distance in one motion via turtle.forward(distance). Why? This allows the code to make a decision on each pixel of the move regarding whether the turtle has violated the boundary and adjust its heading:
if turtle.distance(0, 0) > 100:
angle = turtle.towards(0, 0)
turtle.setheading(angle)
Let's embed this function in code to illustrate what it does:
from random import randint
import turtle
def forward(distance):
while distance > 0:
if turtle.distance(0, 0) > 100:
angle = turtle.towards(0, 0)
turtle.setheading(angle)
turtle.forward(1)
distance = distance - 1
boundary = turtle.Turtle(visible=False)
boundary.color("red")
boundary.penup()
boundary.sety(-100)
boundary.pendown()
boundary.circle(100)
turtle.shape("turtle") # default turtle
for _ in range(1000): # akin to while True:
turtle.left(randint(0, 90))
forward(randint(1, 50))
turtle.done()
OUTPUT

Python Turtle Graphics Choose Point X Distance Away

I am writing a program for the Python Turtle module and I would like it to choose a position X distance away. So basically, from the turtle's present position it needs to choose a point no further or less than X distance away and have the ability to draw a line to that position. Additionally, it should chose this position at random so it is different almost every time.I know I should use the Pythagorean theorem and perhaps the randint module, but I can't figure out how to implement either.
Any help greatly appreciated!
Cheers!
5813
Say in this case, you want x to be 50
import turtle
from random import randint
x = 50
turtle = turtle.Turtle()
degrees = randint(0, 360)
turtle.left(degrees)
turtle.forward(x)
turtle.getscreen()._root.mainloop()

Categories

Resources