2 Star 0 Fork 0

mirrors_beltoforion/recreational_mathematics_with_python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
chaos_game_rgb.py 2.19 KB
一键复制 编辑 原始数据 按行查看 历史
Ingo Berg 提交于 2023-01-16 23:10 . update
import pygame
import random
import math
from pygame.locals import *
idx = [0, 0, 0]
def mark_pixel(surface, pos, plane):
col = surface.get_at(pos)
v = min(col[plane] + 4, 255)
surface.set_at(pos, (v if plane == 0 else col[0],
v if plane == 1 else col[1],
v if plane == 2 else col[2]))
def random_point_index(p):
if len(p) <= 3:
return random.randint(0, len(p) - 1)
global idx
idx[2] = idx[1]
idx[1] = idx[0]
dst1 = abs(idx[1] - idx[2])
while True:
idx[0] = random.randint(0, len(p) - 1)
dst = abs(idx[0] - idx[1])
if dst1 == 0 and (dst == 1 or dst == len(p) - 1):
continue
else:
break
return idx[0]
def init_polygon(width, height, n):
delta_angle = 360/n
r = width/2 - 10
p = []
for i in range(0, n):
angle = (180 + i*delta_angle) * math.pi / 180
p.append((width/2 + r*math.sin(angle), height/2 + r*math.cos(angle)))
return p
def main(width, height, n, r):
pygame.init()
surface = pygame.display.set_mode((width, height))
pygame.display.set_caption('Das Chaos Spiel')
p = init_polygon(width, height, n)
x = [random.randint(0, width), random.randint(0, width), random.randint(0, width)]
y = [random.randint(0, height), random.randint(0, height), random.randint(0, height)]
plane_randomness = [0.02, 0.04, 0.08]
step = 0
while True:
step = step + 1
i = random_point_index(p)
for plane_idx in (range(0,3)):
rr = random.random() * plane_randomness[plane_idx]
x[plane_idx] += (p[i][0] - x[plane_idx]) * (r + rr)
y[plane_idx] += (p[i][1] - y[plane_idx]) * (r + rr)
if 0 <= x[plane_idx] <= width and 0 <= y[plane_idx] <= height:
mark_pixel(surface, (int(x[plane_idx]), int(y[plane_idx])), plane_idx)
if step % 5000 == 0:
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.image.save(surface, 'chaosspiel.jpg')
pygame.quit()
return
if __name__ == "__main__":
n = 5; main(800, 800, n, 0.45)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_beltoforion/recreational_mathematics_with_python.git
git@gitee.com:mirrors_beltoforion/recreational_mathematics_with_python.git
mirrors_beltoforion
recreational_mathematics_with_python
recreational_mathematics_with_python
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385