1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

コッホ曲線をPython Turtleで描画する

1
Last updated at Posted at 2015-03-30

プログラミングコンテスト攻略のためのアルゴリズムとデータ構造参考にしてコッホ曲線を書いてみた

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()


1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?