Polygon Methods

Just to make life a little easier, the GD package now includes a number of polygon manipulation routines that were not part of the original library. All coordinate points are defined with (0, 0) being the upper left corner of the image. To create a shape, add points to the polygon with the addPt( ) method and draw it to the image with the polygon( ) or filledPolygon( ) method. The script in Example 2-5 draws a hobo symbol meaning “man with gun.” The image generated is shown in Figure 2-8.

Example 2-5. Drawing paths with polygons
#!/usr/bin/perl -w # # Example 2-5. Drawing with polygons use strict; use GD; my $image = new GD::Image(280,220); # Create two new polygon objects my $polygon1 = new GD::Polygon; my $polygon2 = new GD::Polygon; # Set up the points as a space-delimited string, # compatible with the SVG polygon format my $points1 = "140,140 220,60 220,10 270,60 220,60 ". "140,140 60,60 60,10 10,60 60,60 "; my $points2 = "140,40 260,200 20,200"; # Add the points to each polygon add_points($polygon1, $points1); add_points($polygon2, $points2); # Allocate colors; white is the background my $white= $image->colorAllocate(255,255,255); my $black = $image->colorAllocate(0,0,0); # Draw the two extended triangles $image->polygon($polygon1, $black); # Draw the center triangle, filled $image->filledPolygon($polygon2, $white); # Stroke the center triangle $image->polygon($polygon2, $black); # Print the image print "Content-type: image/png\n\n"; print $image->png( ); exit; ...

Get Perl Graphics Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.