OSDN Git Service

Working on README, put defs in joy.py
[joypy/Thun.git] / implementations / uvm-ncc / neat_wu_lines.py
1 from PIL import Image, ImageDraw
2
3 LIMIT = 0xffff
4
5
6 def draw_wu_line(draw, x, y, w, h):
7     # Without loss of generality only lines in the first oc
8     assert(w > 0 and h > 0 and w > h)
9     k = LIMIT * (h-1) // w
10     x1 = x + w - 1
11     y1 = y + h - 1
12     d = 0
13     while x1 > x:
14         draw.point([(x, y), (x1, y1)], fill=(0, 0, 0, 0xff - (d >> 8)))
15         draw.point([(x, y + 1), (x1, y1 - 1)], fill=(0, 0, 0,         d >> 8))
16         x += 1
17         x1 -= 1
18         if d + k >= LIMIT:
19             d = k - (LIMIT - d)
20             y += 1
21             y1 -= 1
22         else:
23             d += k
24     if x1 == x:
25         if y1 == y:
26             points = [(x, y)]
27             alpha = 0xff
28         else:
29             points = [(x, y), (x, y1)]
30             alpha = 0x7f
31         draw.point(points, fill=(0, 0, 0, alpha))
32
33 size = 100, 33
34 im = Image.new('RGBA', size)
35 d = ImageDraw.Draw(im, 'RGBA')
36
37 draw_wu_line(d, 0, 0, *size)
38
39 ##for w in range(51, 100):
40 ##    draw_wu_line(d, 0, 0, w, 50)
41
42 base = Image.new('RGBA', size, (0xff, 0xff, 0xff, 0xff))
43 base.alpha_composite(im)
44 base.save('wu_demo.png')