<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package GD::Polygon;

use strict;
use Carp 'carp';
use GD;

# old documentation error
*GD::Polygon::delete = \&amp;deletePt;

=head1 NAME

GD::Polygon - Polygon class for the GD image library

=head1 SYNOPSIS

See L&lt;GD&gt;

=head1 DESCRIPTION

See L&lt;GD&gt;

=head1 AUTHOR

The GD.pm interface is copyright 1995-2005, Lincoln D. Stein.  It is
distributed under the same terms as Perl itself.  See the "Artistic
License" in the Perl source code distribution for licensing terms.

The latest versions of GD.pm are available on CPAN:

  http://www.cpan.org

=head1 SEE ALSO

L&lt;GD&gt;
L&lt;GD::Polyline&gt;,
L&lt;GD::SVG&gt;,
L&lt;GD::Simple&gt;,
L&lt;Image::Magick&gt;

=cut

### The polygon object ###
# create a new polygon
sub new {
    my $class = shift;
    return bless { 'length'=&gt;0,'points'=&gt;[] },$class;
}

# automatic destruction of the polygon
sub DESTROY {
    my $self = shift;
    undef $self-&gt;{'points'};
}

sub clear {
  my $self = shift;
  $self-&gt;{'points'} = [];
}

# add an x,y vertex to the polygon
sub addPt {
    my($self,$x,$y) = @_;
    push(@{$self-&gt;{'points'}},[$x,$y]);
    $self-&gt;{'length'}++;
}

# get a vertex
sub getPt {
    my($self,$index) = @_;
    return () unless ($index&gt;=0) &amp;&amp; ($index&lt;$self-&gt;{'length'});
    return @{$self-&gt;{'points'}-&gt;[$index]};
}

# change the value of a vertex
sub setPt {
    my($self,$index,$x,$y) = @_;
    unless (($index&gt;=0) &amp;&amp; ($index&lt;$self-&gt;{'length'})) {
	carp "Attempt to set an undefined polygon vertex";
	return undef;
    }
    @{$self-&gt;{'points'}-&gt;[$index]} = ($x,$y);
    1;
}

# return the total number of vertices
sub length {
    my $self = shift;
    return $self-&gt;{'length'};
}

# return the array of vertices.
# each vertex is an two-member (x,y) array
sub vertices {
    my $self = shift;
    return @{$self-&gt;{'points'}};
}

# return the bounding box of the polygon
# (smallest rectangle that contains it)
sub bounds {
    my $self = shift;
    my($top,$bottom,$left,$right) = @_;
    $top =    99999999;
    $bottom =-99999999;
    $left =   99999999;
    $right = -99999999;
    my $v;
    foreach $v ($self-&gt;vertices) {
	$left = $v-&gt;[0] if $left &gt; $v-&gt;[0];
	$right = $v-&gt;[0] if $right &lt; $v-&gt;[0];
	$top = $v-&gt;[1] if $top &gt; $v-&gt;[1];
	$bottom = $v-&gt;[1] if $bottom &lt; $v-&gt;[1];
    }
    return ($left,$top,$right,$bottom);
}

# delete a vertex, returning it, just for fun
sub deletePt {
     my($self,$index) = @_;
     unless (($index&gt;=0) &amp;&amp; ($index&lt;@{$self-&gt;{'points'}})) {
 	carp "Attempt to delete an undefined polygon vertex";
 	return undef;
     }
      my($vertex) = splice(@{$self-&gt;{'points'}},$index,1);
     $self-&gt;{'length'}--;
      return @$vertex;
  }

# translate the polygon in space by deltaX and deltaY
sub offset {
    my($self,$dh,$dv) = @_;
    my $size = $self-&gt;length;
    my($i);
    for ($i=0;$i&lt;$size;$i++) {
	my($x,$y)=$self-&gt;getPt($i);
	$self-&gt;setPt($i,$x+$dh,$y+$dv);
    }
}

# map the polygon from sourceRect to destRect,
# translating and resizing it if necessary
sub map {
    my($self,$srcL,$srcT,$srcR,$srcB,$destL,$destT,$destR,$destB) = @_;
    my($factorV) = ($destB-$destT)/($srcB-$srcT);
    my($factorH) = ($destR-$destL)/($srcR-$srcL);
    my($vertices) = $self-&gt;length;
    my($i);
    for ($i=0;$i&lt;$vertices;$i++) {
	my($x,$y) = $self-&gt;getPt($i);
	$x = int($destL + ($x - $srcL) * $factorH);
	$y = int($destT + ($y - $srcT) * $factorV);
	$self-&gt;setPt($i,$x,$y);
    }
}

# These routines added by Winfriend Koenig.
sub toPt {
    my($self, $dx, $dy) = @_;
    unless ($self-&gt;length &gt; 0) {
	$self-&gt;addPt($dx,$dy);
	return;
    }
    my ($x, $y) = $self-&gt;getPt($self-&gt;length-1);
    $self-&gt;addPt($x+$dx,$y+$dy);
}

sub transform($$$$$$$) {
    # see PostScript Ref. page 154
    my($self, $a, $b, $c, $d, $tx, $ty) = @_;
    my $size = $self-&gt;length;
    for (my $i=0;$i&lt;$size;$i++) {
	my($x,$y)=$self-&gt;getPt($i);
	$self-&gt;setPt($i, $a*$x+$c*$y+$tx, $b*$x+$d*$y+$ty);
    }
}

sub scale {
    my($self, $sx, $sy, $cx, $cy) = @_;
    $self-&gt;offset(-$cx,-$cy) if defined $cx or defined $cy;
    $self-&gt;transform($sx,0,0,$sy,$cx,$cy);
}

1;
</pre></body></html>