[英]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.