Debian Codegraph @

examples/mandelbrot.py

raw symbols SCIP
1 #
2 # PyOtherSide: Asynchronous Python 3 Bindings for Qt 5 and Qt 6
3 # Copyright (c) 2011, 2013, Thomas Perl <[email protected]>
4 #
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 #
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10 # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11 # FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12 # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14 # OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 # PERFORMANCE OF THIS SOFTWARE.
16 #
17
18 import pyotherside
19 import random
20
21 COLORS = [(255, 255, 255, 255), (255, 0, 0, 0)]
22
23 for i in range(30):
24 COLORS.insert(1, (
25 255, # alpha
26 random.randint(30, 255), # red
27 random.randint(30, 255), # green
28 random.randint(30, 255), # blue
29 ))
30
31 # Of course, in production code, you don't want to calculate your mandelbrot
32 # set in CPython for performance reasons. This is just an example of what is
33 # possible with the image provider support in PyOtherSide.
34 def mandelbrot(x, y, steps):
35 a = b = 0
36 for i in range(steps):
37 a, b = a**2 - b**2 + x, 2*a*b + y
38 if a**2 + b**2 >= 4:
39 break
40 return i
41
42 def render_mandelbrot(image_id, requested_size):
43 # when the url is: "image://python/xmin/xmax/ymin/ymax"
44 # the image_id is: "xmin/xmax/ymin/ymax"
45
46 parts = [float(x) for x in image_id.split('/')]
47 x_range = parts[0:2]
48 y_range = parts[2:4]
49
50 # width and height will be -1 if not set in QML
51 if requested_size == (-1, -1):
52 requested_size = (200, 200)
53
54 width, height = requested_size
55
56 pixels = []
57 for y in range(height):
58 yy = y_range[0] + (y_range[1] - y_range[0]) * y / height
59 for x in range(width):
60 xx = x_range[0] + (x_range[1] - x_range[0]) * x / width
61 pixels.extend(reversed(COLORS[mandelbrot(xx, yy, len(COLORS))]))
62 return bytearray(pixels), (width, height), pyotherside.format_argb32
63
64 pyotherside.set_image_provider(render_mandelbrot)