python与分形0013 - 超酷星型分形

分形介绍

分形是一个悖论。

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

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

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

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

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

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

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

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

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

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

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

分形结果

58c9d08c281d9cdf4e577c11b7915978.png
6f69bfa0251e66b5856857fa5101a75e.png
a4633df96d01cdb1ffd438b829773e80.png

分形源码

# 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 star(x,y,length,penc,fillc,colorful=False):
  if colorful:
    hx = min(x,y)
    hy = max(x,y)
    color = abs(hx / hy) if hy != 0 else 0
    (r,g,b) = colorsys.hsv_to_rgb(color,1,1)
    penc = (r,g,b)
    fillc=(r,g,b)
  turtle.up()
  turtle.goto(x,y)
  turtle.seth(90)
  turtle.fd(length)
  turtle.seth(180+36/2)
  L = length*math.sin(36*math.pi/180)/math.sin(54*math.pi/180)
  turtle.seth(180+72)
  turtle.down()
  turtle.fillcolor(fillc)
  turtle.pencolor(penc)
  turtle.begin_fill()
  for _ in range(5):
    turtle.fd(L)
    turtle.right(72)
    turtle.fd(L)
    turtle.left(144)
  turtle.end_fill()

def star_fractal(x,y,length,penc,fillc,n,colorful=False):
  if n==0:
    star(x,y,length,penc,fillc,colorful)
    return
  length2 = length/(1+(math.sin(18*math.pi/180)+1)/math.sin(54*math.pi/180))
  L = length-length2-length2*math.sin(18*math.pi/180)/math.sin(54*math.pi/180)
  for i in range(5):
    star_fractal(x+math.cos((90+i*72)*math.pi/180)*(length-length2),
                 y+math.sin((90+i*72)*math.pi/180)*(length-length2),
                 length2,penc,fillc,n-1,colorful)

time.sleep(6)

for _ in range(10):
  turtle.reset()
  turtle.hideturtle()
  turtle.bgcolor('black')
  turtle.speed(0)
  if not _%2:
    star_fractal(0,0, 400, "white", "white", _//2)
  else:
    star_fractal(0,0, 400, "white", "white", _//2, True)
  time.sleep(1)
  window.update()
857718ef8e51bb0520f52c384d9c0f34.gif

分形视频

328de6da10e6b01164ee6cd2881334ce.png 609556aecc5dc4f4093cb2c164eea30d.gif
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值