python与分形0013 - 超酷星型分形
2023-03-31 10:33:33 时间
分形介绍
分形是一个悖论。
它惊人的简单,却又无限的复杂。
它很新,却又比尘埃更古老。
分形是什么?它们是从哪里来的?我们为什么要在乎?
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()
相关文章
- 大数据除了Hadoop,还有Scrapy
- 总结:常用的 Python 爬虫技巧
- 几分钟快速入门Python
- 用Python端对端数据分析识别机器人“僵尸粉”
- 用Python挖掘Twitter数据:数据采集
- 5招教你用Python构建好玩的深度学习应用
- Python树莓派编程2.7 总结
- Python树莓派编程第3章
- Python树莓派编程3.4.3 函数
- 机器人系统设计与制作:Python语言实现导读
- R、Python、Scala 和 Java,到底该使用哪一种大数据编程语言?
- LinkedIn开源Dr. Elephant:Hadoop和Spark性能监控工具
- RDD、DataFrame和DataSet的区别是什么
- Python数据可视化1.1 数据、信息、知识和观点
- Python数据可视化1.2 数据转换
- Python数据可视化1.3 数据可视化历史
- Python数据可视化1.4 可视化如何帮助决策
- 这些数据科学技能,才是老板们最想要的
- Python数据可视化1.5 可视化图像
- Python数据可视化1.6 总结