Trying to draw this picture using turtle, just stuck on the last bit of drawing square into a circle. squares to make circle
so far I've tried just drawing out the points of each individual line but takes way to long with some inconsistencies. What I have so far is
def square(side_length):
for i in range(4):
turtle.fd(side_length)
turtle.lt(90)
square (150)
turtle.penup()
####New Square###
turtle.left(90)
turtle.forward(75)
turtle.left(90)
turtle.forward(30)
turtle.right(180)
turtle.right(45)
turtle.pendown()
def square(side_length):
for i in range(4):
turtle.fd(side_length)
turtle.lt(90)
square (150)
This draws two squares. Now I just need to figure out a way to rotate the two squares by, say, 30 degrees clockwise 4 times. Is there a function I can use to do this, or do I just need to do a lot of math and calculate to draw each individual line?
Despite its name, turtle.circle can be used to draw other regular polygons. It also can be used to draw only part of the polygon. Combined with picking the pen up and down, you can easily draw a series of rotated shapes sharing the same center.
For example,
for i in range(19):
turtle.circle(100, 360, 4) # draw a square
turtle.penup()
# "draw" 10 degrees of the same circle, with the pen up, just to move the pen
turtle.circle(100, 10, 4)
turtle.pendown()
(The circle is just a polygon with enough sides to approximate a circle. If you don't specify the third argument explicitly, turtle picks a sufficiently large value based on the radius.)
It's a combination of the two. You need to do a little math to find the starting point for each square. However, once you move the turtle to that point and turn it to the correct heading, your block of statements to draw the four sides will work just fine.
After you draw the last side of a square, the turtle is sitting on the outer circle, facing 45 degrees off a radius to that point.
Turn the turtle 60 degrees to face across a shallow chord of the circle.
Move it the appropriate distance to traverse that chord (this is where your math comes in).
Turn the turtle another 60 degrees. You are now ready to draw the next square.
Repeat those steps for each added square.
Related
The turtle simulator is quite helpful for moving objects but the problem I am facing is that I don't know the pixel value of the screen where the 'turtle arrow' starts its drawing. Also, when it comes to drawing a circle, it becomes difficult to figure out the pixel coordinates of its centre. Here is an example of a code:
import turtle
ob = turtle.Turtle()
ob.right(100)
#Where does the turtle start with its head (pixel coordinates)?
ob.circle(5)
#Now the turtle draws a circle with radius 5, but in which direction will it point at first?
#How do we figure out the centre of this circle?
Could someone please help me with these two problems?
PS: I am using python 3.10
import turtle
ob = turtle.Turtle()
ob.right(100)
Where does the turtle start with its head (pixel coordinates)?
Turtles start out at (0, 0), the origin. Since you didn't move the turtle, it's head is still over the origin.
ob.circle(5)
Now the turtle draws a circle with radius 5, but in which direction will it point at first?
The turtle starts drawing the circle in whatever direction the turtle is currently pointing. For a newly hatched turtle, that's 0 degrees which is right on the screen. (Using mode 'logo' changes this default.)
Since your turtle first turned to the right 100 degrees, it will start drawing at 260 degrees heading (360 - 100), slightly to the left of straight down (i.e. 270 degrees.)
How do we figure out the centre of this circle?
If the circle was drawn with a newly hatched turtle, then the center would be at (0, 5). (To center the circle on (0, 0), for example, we'd move -5 (i.e. -radius) pixels in the Y direction.)
But your turtle started with a heading of 260 degrees. And, by default, circles are drawn counter-clockwise. So we'd expect the center of your circle to be near (5, 0), where a 270 degree heading would draw it. If we do the math, turning 90 degrees towards the center of the circle and projecting a 5 pixel line, we get:
from math import cos, sin, radians
print(5 * cos(radians(260 + 90)), 5 * sin(radians(260 + 90)))
With output: 4.92403876506104 -0.868240888334652
Similarly, we can also get the center position by doing:
ob.left(90)
ob.penup()
ob.forward(5)
print(ob.position())
With output: (4.92,-0.87)
from turtle import Turtle,Screen
t=Turtle()
s=Screen()
t.left(20)
from cmath import pi
print(pi)
circle=2*pi*40
print("circle=",circle)
t.circle(radius=50,extent=2*pi*50)
turtle docs says that
turtle.circle(radius, extent=None, steps=None)
Draw a circle with given radius. The center is radius units left of the turtle; extent – an angle – determines which part of the circle is drawn. If extent is not given, draw the entire circle. If extent is not a full circle, one endpoint of the arc is the current pen position. Draw the arc in counterclockwise direction if radius is positive, otherwise in clockwise direction. Finally the direction of the turtle is changed by the amount of extent.
As the circle is approximated by an inscribed regular polygon, steps
determines the number of steps to use. If not given, it will be
calculated automatically. May be used to draw regular polygons.
current code
#import the turtle modules
import turtle
#Start a work Screen
ws=turtle.Screen()
#Define a Turtle Instance
geekyTurtle=turtle.Turtle()
#executing loop 6 times for 6 sides
for i in range(6):
#Move forward by 90 units
geekyTurtle.forward(90)
#Turn left the turtle by 300 degrees
geekyTurtle.left(300)
My goal is to make a hexagon grid pattern and I am failing to do it properly. My first issue is if you run the code you get a hexagon but the top is flat, I can't get it to get the pointy corners to get on top. Second I tried to make the grid and it failed and I am not sure why I am unable to copy the same hexagon and clone it next to the other. I will or should have a file of the image that I am going for below.
The output I am getting:
The output I am trying to get:
Before going into loop, turn 30 degrees.
geekyTurtle.right(30)
In order to have its clone beside, just put the turtle to the new place and draw the shape again:
for i in range(6):
geekyTurtle.forward(90)
geekyTurtle.left(300)
geekyTurtle.up()
geekyTurtle.goto(90 * 3 ** .5, 0)
geekyTurtle.down()
for i in range(6):
geekyTurtle.forward(90)
geekyTurtle.left(300)
Put it in a loop to have it for more than two times
You can use the idea of .up() and .goto(x, y) and .down() to draw grids.
It seems like this is a problem that recursion could simplify in a fractal-like way. Each side of the initial hexagon is itself a hexagon, and so forth, filling the available space:
from turtle import Screen, Turtle
SIDE = 75 # pixels
def hexagon(side, depth):
if depth > 0:
for _ in range(6):
turtle.forward(side)
turtle.right(60)
hexagon(side, depth - 1)
turtle.left(120)
screen = Screen()
screen.tracer(False) # because I have no patience
turtle = Turtle()
turtle.penup()
turtle.width(2)
turtle.sety(-SIDE) # center hexagons on window
turtle.pendown()
turtle.left(30) # optional, orient hexagons
hexagon(SIDE, depth=6) # depth depends on coverage area
turtle.hideturtle()
screen.tracer(True)
screen.exitonclick()
How do you draw an inverted circle in Python using turtle only? i.e. a circle which is drawn clockwise rather than the traditional way of drawing one anticlockwise.
The current turtle is on the bottom of the vertically downwards line... How do I draw the indicated circle from there?
Your answer to your own question is incomplete. You clearly stated in the problem:
a circle which is drawn clockwise rather than the traditional way of
drawing one anticlockwise
So a better solution is:
x = turtle.Turtle()
x.right(90)
x.forward(100)
x.left(90)
x.circle(-100)
per the documentation:
Draw the arc in counterclockwise direction if radius is positive,
otherwise in clockwise direction.
Here is my example:
t = turtle.Turtle()
t.forward(100)
To draw a clockwise circle:
t.circle(-100)
I hope that can help you.
x=turtle.Turtle()
x.right(90)
x.forward(100)
x.right(90)
x.circle(100)
I was showing a grandson patterns drawn with Python's Turtle module,
and he asked to see concentric circles.
I thought it would be faster to use the turtle's circle() to draw them
than to write my own code for generating a circle. Ha! I am stuck.
I see that the circle produced begins its circumference at the turtle's
current location and its direction of drawing depends on turtle's current
direction of motion, but I can't figure out what I need to do to get
concentric circles.
I am not at this point interested in an efficient way of producing
concentric circles: I want to see what I have to do to get
this way to work:
def turtle_pos(art,posxy,lift):
if lift:
art.penup()
art.setposition(posxy)
art.pendown()
def drawit(tshape,tcolor,pen_color,pen_thick,scolor,radius,mv):
window=turtle.Screen() #Request a screen
window.bgcolor(scolor) #Set its color
#...code that defines the turtle trl
for j in range(1,11):
turtle_pos(trl,[trl.xcor()+mv,trl.ycor()-mv],1)
trl.circle(j*radius)
drawit("turtle","purple","green",4,"black",20,30)
You can do it like this:
import turtle
turtle.penup()
for i in range(1, 500, 50):
turtle.right(90) # Face South
turtle.forward(i) # Move one radius
turtle.right(270) # Back to start heading
turtle.pendown() # Put the pen back down
turtle.circle(i) # Draw a circle
turtle.penup() # Pen up while we go home
turtle.home() # Head back to the start pos
Which creates the picture below:
Basically it moves the turtle down one radius lenght to keep the center point for all the circles in the same spot.
From the documentation:
The center is radius units left of the turtle.
So wherever the turtle is when you start to draw a circle, the center of that circle is some distance to the right. After each circle, just move left or right some number of pixels and draw another circle whose radius is adjusted for the distance the turtle moved. For example, if you draw a circle with a radius of 50 pixels, then move right 10 pixels, you would draw another circle with a radius of 40, and the two circles should be concentric.
I am not at this point interested in an efficient way of producing
concentric circles: I want to see what I have to do to get this way to
work
To address the OP's question, the change to their original code to make it work is trivial:
turtle_pos(trl, [trl.xcor() + mv, trl.ycor() - mv], 1)
trl.circle(j * radius)
becomes:
turtle_pos(trl, [trl.xcor(), trl.ycor() - mv], 1)
trl.circle(j * mv + radius)
The complete code with the above fix and some style changes:
import turtle
def turtle_pos(art, posxy, lift):
if lift:
art.penup()
art.setposition(posxy)
art.pendown()
def drawit(tshape, tcolor, pen_color, pen_thick, scolor, radius, mv):
window = turtle.Screen() # Request a screen
window.bgcolor(scolor) # Set its color
#...code that defines the turtle trl
trl = turtle.Turtle(tshape)
trl.pencolor(pen_color)
trl.fillcolor(tcolor) # not filling but makes body of turtle this color
trl.width(pen_thick)
for j in range(10):
turtle_pos(trl, (trl.xcor(), trl.ycor() - mv), True)
trl.circle(j * mv + radius)
window.mainloop()
drawit("turtle", "purple", "green", 4, "black", 20, 30)
So now i am giving you the exact code that can draw concentric circles.
import turtle
t=turtle.Turtle()
for i in range(5):
t.circle(i*10)
t.penup()
t.setposition(0,-(i*10))
t.pendown()
turtle.done()