OSDN Git Service

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