home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / python-cairo / examples / hering.py < prev    next >
Encoding:
Python Source  |  2006-01-20  |  1.0 KB  |  56 lines

  1. #!/usr/bin/env python
  2. """cairo/cairo-demo/png/hering.c translated into Python
  3. """
  4.  
  5. import math
  6.  
  7. import cairo
  8.  
  9. WIDTH  = 300
  10. HEIGHT = 600
  11.  
  12. def draw_hering (ctx, width, height):
  13.     LINES= 32
  14.     MAX_THETA = .80 * math.pi * 2
  15.     THETA_INC = 2.0 * MAX_THETA / (LINES-1)
  16.  
  17.     ctx.set_source_rgb (0, 0, 0)
  18.     ctx.set_line_width (2.0)
  19.  
  20.     ctx.save()
  21.  
  22.     ctx.translate (width / 2, height / 2)
  23.     ctx.rotate (MAX_THETA)
  24.  
  25.     for i in range (LINES):
  26.         ctx.move_to (-2 * width, 0)
  27.         ctx.line_to (2 * width, 0)
  28.         ctx.stroke()
  29.  
  30.         ctx.rotate (- THETA_INC)
  31.  
  32.     ctx.restore()
  33.  
  34.     ctx.set_line_width (6)
  35.     ctx.set_source_rgb (1, 0, 0)
  36.  
  37.     ctx.move_to (width / 4.0, 0)
  38.     ctx.rel_line_to (0, height)
  39.     ctx.stroke()
  40.  
  41.     ctx.move_to (3 * width / 4.0, 0)
  42.     ctx.rel_line_to (0, height)
  43.     ctx.stroke()
  44.  
  45.  
  46. surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
  47. ctx = cairo.Context(surface)
  48.  
  49. ctx.set_source_rgb (1, 1, 1)
  50. ctx.set_operator (cairo.OPERATOR_SOURCE)
  51. ctx.paint()
  52.  
  53. draw_hering (ctx, WIDTH, HEIGHT)
  54.  
  55. surface.write_to_png('hering.png')
  56.