~juanramirez/python-snippets/pythoncore

« back to all changes in this revision

Viewing changes to cairo/hering.py

  • Committer: Jono Bacon
  • Date: 2010-03-26 23:26:58 UTC
  • Revision ID: jono@ubuntu.com-20100326232658-ck9fz24d935hubfq
Added a bunch of Cairo snippets.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# [SNIPPET_NAME: Hering]
 
3
# [SNIPPET_CATEGORIES: Cairo]
 
4
# [SNIPPET_DESCRIPTION: Generate a hering]
 
5
# [SNIPPET_DOCS: http://www.tortall.net/mu/wiki/CairoTutorial]
 
6
# [SNIPPET_LICENSE: MPL]
 
7
 
 
8
import math
 
9
import cairo
 
10
import os
 
11
 
 
12
WIDTH  = 300
 
13
HEIGHT = 600
 
14
 
 
15
def draw_hering (ctx, width, height):
 
16
    LINES= 32
 
17
    MAX_THETA = .80 * math.pi * 2
 
18
    THETA_INC = 2.0 * MAX_THETA / (LINES-1)
 
19
 
 
20
    ctx.set_source_rgb (0, 0, 0)
 
21
    ctx.set_line_width (2.0)
 
22
 
 
23
    ctx.save()
 
24
 
 
25
    ctx.translate (width / 2, height / 2)
 
26
    ctx.rotate (MAX_THETA)
 
27
 
 
28
    for i in range (LINES):
 
29
        ctx.move_to (-2 * width, 0)
 
30
        ctx.line_to (2 * width, 0)
 
31
        ctx.stroke()
 
32
 
 
33
        ctx.rotate (- THETA_INC)
 
34
 
 
35
    ctx.restore()
 
36
 
 
37
    ctx.set_line_width (6)
 
38
    ctx.set_source_rgb (1, 0, 0)
 
39
 
 
40
    ctx.move_to (width / 4.0, 0)
 
41
    ctx.rel_line_to (0, height)
 
42
    ctx.stroke()
 
43
 
 
44
    ctx.move_to (3 * width / 4.0, 0)
 
45
    ctx.rel_line_to (0, height)
 
46
    ctx.stroke()
 
47
 
 
48
 
 
49
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
 
50
ctx = cairo.Context(surface)
 
51
 
 
52
ctx.set_source_rgb (1, 1, 1)
 
53
ctx.set_operator (cairo.OPERATOR_SOURCE)
 
54
ctx.paint()
 
55
 
 
56
draw_hering (ctx, WIDTH, HEIGHT)
 
57
 
 
58
# the output is written to your home directory
 
59
surface.write_to_png(os.path.expanduser("~") + '/hering.png')