使用pyglet绘制交互式mandelbrot

问题描述 投票:0回答:1

我有一个函数,用于以大小为(500,500)的numpy数组计算Mandelbrot集。我可以在matplotlib中绘制它。现在,我想通过鼠标单击放大图像。我想为此使用pyglet,但在渲染方面遇到困难。我知道这种(无效的)解决方案非常糟糕,因此我很乐意为您提供如何更好地做到这一点的建议!

PS:缩放级别现在设置为任意,我需要帮助的问题是刷新。

我用文档字符串替换的函数可以找到here

from pyglet.window import mouse
import pyglet
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm
from pyglet import image
"""
def calc_pixel_array(x_center: float = 0, y_center: float = 0, 
                     scale: float = 1.0, width: int = 500, 
                     height: int = 500, threshold: int = 200):
    Returns numpy array with shape (500,500) with color values

def plot_pixel_array(pixel_array, cmap: str = 'gnuplot', show_plot: bool = False):
    Draws pixel_array via matplotlib and saves in curent_mandelbrot.png
"""    
pixel_array = calc_pixel_array()
plot_pixel_array(pixel_array)

scale = 1
i = 1
window = pyglet.window.Window(width = 500, height = 500)
image = pyglet.resource.image('curent_mandelbrot.png')
pic = image.load('curent_mandelbrot.png')

@window.event
def on_draw():
    window.clear()
    pic.blit(0, 0)

@window.event
def on_mouse_press(x, y, button, modifiers):
    if button == mouse.LEFT:
        i = np.random.randint(5)+1
        pixel_array = calc_pixel_array(scale/(10*i))
        plot_pixel_array(pixel_array)
        window.clear()
        image = pyglet.resource.image('curent_mandelbrot.png')
        image.blit(0,0)


pyglet.app.run()
python matplotlib pyglet mandelbrot
1个回答
0
投票

嗨,我经过很多尝试后发现this

 def draw_pixel_array(self, window):
        main_batch = pyglet.graphics.Batch()
        shape = self.pixel_array.shape
        for x in range(shape[0]):
            for y in range(shape[1]):
                color = int(self.pixel_array[x, y])
                pyglet.graphics.draw(1, pyglet.gl.GL_POINTS,
                                     ('v2i', (x, y)),
                                     ('c3B', (color, color, 0)))
        main_batch.draw()

通过删除绘图并将所有内容(计算和绘图)放到on_mouse_press函数中使其运行。

您将找到完整的解决方案here。绘制仍然很慢,热烈欢迎任何建议!

© www.soinside.com 2019 - 2024. All rights reserved.