Perl Cairo tutorial - Stroke, preserve, and fill the path

This is one page of a Perl Cairo tutorial.

This example draws a "flower", strokes the line, and then fills it in with a translucent colour. It demonstrates stroke_preserve. The "flower" is from Some of my better parametric transcendental formula art by Matthias Wandel.
#!/home/ben/software/install/bin/perl
use warnings;
use strict;
use Cairo;
use Math::Trig;
my $size = 500;
my $surface = Cairo::ImageSurface->create ('argb32', $size, $size);
my $cr = Cairo::Context->create ($surface);
$cr->move_to ($size / 2, $size / 2);
$cr->set_source_rgb (0, 0, 0);
# http://matt.wandel.ca/artwork/math_art.html
for (my $theta = 0; $theta < 2 * pi; $theta += .015) {
    my $rad = -(.5 * sin(5 * $theta)) * (.5 * cos(4 * $theta)) * 1000;
    my $angle = $theta + sin ($rad / 100);
    my $xp = $size / 2 + $rad * cos ($angle);
    my $yp = $size / 2 + $rad * sin ($angle);
    $cr->line_to ($xp, $yp); 
}
$cr->stroke_preserve ();
$cr->set_source_rgba (0.5, 0.5, 0, 0.5);
$cr->fill ();
$surface->write_to_png ('flower.png');

(download)


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer