Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / py_simple / sinplots_AA.py
blob44afd21ecb7ce403b42862e08ea1778832da3932
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 random
6 from math import sin
8 AA = True
9 W = 320
10 H = 240
11 N = 10
13 class plotter(object):
14 def __init__(self):
15 self.x0 = random.uniform(0, W)
16 self.y0 = random.uniform(H*0.2, H*0.8)
17 self.a = H * (0.06 + random.uniform(0, 0.1) + random.uniform(0, 0.2))
18 self.period = 4.0 + random.expovariate(4.0/W)
19 self.speed = random.uniform(0.04, min(max(0.05, 0.5 * self.period / self.a), 1.0))
20 def pos(self, t):
21 x = self.x0 + self.speed * t
22 y = self.y0 + sin(x / self.period) * self.a
23 return (x % W, y)
24 def color(self, t):
25 return (
26 128 + 120 * sin(t / (self.a + 10.0)),
27 128 + 120 * sin(t / (self.y0 + 3.0)),
28 128 + 120 * sin(t / (self.x0 + 5.0)))
31 def main():
32 bk = backends.BackendSDLInit(W, H, 16, 0, "Sinplots AA")
33 assert bk
34 print(bk)
35 print("Modify source for parameters,")
36 print("Kill to terminate ;-)")
37 black = bk.pixmap.RGBToPixel(0, 0, 0)
39 ps = [plotter() for i in range(N)]
40 t = random.uniform(0.0, 10.0 * W)
41 while True:
42 t += 1.0
43 for p in ps:
44 (x, y) = p.pos(t)
45 (r, g, b) = p.color(t)
46 if AA:
47 x = int(x * 0x100)
48 y = int(y * 0x100)
49 bk.pixmap.gfx.VLineAA(x + 0x100, y - 0x200, y + 0x200, black)
50 bk.pixmap.gfx.PutPixelAA(x, y, bk.pixmap.RGBToPixel(int(r), int(g), int(b)))
51 else:
52 x = int(x)
53 y = int(y)
54 bk.pixmap.gfx.VLine(x + 1, y - 2, y + 2, black)
55 bk.pixmap.core.PutPixel(x, y, bk.pixmap.RGBToPixel(int(r), int(g), int(b)))
56 bk.Flip()
58 if __name__ == '__main__':
59 main()