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 / splines_tolerance.pl < prev    next >
Encoding:
Perl Script  |  2005-07-12  |  1018 b   |  61 lines

  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Cairo;
  6.  
  7. use constant
  8. {
  9.     WIDTH => 600,
  10.     HEIGHT => 300,
  11. };
  12.  
  13. {
  14.     my $surf = Cairo::ImageSurface->create ('argb32', WIDTH, HEIGHT);
  15.     my $cr = Cairo::Context->create ($surf);
  16.  
  17.     $0 =~ /(.*)\.pl/;
  18.     my $out = "$1.png";
  19.  
  20.     $cr->rectangle (0, 0, WIDTH, HEIGHT);
  21.     $cr->set_source_rgb (1, 1, 1);
  22.     $cr->fill;
  23.  
  24.     draw_splines ($cr, WIDTH, HEIGHT);
  25.  
  26.     $cr->show_page;
  27.  
  28.     $surf->write_to_png ($out);
  29. }
  30.  
  31. sub draw_spline
  32. {
  33.     my ($cr, $height) = @_;
  34.  
  35.     $cr->move_to (0, .1 * $height);
  36.     $height = .8 * $height;
  37.     $cr->rel_curve_to (-$height / 2, $height / 2, $height / 2, $height / 2,
  38.                0, $height);
  39.     $cr->stroke;
  40. }
  41.  
  42. sub draw_splines
  43. {
  44.     my ($cr, $width, $height) = @_;
  45.  
  46.     my @tolerance = (.1, .5, 1, 5, 10);
  47.     my $line_width = .08 * $width;
  48.     my $gap = $width / 6;
  49.  
  50.     $cr->set_source_rgb (0, 0, 0);
  51.     $cr->set_line_width ($line_width);
  52.  
  53.     $cr->translate ($gap, 0);
  54.     for (my $i = 0; $i < 5; $i++)
  55.     {
  56.         $cr->set_tolerance ($tolerance[$i]);
  57.         draw_spline ($cr, $height);
  58.         $cr->translate ($gap, 0);
  59.     }
  60. }
  61.