I have a beginning star. Now, how would I make this into a fractal?
import turtle
turing = turtle.Turtle()
for i in range(5):
turing.forward(110)
turing.left(216)
A fractal is something that repeats with some variation. So put your star-loop code into a loop and repeat it several times. Change something after doing each star-loop. You could change where the turtle is, or what angle it is pointing at, or how long the side of the next star will be, or any or all of these.
Following #mgkrebbs general philosophy, with a simple fractal that deflects a line, we can make all the lines of the deflection smaller duplicates of the fractal. Your star is tricky to work with but since it has verticies, we can recursively put smaller stars at each vertex:
from turtle import Turtle, Screen
def star(turtle, length, depth):
turtle.left(90)
for _ in range(5):
turtle.forward(length)
heading = turtle.heading()
if depth > 1:
star(turtle, length / 2, depth - 1)
turtle.setheading(heading)
turtle.left(216)
turing = Turtle()
turing.speed("fastest")
star(turing, 180, 3)
turing.hideturtle()
screen = Screen()
screen.exitonclick()
As the depth increases, you can see the stars start to overlap -- making the image larger by increasing length, or making the recursions a smaller fraction of the length, may help.
OUTPUT
Related
Why don't the iterating polygons not align perfectly. (if I try to make a polygon with 4 sides, it works fine, but any other shape and it aligns a bit differently). Is it something to do from line 11 to line 16?
This is the question I am trying to solve with my function
import turtle
t = turtle.Turtle()
t.speed(5)
def draw_shape(length, sides, colores, times):
for timestotal in range(1, times+1):
for side in range(sides):
t.color(colores)
t.forward(length*timestotal)
t.right(360/sides)
t.penup()
t.back(length*2)
t.left(360/sides)
t.forward(length*2)
t.right(360/sides)
t.pendown()
draw_shape(4, 8, "red", 8)
It doesn't really matter if it is ascending or descending lengths as long as all the shapes are centered as is in the exercise.
Unfortunately if the parameter is anything other than 4 the shapes do not align properly
If I pass different commands between penup() and pendown() for each number from 3 to 13 using if, elif, and else statements, it does align the shapes but each number(length) needs its own sets of code:
Could it be something around this command:
t.goto(-(lengthtimestotal)/2,(lengthtimestotal)/2)
It seems like the problem can be reduced to "draw a regular polygon of n sides from a center point". If you can do that, then you need only iterate with different sizes in the outer loop, all drawing from the same point (the turtle's current location).
I don't think it's easy to draw a regular polygon from a center point using only forward, backward and turning commands. But it's possible to use the classic trig polygon approach to compute the vertices of the polygon around the circle:
import turtle
from math import cos, radians, sin
def draw_shape(length, color, sides, times):
t.color(color)
x, y = t.pos()
side = 360 // sides
for i in range(times):
current_length = length // times * (1 + i)
t.penup()
for angle in range(0, 361, side):
t.goto(
current_length * cos(radians(angle - side / 2)) + x,
current_length * sin(radians(angle - side / 2)) + y
)
t.pendown()
t = turtle.Turtle()
t.speed(5)
draw_shape(length=100, color="red", sides=8, times=5)
turtle.exitonclick()
The angle - side / 2 bit is used to rotate the polygon by half a side to match the spec.
I also see draw_shape(100, "blue", 4, 3) has unusual output. You can get this with current_length = length - (20 * i) which hardcodes the step size. Not very pleasant to have to do, but such it is.
I am drawing a heptagon using this code:
tegan.setheading(0);
for i in range (7):
tegan.right(51.43)
tegan.forward(100)
However, this code always draws the shape with a flat edge at the top and I want the point at the top. What am I doing wrong?
To make an heptagon, you need to rotate 360°/7 each step. To have the point up, you rotate only half on the first step.
One trick to do this is to rotate in one direction (here left) half the angle, and then proceed in the other direction with the full angle seven times. Another possibility is to write a condition in the loop for the first step, or split the code in two.
import turtle
angle = 360/7
turtle.left(angle/2)
for i in range(7):
turtle.right(angle)
turtle.forward(100)
Alternatively, you can simply do:
import turtle
turtle.circle(-116, steps=7)
turtle.done()
So I'm reading a book to learn python and I got to a part about the module turtle.
So after explaining it, it gives you some exercises.
One of them is to define a function that creates regular polygons.
I got this to work.
import turtle
bob = turtle.Turtle()
def polygon(t, l, n):
angle = 360/n
for i in range(n):
t.fd(l)
t.lt(angle)
polygon(bob, 40, 5)
For example this draws a regular pentagon.
The next exercise asks you to draw a "circle" changing the number of sides of the polygon.
The problem is that sometimes it doesn't work and the polygon/circle doesn't close.
I tried to find the solution by changing lots of time both the lenght and the number of sides or only one of the two but I didn't succeed.
For example, lenght = 10 and n°sides = 140 doesn't work, instead lenght = 20 and n°sides = 120 works.
Can someone explain, please?
Found solution.
Being a beginner I forgot about integers and floats.
That's why the "circle" wasn't closing.
Your code works fine in Python 3 but didn't close the polygon in Python 2 due to the difference in how division works. The fix is to simply use 360.0 instead of 360 and then it works fine in both:
from turtle import Turtle, Screen
def polygon(t, l, n):
angle = 360.0 / n
for _ in range(n):
t.fd(l)
t.lt(angle)
bob = Turtle()
polygon(bob, 10, 140)
screen = Screen()
screen.exitonclick()
Python turtle's own circle() method actually draws polygons with the default assumption that 60 sides is sufficient to look like a circle on the screen. Unless the circle is very small (then it uses fewer sides) or the user insists on more (or less) sides via the steps argument.
Try putting 360.0 instead of 360, because the initial value of Python is in integers.
We want to convert it into decimals, that's why we put the .0 after the 360.
Here is the code In the book Thinkpython 2e.
import turtle
import math
bob = turtle.Turtle()
def polygon(t, n, length):
angle = 360 / n
for i in range(n):
t.fd(length)
t.lt(angle)
def circle(t, r):
circumference = 2 * math.pi * r
n = 50
length = circumference / n
polygon(t, n, length)
circle(bob,50)
turtle.mainloop()
I don't understand how it is possible to be a circle, I think it will be a 50-sides polygon, am i right?
A circle has infinitely many points, a screen has finitely many pixels. You are correct that you can't draw true circles on a screen. This isn't to say that drawing a polygon is the only way to approximate a circle on the screen. As #Qwerty rightly points out in the comments you can also do so with trig functions.
Nevertheless, approximating circles by polygons is an ancient approach and was the classical way in which pi was approximated. Also -- it is a fun exercise for turtles.
I have not programmed in python in a while (specifically with the turtle libraries) but if I remember, there is a way easier
import turtle
circumfrence = 80
turtle = turtle.Turtle()
turtle.shape("circle")
turtle.circle(circumfrence / 2)
It's that Simple!
The odd part about your circle() function to me is that n is fixed at 50. At the extremes of large and small circles, this might not be optimal and maybe should be more dynamic. As far as a 50-sided polygon vs. a circle, let's test using the turtle.circle() command:
from turtle import Turtle, Screen
radius = 100
sides = 50
bob = Turtle(shape="turtle")
bob.width(2)
bob.pencolor("red")
bob.circle(radius)
bob.pencolor("green")
bob.circle(radius, steps=sides)
bob.hideturtle()
screen = Screen()
screen.exitonclick()
The turtle.circle() method uses a polygon approximation but it computes the number of sides as a function of the radius with a maxium of 60. For the radius of 100 above, it actually uses only 28 steps so our 50-sided polygon is potentially more accurate!
Write a function named fatLine(). The function fatLine() takes
three parameters:
a turtle, t
an integer, segments, that is the number of segments in the line that is drawn
an integer, increment, that is how much wider each successive segment of the line is
The function fatLine() should use the turtle t to draw a line
composed of connected segments. Each segment should be of length 50.
The width of the first line segment should be (the parameter)
increment, and each successive segment should be wider than the
preceding segment by increment. For example, if segments = 5 and
increment = 10, the following is correct output
I tried coding this with three parameters and i am not sure how else i would make it run. Any help would be great Thanks.
I am trying to code this in idle but i have no luck. Please help with any idea on how to execute.
Since it's been five years, it's time this question, despite not showing coding effort, had an answer:
from turtle import Screen, Turtle
def fatLine(t, segments, increment):
width = increment
for _ in range(segments):
t.width(width)
t.forward(50)
width += increment
screen = Screen()
turtle = Turtle(visible=False)
turtle.penup()
turtle.backward(200)
turtle.pendown()
fatLine(turtle, 8, 10)
screen.exitonclick()