Draw Koch curve with Python Turtle

I tried to draw a Koch curve with reference to the algorithm and data structure for capturing the programming contest

Screenshot 2015-03-30 11.12.56.png

from math import cos, sin, pi

class Point:
    def __init__(self, x=0.0, y=0.0):
        self.x = x
        self.y = y

def koch_pos(n, a, b):
    if n == 0:
        return
    th = pi * 60.0 / 180.0

    s, t, u = Point(0.0, 0.0), Point(0.0, 0.0), Point(0.0, 0.0)
    s.x = (2.0 * a.x + 1.0 * b.x) / 3.0
    s.y = (2.0 * a.y + 1.0 * b.y) / 3.0
    t.x = (1.0 * a.x + 2.0 * b.x) / 3.0
    t.y = (1.0 * a.y + 2.0 * b.y) / 3.0
    u.x = (t.x - s.x) * cos(th) - (t.y - s.y) * sin(th) + s.x
    u.y = (t.x - s.x) * sin(th) + (t.y - s.y) * cos(th) + s.y

    for pos in koch_pos(n - 1, a, s):
        yield pos
    yield s.x, s.y
    for pos in koch_pos(n - 1, s, u):
        yield pos
    yield u.x, u.y
    for pos in koch_pos(n - 1, u, t):
        yield pos
    yield t.x, t.y
    for pos in koch_pos(n - 1, t, b):
        yield pos


def koch(n, a, b):
    yield a.x, a.y
    for pos in koch_pos(n, a, b):
        yield pos
    yield b.x, b.y

if __name__ == "__main__":
    from turtle import *
    a = Point(-500.0, 0.0)
    b = Point(500.0, 0.0)
    n = 5
    t = Turtle()

    first = True
    for x, y in koch(n, a, b):
        if first:
            first = False
            t.penup()
            t.setpos(x, y)
            t.pendown()
        else:
            t.setpos(x, y)

    exitonclick()


Recommended Posts

Draw Koch curve with Python Turtle
Try to draw a life curve with python
Draw netCDF file with python
[Python] Curve fitting with polynomial
Draw an illustration with Python + OpenCV
Draw Lyapunov Fractal with Python, matplotlib
Draw arrows (vectors) with opencv / python
[Python] Drawing a swirl pattern with turtle
Draw Nozomi Sasaki in Excel with python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Play with 2016-Python
Tested with Python
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
[Python] Draw a directed graph with Dash Cytoscape
[Python] How to draw multiple graphs with Matplotlib
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
[Python] Draw a Qiita tag relationship diagram with NetworkX
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
[Python] How to draw a line graph with Matplotlib
1.1 Getting Started with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
I tried to draw a route map with Python
infomap python draw code
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python