Julia集分形

2024-10-02 10:22:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个程序来生成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()

代码确实有效,但并没有像预期的那样生成分形。例如这个分形:-
z = z**2 + c其中c等于 c = complex(-0.1, 0.651) 应该是这样的 From Wikipedia

但看起来是这样的
Generated by the program

我的代码有什么问题?我也不能做一个缩放机制。。。如果有人帮忙,我将不胜感激。在


Tags: orkeyeventifpygamecolorpixelset
3条回答

如果使用迭代计数除以10的余数来决定颜色,也可以使用原始的着色算法。但是你得先检查一下最大计数,这样里面总是黑色的。或者在计算循环后通过将迭代次数设置为-1来编码最大计数,这在着色方法中更容易检查。在

像这样改变the pixel_color_set会产生很好的效果。
从0到iteration_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)

我认为带有math.sqrt的那一个看起来更好一些,但是尝试两者并选择你想要的。在

Whats wrong with my code ?

没什么,你有很好的形象,但你想要不同的形象。你必须改变绘图算法。 您要制作的图像是:Julia set for fc(z)=z*z + c and c=-0.1+0.651. Made using DEM/J

它是用distance estimation生成的,而不是程序中的转义时间

所以只需改变算法

高温

相关问题 更多 >

    热门问题