demos: spiv: Update cpu counter for multithread filters.
[gfxprim.git] / demos / py_simple / plot.py
blobd58e574214339ead0d44e3705e77d578cbb232b2
1 #!/usr/bin/env python
2 import gfxprim.core as core
3 import gfxprim.backends as backends
4 import gfxprim.gfx as gfx
5 import sys
6 from math import *
8 W = 640
9 H = 480
10 EPS = 0.05
12 env = {}
13 env['sin'] = sin
14 env['cos'] = cos
15 env['sqrt'] = sqrt
17 def compute(func, x, y_min, y_max):
18 env['x'] = x
19 try:
20 ret = eval(func, env)
21 except ZeroDivisionError:
22 return -1
24 return - H * ret / (y_max - y_min) + (H * y_max) / (y_max - y_min)
26 def x_to_x(x, x_min, x_max):
27 return (x_max - x_min) * x / W + x_min
29 class plotter(object):
30 def __init__(self, func, x_min, x_max, y_min, y_max):
31 self.x = 0.0
32 self.y = compute(func, x_to_x(self.x, x_min, x_max), y_min, y_max)
33 self.y_min = y_min
34 self.y_max = y_max
35 self.x_min = x_min
36 self.x_max = x_max
37 self.func = func
38 def next(self):
39 x = x_to_x(self.x + 1, self.x_min, self.x_max)
40 new_y = compute(self.func, x, self.y_min, self.y_max)
42 inc = 1.0
44 while (abs(new_y - self.y) > 1 and inc > EPS):
45 x = x_to_x(self.x + inc, self.x_min, self.x_max)
46 new_y = compute(self.func, x, self.y_min, self.y_max)
47 inc /= 2
49 self.x += inc
50 self.y = new_y
52 print(self.x, self.y)
55 def main():
56 bk = backends.BackendSDLInit(W, H, 0, 0, "Plot AA")
57 assert bk
58 print(bk)
59 print("Modify source for parameters,")
60 print("Kill to terminate ;-)")
61 print("Usage: function x_min x_max y_min y_max")
62 black = bk.context.RGBToPixel(0, 0, 0)
64 x_min = float(sys.argv[2])
65 x_max = float(sys.argv[3])
66 y_min = float(sys.argv[4])
67 y_max = float(sys.argv[5])
68 p = plotter(sys.argv[1], x_min, x_max, y_min, y_max)
70 r = 255
71 g = 0
72 b = 0
74 while p.x < W:
75 x = int((p.x + 0.5) * 0x100)
76 y = int((p.y + 0.5) * 0x100)
77 gfx.PutPixelAA(bk.context, x, y, bk.context.RGBToPixel(int(r), int(g), int(b)))
78 bk.Flip()
79 p.next()
81 while True:
82 pass
84 if __name__ == '__main__':
85 main()