chrisleeming
chrisleeming

Reputation: 21

Koch's Snowflake using Turtle

I'm currently doing a project where I need to create Koch's Snowflake. I decided to use turtle for this assignment. The user can enter an "order" which determineres the amount of sides the triangle has. I'm having a problem where from a higher order like "6" the lines become so blurred on the triangle so I can't even see if its working. Is there a way to fix this? one solution I had would be if the user could zoom in they might be able the details more clearly.

Code :

import turtle
from turtle import \*
from turtle import Screen

t = turtle.Turtle()

turtle.tracer(False)

screen = Screen()

def draw_kochs(length, n):
   if (n == 0):
      t.forward(length)
   else:
      n = n - 1
      length = length / 3
      draw_kochs(length, n)
      t.right(60)
      draw_kochs(length, n)
      t.left(120)
      draw_kochs(length, n)
      t.right(60)
      draw_kochs(length, n)

t.penup()
t.setposition(0, 0)
t.pendown()
t.speed(10)
order = screen.textinput("Order Number", "Please Enter Order Number")
order = int(order)
order = order - 1
for x in range(3):
draw_kochs(200, order)
t.left(120)

input()

I tried to find others answers online but found very little support for turtle.

Upvotes: 0

Views: 2171

Answers (2)

dodrg
dodrg

Reputation: 1211

If it is blurred you should increase the size of the area. — Not by zooming in but by setting the count of pixels with turtle.screensize():

import turtle

# Get the current value (in Pixels)
print(turtle.screensize())

=> (300,300)

# Set a higher value:
turtle.screensize(canvwidth=600, canvheight=600, bg="white")

But note that more Pixels to calculate (an area goes by x²) can slow down the execution speed.

Addendum:

Surely it is no use to let the turtle walk the same small structure on a larger area. It should be obvious that the turtle should use the maximum of the available area to fulfill its task.

As a result the drawn structure covers more calculated pixels. So displaying this larger will not or less be blurred as more information (pixels) are avaliable to represent the structure.

Finally by doing this we've increased the "dpi" (Dots Per Inch) of the turtle's drawing.

=> That's been your question here.

Upvotes: 0

cdlane
cdlane

Reputation: 41905

This can be tricky in turtle as two functions that we'd want to implement zooming, setworldcoordinates() and screensize() don't play nicely together. Below is a simplified example using only setworldcoordinates(). Once the fractal is drawn, click on the window to keep enlarging a corner of it:

from turtle import Screen, Turtle

WIDTH, HEIGHT = 500, 500

def draw_kochs(length, n):
    if n == 0:
        turtle.forward(length)
    else:
        n -= 1
        length /= 3

        draw_kochs(length, n)
        turtle.right(60)
        draw_kochs(length, n)
        turtle.left(120)
        draw_kochs(length, n)
        turtle.right(60)
        draw_kochs(length, n)

adjust = 2

def zoom(x, y):
    global adjust

    adjust += 1
    screen.setworldcoordinates(-WIDTH/adjust, -HEIGHT/adjust, WIDTH/adjust, HEIGHT/adjust)

screen = Screen()
screen.setup(WIDTH, HEIGHT)

order = screen.numinput("Order Number", "Please Enter Order Number:", minval=1, maxval=10)

screen.setworldcoordinates(-WIDTH/adjust, -HEIGHT/adjust, WIDTH/adjust, HEIGHT/adjust)  # initial null change
screen.tracer(False)

turtle = Turtle()
turtle.hideturtle()

for _ in range(3):
    draw_kochs(200 * order, order)
    turtle.left(120)

screen.update()
screen.onclick(zoom)
screen.mainloop()

To move around in an enlarged fractal, we'd want screensize(), which again doesn't play well with setworldcoordinates(). To get around this, we have to implement our own scalable coordinate functionality, toss setworldcoordinates() and use screensize() to create a large backing image store to scroll around in.

Upvotes: 0

Related Questions