I am trying to create a program that draws a polygon with n sides. I know how to draw the polygon, I am just struggling with another part. I want to have lines from the corners of the polygon to the center of the polygon. I have tried various math equations like s / (2 sin(180 / n)), but I can't get anything to work. Does anyone know of a way to do this? My code for drawing a simple polygon is below.
def polygon(t, n, l, a):
#t = turtle
#n = number of sides
#l = length
#a = angle
for i in range(n):
t.forward(l)
t.left(360 / n)
We can work out the trigonometry to do this, and there are examples of such if you seach SO. Here's one approach:
from turtle import Screen, Turtle
from math import pi, sin
def polygon(turtle, sides, length):
angle = 2 * pi / sides
radius = length / 2 / sin(angle / 2)
towards_center = angle / 2 + pi / 2
turtle.penup()
turtle.forward(radius)
turtle.left(towards_center)
turtle.pendown()
for _ in range(sides):
turtle.forward(length)
turtle.left(towards_center)
turtle.forward(radius)
turtle.backward(radius) # undo forward(radius)
turtle.right(towards_center) # undo turtle.left(towards_center)
turtle.left(angle)
screen = Screen()
yertle = Turtle()
yertle.radians() # switch turtle to radians to match math
yertle.dot() # mark the center of our screen for reference
polygon(yertle, 5, 100)
yertle.hideturtle()
screen.exitonclick()
Tacking advantage of turtle's own capabilities, we can also do:
def polygon(turtle, sides, length):
angle = 2 * pi / sides
radius = length / 2 / sin(angle / 2)
turtle.penup()
turtle.sety(-radius)
turtle.pendown()
for _ in range(sides):
turtle.circle(radius, angle, steps=1)
heading = turtle.heading()
position = turtle.position()
turtle.home()
turtle.setposition(position)
turtle.setheading(heading)
Related
I'm trying to use patterns to create a flower with diamond shaped petals but these oval shaped ones are the closest I've been able to get. I was wondering if anyone could help or provide any feed back with this.
Flower I'm trying to create
Code:
import turtle
def petal(t, r, angle):
for i in range(2):
t.circle(r,angle)
t.left(180-angle)
def flower(t, n, r, angle):
for i in range(n):
petal(t,r,angle)
t.left(360.0/n)
def move(t, length):
window = turtle.Screen()
window.bgcolor("Yellow")
t.pu()
t.fd(length)
t.pd()
sam = turtle.Turtle()
sam.speed(99)
move(sam, -150)
sam.color("red")
move(sam, 150)
flower(sam, 10, 40.0, 100.0)
Use straight movements in the petal function:
def petal(t,d,a):
for _ in range(2):
t.fd(d)
t.left(a)
t.fd(d)
t.left(180-a)
note: the flower looks closer to your objective with an angle of 60 instead of 100
I agree with #AlainT. about the use of the smaller 60 degree angle, but I believe that solution falls short if we interpret r in the OP's petal(t, r, angle) call as radius. If so, the rhombi drawn for the petals should be constrained by that radius:
To do so requires a little bit of math to calculate how far to move forward on each edge of the petal, not using the passed radius as the distance:
from math import radians, sin
from turtle import Screen, Turtle
def petal(turtle, radius, angle):
side = (radius / 2) / sin(radians(90 - angle/2))
for _ in range(2):
turtle.forward(side)
turtle.left(angle)
turtle.forward(side)
turtle.left(180 - angle)
def flower(turtle, petals, radius, angle):
for _ in range(petals):
petal(turtle, radius, angle)
turtle.left(360 / petals)
screen = Screen()
screen.bgcolor('yellow')
sam = Turtle()
sam.hideturtle()
sam.speed('fastest')
sam.color('red')
flower(sam, 10, 80, 60)
screen.exitonclick()
I wanted ask how can I draw a circle using turtle module in python just using turtle.forward and turtle.left? I use the code below:
for i in range(30):
turtle.forward(i)
turtle.left(i)
turtle.done()
What I get is that the line does not stop once I get the full cirle. How can I code so that I have a circle of specific radius and that I have a full stop once the circle is drawn (without using turtle.circle).
I made this image as a reference,
Essentially you need to draw the inscribed polygon with n sides.
The initial left turn will be ϴ/2.
Then forward by a = 2rsin(ϴ/2).
Each forward is followed by a left turn of the full ϴ, except that after the last forward we want only a left turn of ϴ/2 so that the heading will be correctly updated to be tangential to the circle (or arc).
Something like this,
import turtle
import math
def circle2(radius,extent=360,steps=360):
if extent<360 and steps==360:
steps=extent
theta=extent/steps
step_size=2*radius*math.sin(math.radians(theta/2))
turtle.left(theta/2)
turtle.forward(step_size)
for i in range(1,steps):
turtle.left(theta)
turtle.forward(step_size)
turtle.left(theta/2)
turtle.hideturtle()
turtle.speed(0)
turtle.getscreen().tracer(False)
circle2(50)
circle2(100,180)
turtle.up()
turtle.home()
turtle.down()
circle2(130)
circle2(130,360,10)
turtle.update()
turtle.mainloop()
If you want to draw a circle the best thing to do is to simplyfy the problem, if we consider moving 1 space for each degree of the circle then we can simply write this as
def draw_circle1():
for _ in range(360):
turtle.forward(1)
turtle.left(1)
Now what do we know about this basic circle that we drew? well we know it took 360 steps and each step was 1. so the circle has a circumference of 360. we can use a bit of math to calculate the radius.
circumference = 2 * 3.14... * radius
360 = 2 * 3.14... * radius
360 / 2 / 3.14... = radius
radius = 57.29...
So now we can reverse this, if we want to specify a circle of a given radius, we can calculate what circumference that circle should have. divide that by the 360 degrees and we know what size step to take before each turn of 1 degree.
def draw_circle(radis):
circumfrence = 2 * math.pi * radis
step_size = circumfrence / 360
for _ in range(360):
turtle.forward(step_size)
turtle.left(1)
if we run this for 3 separate circles each increasing in size you see it gives us a consistent result
draw_circle(20)
draw_circle(40)
draw_circle(60)
turtle.hideturtle()
turtle.done()
So now we have a function which can accept a radius and draw a circle based on that radius
A spirograph code
from turtle import Turtle
import random
josh = Turtle()
josh.color('DarkRed')
def random_color()->tuple:
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
return (r,g,b)
josh.speed('fastest')
josh.pensize(2)
for i in range(72):
josh.circle(100)
josh.right(5)
colormode(255)
josh.pencolor(random_color())
screen = Screen()
screen.setup(800,800)
screen.exitonclick()
Example here,
import turtle
def circle(distance, sections):
angle = 360/sections
for i in range(1, sections+1):
turtle.forward(distance)
turtle.left(angle)
circle(20, 30)
turtle.done()
Create a SPIROGRAPH using turtle. Final output:
import random
import turtle
from turtle import Turtle, Screen
tim = Turtle()
tim.shape('arrow')
turtle.colormode(255)
def random_colour( ):
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
return (r, g, b)
tim.speed('fastest')
def draw_spirograph(size_of_gap):
for _ in range(int(360/size_of_gap)):
tim.color(random_colour())
tim.circle(100)
tim.setheading(tim.heading()+size_of_gap)
draw_spirograph(5)
screen = Screen()
screen.exitonclick()
I'm having trouble writing a recursive function that draws circles to a certain 'depth'.
For example, at depth one, the program should draw this: depth 1
At depth two the program should draw this: depth 2
At depth three the program should draw this:depth 3
And for reference, my program draws this:myTurtleDrawing
import turtle
# These are basic instructions that do not affect the drawing if changed, only the appearance of the entities within the window
turtle.mode('logo')
turtle.speed(1)
turtle.shape('classic')
turtle.title("Circle")
def recCircle(d, r):
if d == 0:
pass
if d == 1:
print("Drawing to the depth of: ", d)
turtle.down()
turtle.circle(r)
turtle.up()
else:
print("Drawing to the depth of: ", d)
turtle.circle(r)
turtle.seth(90)
turtle.down()
recCircle(d - 1, (r / 2)) # Draw the leftmost circle
turtle.seth(360)
turtle.up
turtle.seth(270)
turtle.forward(2 * r)
turtle.down()
recCircle(d - 1, - r / 2) # Draw the rightmost circle
turtle.up()
turtle.seth(360)
turtle.forward(2*r)
def main():
d = 3 #depth of recursion
r = 100 #radius of circle
recCircle(d, r)
turtle.done()
main()
I believe the problem lies around line 20
turtle.circle(r)
I can not figure out how to return the turtle to the same location and orientation.
turtle.home or turtle.goto as I'm trying not to use those
Specific issues with your code:
turtle.mode('logo')
I understand the desire to work with North == 0 but in the case of this design, it's not to your advantage and I'd stay with the default orientation. This won't work:
turtle.up
It needs to be turtle.up(). I don't see how you got your example output with this in the code as it should throw an error. This is OK:
turtle.seth(270)
as long as you're assuming a vertical orientation. But in general, if we want to draw at any angle, you can't use setheading() as it's as absolute as turtle.goto() or turtle.home(). But this, seems strange:
turtle.seth(360)
vs. simply turtle.setheading(0). A key concept when doing a drawing like this is to return the turtle to where it started, either implicitly in the drawing command, or explicitly by undoing anything you did to position the turtle. Below's my complete rework of your code:
from turtle import Screen, Turtle
def recCircle(depth, radius):
if depth == 0:
return
print("Drawing to the depth of: ", depth)
turtle.pendown()
turtle.circle(radius)
turtle.penup()
if depth > 1:
length = 11 * radius / 8 # no specific ratio provided, so eyeballed
turtle.left(45)
turtle.forward(length)
turtle.right(45)
recCircle(depth - 1, radius / 2) # Draw the leftmost circle
turtle.backward((2 * length ** 2) ** 0.5)
recCircle(depth - 1, radius / 2) # Draw the rightmost circle
turtle.right(45)
turtle.forward(length)
turtle.left(45)
screen = Screen()
screen.title("Circle")
turtle = Turtle('classic')
turtle.speed('fast')
depth = 3 # depth of recursion
radius = 100 # radius of circle
recCircle(depth, radius)
screen.exitonclick()
I have this assignment for school:
Build a Snowman without turtle circle function
The snowman should be on a blue background, and should be drawn filled with white.
The outline of the snowman should be in black.
The snowman’s body should be made of 3 filled circles.
The outline of each circle should be 3 pixels wide.
The bottom circle should have a radius of 100 pixels.
The middle circle should have a radius of 70 pixels.
The top circle should have a radius of 40 pixels.
Each circle should be centered above the one below it (except the bottom circle, which can be located anywhere).
There should be no gap between the circles.
Give the snowman a mouth, eyes, and a nose (a hat is optional).
Make sure to include two stick-arms and at least two fingers on each hand.
So far I created this, but I can't seem to get the circles right before I move on.
Also, don't know how to color in circles or make dots for eyes. Help me please, first time coding.
import turtle # allows us to use turtle library
wn = turtle.Screen() # allows us to create a graphics window
wn.bgcolor("blue") # sets gtaphics windows background color to blue
import math # allows us to use math functions
quinn = turtle.Turtle() # sets up turtle quinn
quinn.setpos(0,0)
quinn.pensize(3)
quinn.up()
# drawing first circle middle
quinn.forward(70)
quinn.down()
quinn.left(90)
# calculation of cicumference of a circle
a = (math.pi*140.00/360)
#itineration for first circle
for i in range (1,361,1):
quinn.left(a)
quinn.forward (1)
# drawing second circle bottom
quinn.up()
quinn.home()
quinn.right(90)
quinn.forward(70)
quinn.left(90)
quinn.down()
b = (math.pi*200.00/360)
for i in range (1,361,1):
quinn.right(b)
quinn.forward(1)
# drawing third circle head top
quinn.up ()
quinn.goto(0,70)
quinn.right(90)
quinn.down()
c =(math.pi*80/360)
for i in range (1,361,1):
quinn.left(c)
quinn.forward(1)
wn.exitonclick()
The following is an example function to draw a circle filled in blue:
def draw_circle(radius):
turtle.up()
turtle.goto(0,radius) # go to (0, radius)
turtle.begin_fill() # start fill
turtle.down() # pen down
turtle.color('blue')
times_y_crossed = 0
x_sign = 1.0
while times_y_crossed <= 1:
turtle.forward(2*math.pi*radius/360.0) # move by 1/360
turtle.right(1.0)
x_sign_new = math.copysign(1, turtle.xcor())
if(x_sign_new != x_sign):
times_y_crossed += 1
x_sign = x_sign_new
turtle.up() # pen up
turtle.end_fill() # end fill.
return
Then you can modify the above function adding parameters for position (x,y) of the circle center:
def draw_circle(radius, x, y):
turtle.up()
turtle.goto(x,y+radius) # go to (x, y + radius)
turtle.begin_fill() # start fill
turtle.down() # pen down
turtle.color('blue')
times_y_crossed = 0
x_sign = 1.0
while times_y_crossed <= 1:
turtle.forward(2*math.pi*radius/360.0) # move by 1/360
turtle.right(1.0)
x_sign_new = math.copysign(1, turtle.xcor())
if(x_sign_new != x_sign):
times_y_crossed += 1
x_sign = x_sign_new
turtle.up() # pen up
turtle.end_fill() # end fill.
return
You can easily add dots as, for instance,:
turtle.goto(-20,10)
turtle.color('red')
turtle.dot(20)
turtle.goto(40,10)
turtle.dot(20)
Putting together:
import turtle
import math
def draw_circle(radius, x, y):
turtle.up()
turtle.goto(x,y+radius) # go to (0, radius)
turtle.begin_fill() # start fill
turtle.down() # pen down
turtle.color('blue')
times_y_crossed = 0
x_sign = 1.0
while times_y_crossed <= 1:
turtle.forward(2*math.pi*radius/360.0) # move by 1/360
turtle.right(1.0)
x_sign_new = math.copysign(1, turtle.xcor())
if(x_sign_new != x_sign):
times_y_crossed += 1
x_sign = x_sign_new
turtle.up() # pen up
turtle.end_fill() # end fill.
return
draw_circle(100, 10, 10)
turtle.goto(-20,10)
turtle.color('red')
turtle.dot(20)
turtle.goto(40,10)
turtle.dot(20)
turtle.pen(shown=False)
turtle.done()
You should attempt to do by yourself the remaining part of the assignment.. ;)
Sorry for not giving an explanation.
The first part is Ramanujan's aproximation of pi, but not a very good one because it only reaches an aproximation of pi after something like 300,000 iterations of the loop and it is only accurate to 5 decimal places. that would be this part:
r += (1 / k) * (-1)**i
pi = (4 * (1 - r))
Then I use the circumference of a circle:
t.forward(2*5*pi)
Finally I just make the turtle walk clock wise by 20.
import turtle
t = turtle.Turtle()
t.right(90)
t.penup()
t.goto(100, 0)
t.pendown()
i = 0
r = 0
k = 3
while i <= 360:
r += (1 / k) * (-1)**i
pi = (4 * (1 - r))
t.write(pi)
t.forward(2*5*pi)
t.right(20)
i += 1
k += 2
turtle.done()
From a mathematical standpoint, you can use functions sin and cos from the math to plot the circle.
Once the circle is plot, use the turtle.begin_fill() and turtle.end_fill() methods to fill in the circle (though I do agree that in programming, this method is more practical):
from turtle import Turtle
from math import sin, cos, radians
def draw_circle(radius, x, y, color="light blue", line_width=3):
c = Turtle(visible=False)
c.width(3)
c.penup()
c.goto(x + radius, y)
c.pendown()
c.color("black", color)
c.begin_fill()
# Circle drawing starts here
for i in range(1, 361):
c.goto(radius * cos(radians(i)) + x,
radius * sin(radians(i)) + y)
# Circle drawing ends here
c.end_fill()
draw_circle(100, 0, -100)
As pointed out in this answer, you can use the turtle.dot() method to draw a dot on the screen,
and the width of the pen will be the diameter of the dot.
There's another way to do that, but compared to the dot method, it is impractical.
I'll throw it out there anyway, just to show how there are many possibilities in workarounds:
from turtle import Turtle
def draw_circle(radius, x, y, color="light bue", line_width=3):
c = Turtle(visible=False)
c.penup()
c.goto(x, y)
c.pendown()
# Circle drawing starts here
c.width(radius * 2 + line_width)
c.forward(0)
c.color(color)
c.width(radius * 2 - line_width)
c.forward(0)
# Circle drawing ends here
draw_circle(100, 0, -100)
So a turtle.dot() is the equivalent of turtle.forward(0) (and turtle.backward(0), turtle.goto(turtle.pos()), turtle.setpos(turtle.pos()), etc., etc.).
Output:
Most solutions to "without turtle circle function" involve writing your own equivalent to turtle's circle function. But there are already two other ways you can draw outlined, filled circles with turtle.
One is you can use concentric dots:
turtle.color('black')
turtle.dot(200 + 3)
turtle.color('white')
turtle.dot(200 - 3)
Just remember that dot() takes a diameter whereas circle() takes a radius:
However, I prefer to use stamping to solve these sorts of problems:
''' Build a Snowman without turtle circle function '''
from turtle import Turtle, Screen
# The snowman’s body should be made of 3 filled circles.
# The bottom circle should have a radius of 100 pixels.
# The middle circle should have a radius of 70 pixels.
# The top circle should have a radius of 40 pixels.
RADII = (100, 70, 40)
STAMP_SIZE = 20
# The snowman should be on a blue background
screen = Screen()
screen.bgcolor('blue')
quinn = Turtle('circle')
quinn.setheading(90)
quinn.up()
# The outline of the snowman should be in black, and should be drawn filled with white.
quinn.color('black', 'white')
for not_first, radius in enumerate(RADII):
if not_first:
quinn.forward(radius)
# The outline of each circle should be 3 pixels wide.
quinn.shapesize((radius * 2) / STAMP_SIZE, outline=3)
quinn.stamp()
# Each circle should be centered above the one below it
# There should be no gap between the circles.
quinn.forward(radius)
# Give the snowman eyes
quinn.shapesize(15 / STAMP_SIZE)
quinn.color('black')
quinn.backward(3 * RADII[-1] / 4)
for x in (-RADII[-1] / 3, RADII[-1] / 3):
quinn.setx(x)
quinn.stamp()
# Give the snowman a mouth, and a nose (a hat is optional).
pass
# Make sure to include two stick-arms and at least two fingers on each hand.
pass
quinn.hideturtle()
screen.exitonclick()
The idea is you contort the turtle cursor itself to what you need, make a snapshot of it on the screen, and then contort it to the next thing you need to draw.
You can create a function that takes arguments fd, and left.
here is what I created.
from turtle import *
speed(100000)
for i in range(360):
fd(2)
left(1)
Here is the calculation: The iterations in range divided by the fd+left.
That is the approximation I have. SO you should be able to create a function like that.
You could try this, hope this helps!
def polygon(length, sides):
for i in range(sides):
turtle.forward(length)
turtle.left(360.0/sides)
polygon(1, 360)
I have a program that is suppose to create a triangle at an x,y with a set height and a set width. This is the code I have but the triangle that it is creating is messed up sometimes if the width is really small. How do I make a perfect triangle with whatever numbers I want as the width and height?
import turtle
def triangleBuild(width,height):
turtle.forward(width)
turtle.left(120)
turtle.forward(height)
turtle.left(120)
turtle.forward(height)
def xYPostion(x,y,width,height):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
triangleBuild(width,height)
The height in your case is the distance from the top vertex to the base. And what you are doing is that you are drawing a triangle with 2 sides of the same length (height) You may want to use some math to compute the correct sides length (that may not be equal to the height)
Edit
If you want to draw a triangle just from the width and height, you may want to get the angle of the triangle, then with some math:
import turtle
import math
def triangleBuild(width,height):
l = ( height**2 + (width/2.0)**2)**0.5
alfa = math.atan2(height, width/2.0) # To compute alfa
alfa = math.degrees(alfa)
alfa = 180.0 - alfa
turtle.forward(width)
turtle.left(alfa)
turtle.forward(l)
turtle.left(2*(180-alfa))
turtle.forward(l)
turtle.penup()
turtle.goto(10,20)
turtle.pendown()
width = 200
height = 100
triangleBuild(width,height)