drawing flower with python turtle - python

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)

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)

Using python turtle to draw a polygon with n number of sides

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

How do I draw a random dot in a circle in python?

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/

Drawing polygon with n number of sides in Python 3.2

I have to write a program in Python that reads the value n and draws a polygon of n sides on the screen. I can use either the turtle graphics module or graphics.py module.
I know how to draw a polygon when n = the number of points you input and then click n times on the screen, but I'm having some trouble getting an idea on how to transform a number of sides into a polygon.
Here's the code I have for the polygon with n number of points:
def newPolygon(self,cmd):
p = eval(input("how many points"))
print("click",p,"times")
num = []
for i in range(p):
vertices = self.win.getMouse()
num.append(vertices)
poly = Polygon(num)
poly.setFill(self.color)
poly.draw(self.win)
self.figs.append(poly)
This isn't the whole code to the program(which is 384 lines). This is just the part of the program where the draw polygon function is where self.figs = [ ] , a list of drawn figures.
I'm assuming what you would like is a way of generating equal sided polygon coordinates which you can feed to your drawing program. I'm not sure which library you are using, so I'm going to stick to lists of pairs of values:
import math
def polygon(sides, radius=1, rotation=0, translation=None):
one_segment = math.pi * 2 / sides
points = [
(math.sin(one_segment * i + rotation) * radius,
math.cos(one_segment * i + rotation) * radius)
for i in range(sides)]
if translation:
points = [[sum(pair) for pair in zip(point, translation)]
for point in points]
return points
There's a fair bit going on in there, so I'll talk through it. The basic approach is to sweep out a circle, and put n equally spaced points on it. These will be the points of our polygon, starting at the 12 'o' clock position.
The first thing to do is work out the angle (in radians) of each wedge from the center outwards. The total number of radians in a circle is 2 pi, so our value is 2 pi / n per segment.
After that a bit of basic trig gives us our points (https://en.wikipedia.org/wiki/Trigonometry#Extending_the_definitions). At this point we scale by our desired radius, and have the opportunity to offset the rotation by a fixed amount too.
After that we translate the values by a certain amount, because you probably want your polygon in the center of the screen, not in the corner.
A few examples
print polygon(5) # A unit pentagon
# [(0.0, 1.0), (0.9510565162951535, 0.30901699437494745), (0.5877852522924732, -0.8090169943749473), (-0.587785252292473, -0.8090169943749476), (-0.9510565162951536, 0.30901699437494723)]
print polygon(4, 100) # A square, point up, 100 from the center to the points
# [(0.0, 100.0), (100.0, 6.123233995736766e-15), (1.2246467991473532e-14, -100.0), (-100.0, -1.8369701987210297e-14)]
print polygon(4, 2, math.pi / 4, [10, 10]) # A flat square centered on 10, 10
# [[11.414213562373096, 11.414213562373096], [11.414213562373096, 8.585786437626904], [8.585786437626904, 8.585786437626904], [8.585786437626904, 11.414213562373094]]
As you can see, these are all floats, so you may need to squish these to integers before you can use them.
I don't know if this will help but to define a polygon using number of sides and length then I would use my code:
import turtle as t
def polygon(n,l):
f = (n - 2) * 180/n
for i in range(n):
t.forward(l)
t.right(180 - f)
polygon()
In this case, n would be number of sides and l would be length of sides.
This took me quite a while as I am only 13 and am not advanced but this was a fun project!

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