我正在编写一个燃烧的船分形的东西,当放大到很远时,它在 pygame 窗口中显示,我认为这是因为当你放大 z 的精度或某些东西必须更高,这需要更多的小数这需要更多的内存,如果我对错误的看法是正确的,你怎么能增加给浮点数的内存或表示更精确的数字,我的缩放代码也能工作但不是真的你怎么能解决这个问题
import pygame
import numpy as np
from PIL import Image
from numba import jit
from tqdm import tqdm
################################################################################################################################################
#################################################################### pygame ####################################################################
################################################################################################################################################
@jit(nopython=True)
def color_value_ajust(color_value):
loops = color_value // 765
if loops % 2 == 0:
color_value = color_value - 765 * loops
else:
color_value = 765 + (765 * loops - color_value)
return color_value
def plot_fractal(max_iter, res_decrease_factor, width, height, screen, x_deviation, y_deviation, zoom, zoom_multiplyer, step):
x_min, x_max = -2.0, 2.0
y_min, y_max = -2.0, 2.0
for x in range(0, width, res_decrease_factor):
for y in range(0, height, res_decrease_factor):
c = complex((x_min + ((x + x_deviation * zoom * 2) / width) * (x_max - x_min)) / zoom, (y_min + ((y + y_deviation * zoom) / width) * (y_max - y_min)) / zoom)
z = c
for i in range(max_iter):
if abs(z) > 2.0:
break
z = (abs(z.real) + abs(z.imag)*1j)**2 + c
if i == max_iter - 1:
color = (0, 0, 0)
else:
color_value = color_value_ajust(i * 10)
r = min(color_value, 255)
g = max(min(color_value - 255, 255), 0)
b = max(min(color_value - 510, 255), 0)
color = (r, g, b)
screen.set_at((x, y), color)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
(mouseX, mouseY) = pygame.mouse.get_pos()
x_position = mouseX - width/2
y_position = mouseY - height/2
zoom *= zoom_multiplyer
x_deviation += x_position/zoom/2
y_deviation += y_position/zoom*2
screen.fill((0, 0, 0))
return True, zoom, x_deviation, y_deviation, zoom_multiplyer, step, res_decrease_factor, max_iter
if event.type == pygame.KEYDOWN:
keys = pygame.key.get_pressed()
if keys[pygame.K_LSHIFT] and keys[pygame.K_z] and keys[pygame.K_UP]:
zoom_multiplyer += 0.1
print("zoom_multiplyer increased to " + str(zoom_multiplyer))
elif keys[pygame.K_z] and keys[pygame.K_UP]:
zoom_multiplyer += 1
print("zoom_multiplyer increased to " + str(zoom_multiplyer))
elif keys[pygame.K_r] and keys[pygame.K_UP]:
if res_decrease_factor > 1:
res_decrease_factor -= 1
print("resolution increased to " + "1/" + str(res_decrease_factor))
return True, zoom, x_deviation, y_deviation, zoom_multiplyer, step, res_decrease_factor, max_iter
elif keys[pygame.K_s] and keys[pygame.K_UP]:
step += 10
print("step increased to " + str(step))
elif keys[pygame.K_i] and keys[pygame.K_UP]:
max_iter += 10
print("iteration depth increased to " + str(max_iter))
return True, zoom, x_deviation, y_deviation, zoom_multiplyer, step, res_decrease_factor, max_iter
elif keys[pygame.K_UP]:
y_deviation -= step/zoom
return True, zoom, x_deviation, y_deviation, zoom_multiplyer, step, res_decrease_factor, max_iter
if keys[pygame.K_LSHIFT] and keys[pygame.K_z] and keys[pygame.K_DOWN]:
if zoom_multiplyer > 1:
zoom_multiplyer -= 0.1
print("zoom_multiplyer decreased to " + str(zoom_multiplyer))
elif keys[pygame.K_z] and keys[pygame.K_DOWN]:
if zoom_multiplyer > 1:
zoom_multiplyer -= 1
print("zoom_multiplyer decreased to " + str(zoom_multiplyer))
elif keys[pygame.K_r] and keys[pygame.K_DOWN]:
res_decrease_factor += 1
print("resolution decreased to " + "1/" + str(res_decrease_factor))
screen.fill((0, 0, 0))
return True, zoom, x_deviation, y_deviation, zoom_multiplyer, step, res_decrease_factor, max_iter
elif keys[pygame.K_s] and keys[pygame.K_DOWN]:
if step > 10:
step -= 10
print("step decreased to " + str(step))
elif keys[pygame.K_i] and keys[pygame.K_DOWN]:
if max_iter > 10:
max_iter -= 10
print("iteration depth decreased to " + str(max_iter))
return True, zoom, x_deviation, y_deviation, zoom_multiplyer, step, res_decrease_factor, max_iter
elif keys[pygame.K_DOWN]:
y_deviation += step/zoom
return True, zoom, x_deviation, y_deviation, zoom_multiplyer, step, res_decrease_factor, max_iter
if keys[pygame.K_LEFT]:
x_deviation -= step/zoom
return True, zoom, x_deviation, y_deviation, zoom_multiplyer, step, res_decrease_factor, max_iter
if keys[pygame.K_RIGHT]:
x_deviation += step/zoom
return True, zoom, x_deviation, y_deviation, zoom_multiplyer, step, res_decrease_factor, max_iter
if keys[pygame.K_RETURN]:
pil(x_deviation, y_deviation, zoom)
return False, zoom, x_deviation, y_deviation, zoom_multiplyer,step, res_decrease_factor, max_iter
################################################################################################################################################
##################################################################### PIL ######################################################################
################################################################################################################################################
@jit(nopython=True)
def jpg(max_iter, width, height, x_deviation, y_deviation, zoom, x, x_min, x_max, y_min, y_max, image):
for y in range(height):
c = complex((x_min + ((x + x_deviation * zoom * 2) / width) * (x_max - x_min)) / zoom, (y_min + ((y + y_deviation * zoom) / width) * (y_max - y_min)) / zoom)
z = c
for i in range(max_iter):
if abs(z) > 2.0:
break
z = (abs(z.real) + abs(z.imag)*1j)**2 + c
if i == max_iter - 1:
image[y,x,:] = [0, 0, 0]
else:
color_value = color_value_ajust(i * 10)
r = min(color_value, 255)
g = max(min(color_value - 255, 255), 0)
b = max(min(color_value - 510, 255), 0)
image[y,x,:] = [r, g, b]
return image
def pil(x_deviation, y_deviation, zoom):
x_deviation = x_deviation/1600
y_deviation = y_deviation/990
resolution = int(input("Enter resolution (default=1000): ") or "1000")
width, height = int(1000 * resolution / 1000), int(618.75 * resolution / 1000)
x_deviation = x_deviation*width
y_deviation = y_deviation*height
max_iter = int(input("Enter max iteration depth (default=100): ") or "100")
image = np.zeros((height, width, 3), dtype=np.uint8)
x_min, x_max = -2.0, 2.0
y_min, y_max = -2.0, 2.0
for x in tqdm(range(width), total=width):
image = jpg(max_iter, width, height, x_deviation, y_deviation, zoom, x, x_min, x_max, y_min, y_max, image)
img_new = Image.fromarray(image)
img_new.save('stuff.png')
print("\ncomplete\n")
img_new.show()
################################################################################################################################################
##################################################################### main #####################################################################
################################################################################################################################################
def main():
point = input("Do you want a center point Y/N (defalt=N): ") or "N"
max_iter = int(input("Enter max iteration depth (default=100): ") or "100")
res_decrease_factor = int(input("Enter what factor you want to decrease resolution by (defalt=2): ") or "2")
pygame.init()
width = 1600
height = 990
x_deviation = 0
y_deviation = 305
zoom = 1
zoom_multiplyer = 2
step = 100
screen = pygame.display.set_mode((width, height))
if point == "Y" or point == "y":
screen.set_at((800, 495), (0, 255, 0))
pygame.display.flip()
while True:
is_zooming, zoom, x_deviation, y_deviation, zoom_multiplyer, step, res_decrease_factor, max_iter = plot_fractal(max_iter, res_decrease_factor, width, height, screen, x_deviation, y_deviation, zoom, zoom_multiplyer, step)
while is_zooming == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
(mouseX, mouseY) = pygame.mouse.get_pos()
x_position = mouseX - width/2
y_position = mouseY - height/2
x_deviation += x_position/zoom/2
y_deviation += y_position/zoom*2
zoom *= 2
screen.fill((0, 0, 0))
is_zooming = True
elif event.type == pygame.KEYDOWN:
keys = pygame.key.get_pressed()
if keys[pygame.K_LSHIFT] and keys[pygame.K_z] and keys[pygame.K_UP]:
zoom_multiplyer += 0.1
print("zoom_multiplyer increased to " + str(zoom_multiplyer))
elif keys[pygame.K_z] and keys[pygame.K_UP]:
zoom_multiplyer += 1
print("zoom_multiplyer increased to " + str(zoom_multiplyer))
elif keys[pygame.K_r] and keys[pygame.K_UP]:
if res_decrease_factor > 1:
res_decrease_factor -= 1
print("resolution increased to " + "1/" + str(res_decrease_factor))
is_zooming = True
elif keys[pygame.K_s] and keys[pygame.K_UP]:
step += 10
print("step increased to " + str(step))
elif keys[pygame.K_i] and keys[pygame.K_UP]:
max_iter += 10
print("iteration depth increased to " + str(max_iter))
is_zooming = True
elif keys[pygame.K_UP]:
y_deviation -= step/zoom
is_zooming = True
if keys[pygame.K_LSHIFT] and keys[pygame.K_z] and keys[pygame.K_DOWN]:
if zoom_multiplyer > 1:
zoom_multiplyer -= 0.1
print("zoom_multiplyer decreased to " + str(zoom_multiplyer))
elif keys[pygame.K_z] and keys[pygame.K_DOWN]:
if zoom_multiplyer > 1:
zoom_multiplyer -= 1
print("zoom_multiplyer decreased to " + str(zoom_multiplyer))
elif keys[pygame.K_r] and keys[pygame.K_DOWN]:
res_decrease_factor += 1
print("resolution decreased to " + "1/" + str(res_decrease_factor))
screen.fill((0, 0, 0))
is_zooming = True
elif keys[pygame.K_s] and keys[pygame.K_DOWN]:
if step > 10:
step -= 10
print("step decreased to " + str(step))
elif keys[pygame.K_i] and keys[pygame.K_DOWN]:
if max_iter > 10:
max_iter -= 10
print("iteration depth decreased to " + str(max_iter))
is_zooming = True
elif keys[pygame.K_DOWN]:
y_deviation += step/zoom
is_zooming = True
if keys[pygame.K_LEFT]:
x_deviation -= step/zoom
is_zooming = True
if keys[pygame.K_RIGHT]:
x_deviation += step/zoom
is_zooming = True
if keys[pygame.K_RETURN]:
pil(x_deviation, y_deviation, zoom)
if __name__ == '__main__':
main()
idk,我用花车。
为了缩放,我用 c-
做了这个c = complex((x_min + ((x + x_deviation * zoom * 2) / width) * (x_max - x_min)) / zoom, (y_min + ((y + y_deviation * zoom) / width) * (y_max - y_min)) / zoom)
并这样做以获得 y 和 x 偏差-
elif event.type == pygame.MOUSEBUTTONDOWN:
(mouseX, mouseY) = pygame.mouse.get_pos()
x_position = mouseX - width/2
y_position = mouseY - height/2
zoom *= zoom_multiplyer
x_deviation += x_position/zoom/2
y_deviation += y_position/zoom*2
screen.fill((0, 0, 0))
return True, zoom, x_deviation, y_deviation, zoom_multiplyer, step, res_decrease_factor, max_iter