Perl Cairo tutorial - Curves

This is one page of a Perl Cairo tutorial.

This shows drawing cubic Bezier curves with curve_to. The data is taken from Camel contour by nicubunu.
#!/usr/bin/perl
use warnings;
use strict;
use Cairo;
my @camel = <DATA>;
my $size = 500;
my $surface = Cairo::ImageSurface->create ('argb32', $size, $size);
my $cr = Cairo::Context->create ($surface);
# Orange body
camel_path ();
$cr->set_source_rgb (238/255, 154/255, 0);
$cr->fill_preserve ();
# Black outline
$cr->set_source_rgb (0, 0, 0);
$cr->stroke ();
$surface->write_to_png ('camel.png');
exit;

sub camel_path
{
    for my $line (@camel) {
        my ($command, @numbers) = split /,|\s/, $line;
        if ($command eq 'M') {
            $cr->move_to (@numbers);
        }
        elsif ($command eq 'C') {
            $cr->curve_to (@numbers);
        }
        elsif ($command eq 'L') {
            $cr->line_to (@numbers);
        }
        else {
            die "Unknown command '$command' in $line";
        }
    }
}

# This image data comes from
# http://openclipart.org/detail/14482/camel-contour-by-nicubunu
# Copyright: public domain
__DATA__
M 49.175268,201.01485
C 48.512168,203.50485 47.939368,208.05185 52.101168,207.54885
C 57.470968,206.89985 65.718168,194.04185 70.475468,190.61485
C 74.091968,188.00885 79.047168,189.43785 83.175268,188.48585
C 90.600868,186.77185 96.925668,182.21085 102.17527,177.01485
C 102.78987,194.09585 99.976468,210.97685 100.17917,228.01485
C 100.44067,250.00585 104.59117,271.20985 106.08577,293.01485
C 106.72777,302.38185 104.74567,311.77185 105.26867,321.01485
C 105.66827,328.07785 104.80057,337.23585 106.60817,344.01485
C 109.59397,355.21285 113.17527,362.79985 113.17527,375.01485
C 113.17527,385.56285 110.74087,396.94885 111.28327,407.01485
C 111.61137,413.10385 114.35137,418.94085 115.00167,425.01485
C 115.64847,431.05685 113.11027,441.22985 120.28017,444.27285
C 126.97257,447.11285 152.42626,449.78185 148.47126,438.01885
C 146.08026,430.90585 137.42526,429.58985 134.01396,422.99985
C 129.84547,414.94885 132.98116,402.19385 122.17527,399.01485
L 119.55797,353.01485
L 123.08577,333.01485
L 123.59347,307.01485
L 134.77016,264.01485
L 145.17526,232.01485
L 147.17526,232.01485
C 148.85526,251.28285 152.45526,270.47085 151.08526,290.01485
C 150.38126,300.06785 142.91726,310.24385 143.81426,320.01485
C 144.47026,327.15985 150.37626,333.24785 152.58126,340.01485
C 159.19026,360.30385 167.06526,381.79385 176.09426,401.01485
C 179.92826,409.17685 187.09126,418.06285 188.82226,427.01485
C 189.57626,430.91685 186.01926,438.00985 189.77826,440.98685
C 192.08026,442.81085 196.45126,441.96285 199.17526,442.02985
C 205.61526,442.18785 226.63826,446.71885 229.32426,437.90585
C 231.30326,431.41485 219.47626,424.89685 215.17526,422.01585
C 197.76926,410.35485 187.64426,395.32785 180.07126,376.01485
C 174.08826,360.75585 168.36126,338.49985 170.24826,322.01485
C 172.94126,298.48085 183.65226,276.33585 188.17526,253.01485
C 202.02926,264.36885 229.55726,271.42285 247.17526,274.01485
C 247.17526,287.62385 247.50026,304.39285 255.82426,315.99985
C 259.81826,321.56985 268.27526,323.65685 271.05826,330.01885
C 272.41226,333.11585 270.91826,336.74685 271.56926,340.01485
C 273.53526,349.88585 277.15526,359.95185 277.95026,370.01485
C 278.74626,380.07885 272.89226,389.97585 269.78526,399.01485
C 265.72726,410.81885 265.22326,423.95885 264.28926,436.01485
C 263.99226,439.84785 261.10026,444.21685 261.62626,447.90585
C 262.60926,454.81085 278.64626,452.09285 283.17526,451.01485
C 283.08626,455.32185 280.77126,461.86685 283.20326,465.73585
C 286.99026,471.76385 303.88826,471.00185 310.17526,471.01485
C 314.51926,471.02385 326.49127,474.33985 328.51327,468.73985
C 329.66027,465.56185 326.44427,463.08385 324.80427,460.84485
C 318.54526,452.30385 310.25626,451.05285 303.45426,443.80185
C 294.46226,434.21785 294.10326,415.26185 293.26526,403.01485
C 292.52526,392.22285 291.11926,377.19485 295.31926,367.02985
C 297.53826,361.65885 302.88726,358.79185 304.65626,353.01485
C 307.36126,344.18485 304.14826,334.16785 309.25026,326.05785
C 317.39826,313.10485 333.38427,313.14685 334.17527,295.01485
L 335.17527,295.01485
C 345.65727,323.59485 382.92227,314.28985 398.64627,295.91085
C 418.12827,273.13885 425.86527,244.23985 425.16027,215.01485
C 424.78827,199.57585 425.54027,184.41185 427.00527,169.01485
C 427.82727,160.38085 426.09227,155.58685 431.17527,148.01485
L 432.17527,148.01485
C 441.82127,157.05285 461.02427,149.25885 457.78127,135.01485
C 454.15327,119.08265 434.64127,113.57045 421.17527,109.01485
C 422.27027,105.15555 423.94727,101.05865 424.03827,97.014856
C 424.24827,87.798956 416.24527,83.784956 408.17527,84.737056
C 394.09527,86.398456 379.82027,92.608156 368.17527,100.65916
C 357.16727,108.26935 343.50027,122.80795 342.39927,137.01485
C 341.80127,144.73285 345.41827,152.20285 344.17527,160.01485
C 326.13127,140.42285 316.28126,107.83285 312.38826,82.014856
C 311.00526,72.842556 318.50826,62.746956 313.71826,54.119756
C 311.56426,50.239656 306.97826,48.950656 303.17526,47.248656
C 292.81926,42.613856 276.55226,38.492556 265.17526,40.227856
C 239.32126,44.171056 247.21426,91.130356 217.21826,82.580456
C 203.41626,78.646356 210.01526,62.135356 206.57226,53.029456
C 200.85326,37.905956 184.05626,40.573556 172.17526,40.886056
C 168.05526,40.994356 164.17526,39.389656 160.17626,41.105156
C 152.72126,44.302556 148.44426,53.516156 146.84426,61.014856
C 143.38026,77.244156 145.76326,93.373456 138.68026,108.92535
C 136.80646,113.03875 132.92146,116.03675 130.70457,120.01485
C 119.56617,140.00185 110.49307,157.87085 90.175268,170.83085
C 81.096568,176.62185 71.534368,177.13885 62.175268,181.68485
C 55.529968,184.91285 35.168268,192.28485 49.175268,201.01485

(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