Reputation: 324
I have a function that calculates the Mandelbrot set in an numpy array of size (500,500). I can draw it in matplotlib. Now i want to zoom into the image via mouse click. I want to use pyglet for that but struggle with rendering. 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.
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()
Upvotes: 0
Views: 322
Reputation: 324
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.
You will find the complete solution here. It is still very slow in drawing, any suggestions there are warmly welcome!
Upvotes: 1