[英]Draw interactive mandelbrot with pyglet
我有一个函数可以计算大小为 (500,500) 的 numpy 数组中的 Mandelbrot 集。 我可以在 matplotlib 中绘制它。 现在我想通过鼠标点击放大图像。 我想为此使用 pyglet,但在渲染方面很挣扎。 我知道这个(不工作)解决方案是不雅的,所以我很高兴就如何更好地做到这一点提供建议!
PS:现在缩放级别是任意设置的,我寻求帮助的问题是令人耳目一新。
我用文档字符串替换的函数可以在这里找到
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()
嗨,经过多次尝试,我发现了这个
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 draw 并将所有内容(计算和绘图)放在 on_mouse_press 函数中来运行它。
您将在此处找到完整的解决方案。 画的还是很慢,欢迎大家提出建议!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.