简体   繁体   English

用 pyglet 绘制交互式 mandelbrot

[英]Draw interactive mandelbrot with pyglet

I have a function that calculates the Mandelbrot set in an numpy array of size (500,500).我有一个函数可以计算大小为 (500,500) 的 numpy 数组中的 Mandelbrot 集。 I can draw it in matplotlib.我可以在 matplotlib 中绘制它。 Now i want to zoom into the image via mouse click.现在我想通过鼠标点击放大图像。 I want to use pyglet for that but struggle with rendering.我想为此使用 pyglet,但在渲染方面很挣扎。 I know this (not working) solution is inelegant, so I am happy for advice how to do this better!我知道这个(不工作)解决方案是不雅的,所以我很高兴就如何更好地做到这一点提供建议!

PS: The zoom level is set arbitrary right now, my problem I ask for help is the refreshing. PS:现在缩放级别是任意设置的,我寻求帮助的问题是令人耳目一新。

The functions I replaced with a docstring can be found 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()

Hi after a lot of trying around I found 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()

Got it running by removing the on draw and putting everything (calculating and drawing) in the on_mouse_press function.通过删除 on draw 并将所有内容(计算和绘图)放在 on_mouse_press 函数中来运行它。

You will find the complete solution here .您将在此处找到完整的解决方案。 It is still very slow in drawing, any suggestions there are warmly welcome!画的还是很慢,欢迎大家提出建议!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM