zl程序教程

您现在的位置是:首页 >  Python

当前栏目

python与分形0014 - 超酷2D变3D方形螺旋

2023-04-18 14:55:39 时间

分形介绍

分形是一个悖论。

它惊人的简单,却又无限的复杂。

它很新,却又比尘埃更古老。

分形是什么?它们是从哪里来的?我们为什么要在乎?

20世纪非传统的数学家Benoit Mandelbrot在1975年从拉丁词fractus(意思是不规则的或破碎的)创造了分形这个词。

我们周围到处都可以看到分形的影子。

从最基本的角度看,分形是重复模式或公式的视觉表达,开始时很简单,然后逐渐变得更复杂。

在数学中,分形是欧氏空间的子集,其分形维数严格超过其拓扑维数。

分形在不同的尺度上表现相同,如Mandelbrot集合的连续放大。

分形通常在越来越小的尺度上表现出类似的模式,这种特性称为自相似性,也称为扩展对称或展开对称。

如果这种复制在每个尺度上都完全相同,就像在门格尔海绵中一样,那么它就被称为仿射自相似。

分形几何属于度量理论的数学分支。

分形结果

黑白

红绿

分形源码

# coding: utf-8

import turtle
import math
import random
import time
import colorsys

window = turtle.Screen()
window.screensize()
window.setup(width=1.0, height=1.0, startx=None, starty=None)

turtle.speed(0)
turtle.hideturtle()
#turtle.tracer(0)
turtle.bgcolor('black')

def draw_square(x,y,direction,length,c):
    turtle.color(c)
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(direction)
    turtle.back(length/2)
    turtle.left(90)
    turtle.back(length/2)
    turtle.seth(direction)
    turtle.down()
    turtle.fillcolor(c)
    turtle.begin_fill()
    for _ in range(4):
        turtle.fd(length)
        turtle.left(90)
    turtle.end_fill()

colors = ["white", "black"]
#colors = ["lawngreen", "red"]

index = 1

def square_spiral(x,y,direction,length):
    if length < 5: return
    global index
    draw_square(x,y,direction,length, colors[1-index])
    index = 1-index
    square_spiral(x,y,direction+alpha,length/(math.sin(math.radians(alpha)) + math.cos(math.radians(alpha))))

time.sleep(6)
alpha=5
square_spiral(0,0,0,800)