首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python与分形0013 - 超酷星型分形

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

作者头像
滚神大人
发布2021-12-02 14:13:19
3910
发布2021-12-02 14:13:19
举报
文章被收录于专栏:趣Python趣Python

分形介绍

分形是一个悖论。

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

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

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

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 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()
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-12-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 趣Python 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 分形介绍
  • 分形结果
  • 分形源码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档