Python Julia集分形

Python Julia集分形,python,pygame,iteration,complex-numbers,fractals,Python,Pygame,Iteration,Complex Numbers,Fractals,我写了一个程序来生成Julia集分形。该程序还允许用户输入其c值或让程序生成一个随机值。代码如下:- import pygame, sys, math, cmath, random from pygame.locals import * print("Julia set fractal generator") custom = int(input("Do you want a custom set? Yes(1); No(-1): ")) if custom == -1: c = com

我写了一个程序来生成Julia集分形。该程序还允许用户输入其c值或让程序生成一个随机值。代码如下:-

import pygame, sys, math, cmath, random
from pygame.locals import *
print("Julia set fractal generator")
custom = int(input("Do you want a custom set? Yes(1); No(-1): "))
if custom == -1:
    c = complex((random.randint(-999,1000))/1000.0,(random.randint(-999,1000))/1000.0)
else:
    a = float(input("Real?: "))
    b = float(input("Imaginary?: "))
    c = complex(a,b)
lim = 4
limn = -4
mul = 0
iteration_detail = 100
screen = pygame.display.set_mode((512,512),0,32)
pygame.display.set_caption("Julia set fractal generator")
def iterate (px_i,py_i,iters):
    itnum = 1
    z = complex(((px_i-256)/512.0)*4,((py_i-256)/512.0)*4)
    while itnum <= iters:
        if z.real >= lim or z.imag >= lim or z.real <= limn or z.imag <= limn:
                break
        z = z**2 + c
        itnum += 1
return(z.real, z.imag, itnum)
def pixel_color_set (iterx, itery, iterations):
    pixel_color = (0,0,0)
    if iterx >= lim or itery >= lim or iterx <= limn or itery <= limn:
        if iterations < 2:
                pixel_color = (204,0,102)
        elif iterations == 2:
                pixel_color = (204,0,204)
        elif iterations == 3:
                pixel_color = (102,0,204)
        elif iterations ==4:
                pixel_color = (0,0,204)
        elif iterations ==5:
                pixel_color = (0,102,204)
        elif iterations ==6:
                pixel_color = (0,204,204)
        elif iterations ==7:
                pixel_color = (0,204,102)
        elif iterations ==8:
                pixel_color = (0,204,0)
        elif iterations ==9:
                pixel_color = (102,204,0)
return(pixel_color)
def draw_pixel (px, py, color):
return(screen.fill(color, ((px, py),(1, 1))))
while 1:
for event in pygame.event.get():
        if event.type == QUIT:
                pygame.quit()
                sys.exit()
        if event.type == KEYDOWN:
                if event.key == K_UP:
                        mul = 0.1
                elif event.key == K_DOWN:
                        mul = -0.1
                if event.key == K_SPACE:
                        pygame.image.save(screen, "fractal.jpg")
        if event.type == KEYUP:
                 if event.key == K_UP:
                        mul = 0
                 elif event.key == K_DOWN:
                        mul = 0
        c += mul
ypxl = 0
while ypxl < 512:
        xpxl = 0
        while xpxl < 512:
                ipxl = iterate(xpxl,ypxl,iteration_detail)
                cpxl = pixel_color_set(ipxl[0], ipxl[1], ipxl[2])
                draw_pixel(xpxl, ypxl, cpxl)
                xpxl += 1
        ypxl += 1
pygame.display.update()
导入pygame、sys、math、cmath、random
从pygame.locals导入*
打印(“Julia集分形生成器”)
custom=int(输入(“您想要自定义集吗?是(1);否(-1):”)
如果自定义==-1:
c=复数((random.randint(-9991000))/1000.0,(random.randint(-9991000))/1000.0)
其他:
a=浮点(输入(“实:”)
b=浮动(输入(“假想的:”)
c=复合物(a,b)
lim=4
limn=-4
mul=0
迭代_细节=100
screen=pygame.display.set_模式((512512),0,32)
pygame.display.set_标题(“Julia集分形生成器”)
def迭代(px_i,py_i,iters):
itnum=1
z=复数((px_i-256)/512.0)*4,((py_i-256)/512.0)*4)

当itnum=lim或z.imag>=lim或z.real=lim或iterx改变
像这样的像素颜色集时,得到了很好的结果。
从0到
iteration\u detail
的迭代被缩放到0到255的范围

def pixel_color_set (iterx, itery, iterations):
    pixel_color = (0,0,0)
    if iterx >= lim or itery >= lim or iterx <= limn or itery <= limn:
        RGB =int(math.sqrt(iterations) * int(255.0 / math.sqrt(iteration_detail)))
        # RGB = iterations * int(255.0/ iteration_detail)
        pixel_color = (RGB, RGB, RGB)
    return(pixel_color)
def像素颜色设置(iterx、itery、迭代):
像素颜色=(0,0,0)

如果iterx>=lim或itery>=lim或iterx,如果使用迭代计数的10除以的剩余部分来确定颜色,则也可以使用原始着色算法。但是,你必须先检查最大计数,以使内部始终为黑色。或者在计算循环后通过将迭代数设置为-1来对最大计数进行编码,这在着色方法中更容易检查

我的代码怎么了

没什么,你有好的形象,但你想要不同的形象。使新图像更改绘图算法。您要制作的图像是:

它是用逃逸时间制作的,而不是程序中的逃逸时间

因此,只需更改算法即可


HTH

这可能与缺少缩放机制有关。。分形是非常不同的取决于你放大的地方。查看youtube上缩放分形的视频。看起来你只是得到了一个不太有趣的位置。@turbo实际上,这是分形的最外层位置。如果我的程序运行正常,结果会与上图相似。嗯,可能你的迭代次数太少或太多?试着增加iterations@turbo我尝试了10次、50次、100次、1000次、10000次迭代,所有这些都给出了相同的结果:-/我该怎么感谢你呢?:)我希望我能做点什么来报答你。没有你的帮助我什么都做不了。。。谢谢没问题,这就是这个网站的目的:)我仍然需要做一个方法来缩放。。。有什么想法吗?