Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 实施科赫曲线?_Python_Fractals - Fatal编程技术网

Python 实施科赫曲线?

Python 实施科赫曲线?,python,fractals,Python,Fractals,我在维基百科上查看科赫雪花()的页面,所有的例子都是logo/turtle样式,这让我很烦恼。所以我开始制作我自己的,返回一个列表或坐标 我的实现是用python实现的,我基本上借鉴了python turtle实现,但用基本trig替换了turtle特有的东西。这导致了一些丑陋的代码。我对您的挑战是要么改进我的代码,要么提出您自己的更简洁的解决方案。它可以是python,也可以是您最喜欢的语言 我的代码: from math import sin, cos, radians def grow(

我在维基百科上查看科赫雪花()的页面,所有的例子都是logo/turtle样式,这让我很烦恼。所以我开始制作我自己的,返回一个列表或坐标

我的实现是用python实现的,我基本上借鉴了python turtle实现,但用基本trig替换了turtle特有的东西。这导致了一些丑陋的代码。我对您的挑战是要么改进我的代码,要么提出您自己的更简洁的解决方案。它可以是python,也可以是您最喜欢的语言

我的代码:

from math import sin, cos, radians

def grow(steps, length = 200, startPos = (0,0)):
    angle = 0
    try:
        jump = float(length) / (3 ** steps)
    except:
        jump = length

    set="F"
    for i in xrange(steps): set=set.replace("F", "FLFRFLF")

    coords = [startPos]
    for move in set:
        if move is "F": 
            coords.append(
              (coords[-1][0] + jump * cos(angle),
               coords[-1][1] + jump * sin(angle)))
        if move is "L":
            angle += radians(60)
        if move is "R":
            angle -= radians(120)

    return coords

编辑:由于懒惰的复制,我忘记了导入

我不认为它特别难看,我只会逐步地重构它,例如,作为第一步(我已经删除了
try/except
,因为我不知道你想要避免什么…如果它需要返回,它应该更明确一点,IMHO):


如果第二步得到保证,我可能会将此功能汇总到一个类中,但我不确定这是否会使事情实质上更好(或者,实际上更好;-)。

Mathematica在数学方面更出色:

points = {{0.0, 1.0}};
koch[pts_] := Join[
    pts/3,
    (RotationMatrix[60 Degree].#/3 + {1/3, 0}) & /@ pts,
    (RotationMatrix[-60 Degree].#/3 + {1/2, 1/Sqrt[12]}) & /@ pts,
    (#/3 + {2/3, 0}) & /@ pts
];
Graphics[Line[Nest[koch, points, 5]], PlotRange -> {{0, 1}, {0, 0.3}}] //Print

我非常喜欢你的问题,因此我将其作为一个新问题发布了一个答案,以便其他人可以改进它:

我没有使用标志/海龟之类的东西,也没有使用三角法


祝贺你是第一个将这个问题带到StackOverflow的人

> P> >如果不考虑你的实现,然后测试你的实现,那么Python龟可以记录它正在做的事情并给你回坐标。在要录制的代码周围使用
begin_poly()
end_poly()
,然后使用
get_poly()
获得分数

在本例中,我将根据绘制雪花,然后将这些坐标注册回新的海龟形状,我将在屏幕上随机(快速)标记:

import turtle
from random import random, randrange

def koch_curve(turtle, steps, length):
    if steps == 0:
        turtle.forward(length)
    else:
        for angle in [60, -120, 60, 0]:
            koch_curve(turtle, steps - 1, length / 3)
            turtle.left(angle)

def koch_snowflake(turtle, steps, length):
    turtle.begin_poly()

    for _ in range(3):
        koch_curve(turtle, steps, length)
        turtle.right(120)

    turtle.end_poly()

    return turtle.get_poly()

turtle.speed("fastest")

turtle.register_shape("snowflake", koch_snowflake(turtle.getturtle(), 3, 100))

turtle.reset()

turtle.penup()

turtle.shape("snowflake")

width, height = turtle.window_width() / 2, turtle.window_height() / 2

for _ in range(24):
    turtle.color((random(), random(), random()), (random(), random(), random()))
    turtle.goto(randrange(-width, width), randrange(-height, height))
    turtle.stamp()

turtle.done()
如果不希望用户看到此步骤,可以在多边形生成过程中隐藏画笔和海龟


“set”在python中是一种内置类型,实际上,当我想到更好的实现时,我最终重写了一个类似于Turtle的类。也不必每次计算jump*cos(角度)和jump*sin(角度),因为知道只有6个可能的角度……感谢您的解决方案。唯一的办法是我装聋作哑,忘了把它拿出来。最初是为了捕捉零除法错误。我注意到这有很好的速度提升!一个学习的好剧本!
import turtle
from random import random, randrange

def koch_curve(turtle, steps, length):
    if steps == 0:
        turtle.forward(length)
    else:
        for angle in [60, -120, 60, 0]:
            koch_curve(turtle, steps - 1, length / 3)
            turtle.left(angle)

def koch_snowflake(turtle, steps, length):
    turtle.begin_poly()

    for _ in range(3):
        koch_curve(turtle, steps, length)
        turtle.right(120)

    turtle.end_poly()

    return turtle.get_poly()

turtle.speed("fastest")

turtle.register_shape("snowflake", koch_snowflake(turtle.getturtle(), 3, 100))

turtle.reset()

turtle.penup()

turtle.shape("snowflake")

width, height = turtle.window_width() / 2, turtle.window_height() / 2

for _ in range(24):
    turtle.color((random(), random(), random()), (random(), random(), random()))
    turtle.goto(randrange(-width, width), randrange(-height, height))
    turtle.stamp()

turtle.done()