home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / libcairo-perl / examples / png / hering.pl < prev    next >
Encoding:
Perl Script  |  2006-05-14  |  1.1 KB  |  77 lines

  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Cairo;
  6.  
  7. use constant
  8. {
  9.     WIDTH => 300,
  10.     HEIGHT => 600,
  11.     LINES => 32,
  12.     M_PI_2 => 3.14159265 / 2.0,
  13. };
  14.  
  15. use constant
  16. {
  17.     MAX_THETA => (.80 * M_PI_2),
  18. };
  19.  
  20. use constant
  21. {
  22.     THETA_INC => (2.0 * MAX_THETA / (LINES-1)),
  23. };
  24.  
  25. {
  26.     my $surf = Cairo::ImageSurface->create ('argb32', WIDTH, HEIGHT);
  27.     my $cr = Cairo::Context->create ($surf);
  28.  
  29.     $0 =~ /(.*)\.pl/;
  30.     my $out = "$1.png";
  31.  
  32.     $cr->rectangle (0, 0, WIDTH, HEIGHT);
  33.     $cr->set_source_rgb (1, 1, 1);
  34.     $cr->fill;
  35.  
  36.     draw_hering ($cr, WIDTH, HEIGHT);
  37.  
  38.     $cr->show_page;
  39.  
  40.     $surf->write_to_png ($out);
  41. }
  42.  
  43. sub draw_hering
  44. {
  45.     my ($cr, $width, $height) = @_;
  46.  
  47.     $cr->set_source_rgb (0, 0, 0);
  48.     $cr->set_line_width (2.0);
  49.  
  50.     $cr->save;
  51.     {
  52.         $cr->translate ($width / 2, $height / 2);
  53.         $cr->rotate (MAX_THETA);
  54.  
  55.         for (my $i=0; $i < LINES; $i++)
  56.         {
  57.         $cr->move_to (-2 * $width, 0);
  58.         $cr->line_to (2 * $width, 0);
  59.         $cr->stroke;
  60.  
  61.         $cr->rotate (- THETA_INC);
  62.         }
  63.     }
  64.     $cr->restore;
  65.  
  66.     $cr->set_line_width (6);
  67.     $cr->set_source_rgb (1, 0, 0);
  68.  
  69.     $cr->move_to ($width / 4, 0);
  70.     $cr->rel_line_to (0, $height);
  71.     $cr->stroke;
  72.  
  73.     $cr->move_to (3 * $width / 4, 0);
  74.     $cr->rel_line_to (0, $height);
  75.     $cr->stroke;
  76. }
  77.