Perl Cairo tutorial - Draw a grid

This is one page of a Perl Cairo tutorial.

This shows drawing a grid of lines like a piece of graph paper.
#!/usr/bin/perl
use warnings;
use strict;
use Cairo;
my $size = 400;
my $surface = Cairo::ImageSurface->create ('argb32', $size, $size);
my $cr = Cairo::Context->create ($surface);
$cr->rectangle (0, 0, $size, $size);
$cr->set_source_rgb (0.9, 0.9, 0.9);
$cr->fill ();
$cr->set_source_rgb (0.4, 0.4, 1);
$cr->set_line_width (2);
my $divisions = 10;
for my $i (1..$divisions - 1) {
    $cr->move_to (($size * $i)/$divisions, 0);
    $cr->line_to (($size * $i)/$divisions, $size);
    $cr->move_to (0, ($size * $i)/$divisions);
    $cr->line_to ($size, ($size * $i)/$divisions);
}
$cr->stroke ();
my $png = 'grid.png';
$surface->write_to_png ($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