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)
Related
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()
I am new to python with turtle and would appreciate some help.
I am trying to create a program that takes an input for a number of sides then draws a regular polygon with that number of sides. However, it either produces a TimeLimitError or it simply draws a straight line.
Here is what I have:
sides = int(input("How many sides would you like? "))
angle = sides / 360
import turtle
for count in range(sides):
turtle.fd(50)
turtle.lt(angle)
But this is what it keeps producing:
How many sides would you like? 5
TimeLimitError: Program exceeded run time limit. on line 1
you should divide 360 by number of sides not the other way around.
angle = 360 / sides
I am learning programming with python (referring think python 2) and am struck at a program. Problem statement: Python program to draw a symmetric flower after seeking the size of and number of petals from user.
The code i came up with is below, except i am unable to get the angle between each petal mathematically right (part where the code near the end states bob.lt(360/petal)). Can someone help here?
import math
radius=int(input("What is the radius of the flower? "))
petals=int(input("How many petals do you want? "))
#radius=100
#petals=4
def draw_arc(b,r): #bob the turtle,corner-to-corner length (radius) of petal (assume 60 degree central angle of sector for simplicity)
c=2*math.pi*r #Circumference of circle
ca=c/(360/60) #Circumference of arc (assume 60 degree central angle of sector as above)
n=int(ca/3)+1 #number of segments
l=ca/n #length of segment
for i in range(n):
b.fd(l)
b.lt(360/(n*6))
def draw_petal(b,r):
draw_arc(b,r)
b.lt(180-60)
draw_arc(b,r)
import turtle
bob=turtle.Turtle()
#draw_petal(bob,radius)
for i in range(petals):
draw_petal(bob,radius)
bob.lt(360/petals)
turtle.mainloop()
Correct (Symmetric)
Incorrect (Asymmetric)
I think the problem is simpler than you're making it.
The first issue is that drawing a petal changes the turtle heading and you're trying to do math to set it back to where it started. Here we can just record the heading before drawing the petal and restore it afterward, no math.
The second issue is you're implementing your own arc code when turtle can do this using an extent argument to turtle.circle() which produces the same result but much faster:
from turtle import Turtle, Screen
def draw_petal(turtle, radius):
heading = turtle.heading()
turtle.circle(radius, 60)
turtle.left(120)
turtle.circle(radius, 60)
turtle.setheading(heading)
my_radius = int(input("What is the radius of the flower? "))
my_petals = int(input("How many petals do you want? "))
bob = Turtle()
for _ in range(my_petals):
draw_petal(bob, my_radius)
bob.left(360 / my_petals)
bob.hideturtle()
screen = Screen()
screen.exitonclick()
USAGE
> python3 test.py
What is the radius of the flower? 100
How many petals do you want? 10
OUTPUT
Just modify your code like this(in draw_petals add b.rt(360/petals-30 and correct bob.lt(360/petals) to 360/4 ):
import math
radius=int(input("What is the radius of the flower? "))
petals=int(input("How many petals do you want? "))
#radius=100
#petals=4
def draw_arc(b,r): #bob the turtle,corner-to-corner length (radius) of petal (assume 60 degree central angle of sector for simplicity)
c=2*math.pi*r #Circumference of circle
ca=c/(360/60) #Circumference of arc (assume 60 degree central angle of sector as above)
n=int(ca/3)+1 #number of segments
l=ca/n #length of segment
for i in range(n):
b.fd(l)
b.lt(360/(n*6))
def draw_petal(b,r):
draw_arc(b,r)
b.lt(180-60)
draw_arc(b,r)
b.rt(360/petals-30) # this will take care of the correct angle b/w petals
import turtle
bob=turtle.Turtle()
#draw_petal(bob,radius)
for i in range(petals):
draw_petal(bob,radius)
bob.lt(360/4)
So I have my circle drawn already, it has a radius of 140. Should I use r.randint(-140,140) to throw a random dot? and how do I make it seen in the circle(turtle graphic)?
You will need to verify that the point is actually inside your circle before you draw it, the point (-140,-140) isn't inside the circle for example but could be generated by (randint(-140,140), randint(-140,140)).
The common way of doing this is to loop until you get a result that fits your restrictions, in your case that its distance from (0,0) is less than the radius of the circle:
import math, random
def get_random_point(radius):
while True:
# Generate the random point
x = random.randint(-radius, radius)
y = random.randint(-radius, radius)
# Check that it is inside the circle
if math.sqrt(x ** 2 + y ** 2) < radius:
# Return it
return (x, y)
A non-looping variant:
import math, random, turtle
turtle.radians()
def draw_random_dot(radius):
# pick random direction
t = random.random() * 2 * math.pi
# ensure uniform distribution
r = 140 * math.sqrt(random.random())
# draw the dot
turtle.penup()
turtle.left(t)
turtle.forward(r)
turtle.dot()
turtle.backward(r)
turtle.right(t)
for i in xrange(1000): draw_random_dot(140)
It depends on where the beginning of the coordinate system is. If zeros start in the left upper conner of the picture, while loop is needed to make sure that dots are being placed within the circle's boundaries. If xy coordinates start in the center of the circle then the placement of the dots is limited by the circle's radius. I made a script for Cairo. It is not too too off topic. https://rockwoodguelph.wordpress.com/2015/06/12/circle/
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()