annotate GD/Polygon.pm @ 0:e94de0ea3351 draft default tip

Uploaded
author dereeper
date Wed, 11 Sep 2013 09:08:15 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1 package GD::Polygon;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
2
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
3 use strict;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
4 use Carp 'carp';
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
5 use GD;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
6
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
7 # old documentation error
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
8 *GD::Polygon::delete = \&deletePt;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
9
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
10 =head1 NAME
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
11
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
12 GD::Polygon - Polygon class for the GD image library
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
13
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
14 =head1 SYNOPSIS
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
15
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
16 See L<GD>
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
17
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
18 =head1 DESCRIPTION
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
19
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
20 See L<GD>
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
21
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
22 =head1 AUTHOR
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
23
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
24 The GD.pm interface is copyright 1995-2005, Lincoln D. Stein. It is
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
25 distributed under the same terms as Perl itself. See the "Artistic
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
26 License" in the Perl source code distribution for licensing terms.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
27
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
28 The latest versions of GD.pm are available on CPAN:
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
29
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
30 http://www.cpan.org
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
31
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
32 =head1 SEE ALSO
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
33
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
34 L<GD>
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
35 L<GD::Polyline>,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
36 L<GD::SVG>,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
37 L<GD::Simple>,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
38 L<Image::Magick>
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
39
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
40 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
41
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
42 ### The polygon object ###
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
43 # create a new polygon
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
44 sub new {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
45 my $class = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
46 return bless { 'length'=>0,'points'=>[] },$class;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
47 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
48
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
49 # automatic destruction of the polygon
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
50 sub DESTROY {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
51 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
52 undef $self->{'points'};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
53 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
54
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
55 sub clear {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
56 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
57 $self->{'points'} = [];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
58 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
59
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
60 # add an x,y vertex to the polygon
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
61 sub addPt {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
62 my($self,$x,$y) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
63 push(@{$self->{'points'}},[$x,$y]);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
64 $self->{'length'}++;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
65 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
66
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
67 # get a vertex
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
68 sub getPt {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
69 my($self,$index) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
70 return () unless ($index>=0) && ($index<$self->{'length'});
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
71 return @{$self->{'points'}->[$index]};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
72 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
73
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
74 # change the value of a vertex
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
75 sub setPt {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
76 my($self,$index,$x,$y) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
77 unless (($index>=0) && ($index<$self->{'length'})) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
78 carp "Attempt to set an undefined polygon vertex";
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
79 return undef;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
80 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
81 @{$self->{'points'}->[$index]} = ($x,$y);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
82 1;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
83 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
84
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
85 # return the total number of vertices
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
86 sub length {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
87 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
88 return $self->{'length'};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
89 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
90
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
91 # return the array of vertices.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
92 # each vertex is an two-member (x,y) array
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
93 sub vertices {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
94 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
95 return @{$self->{'points'}};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
96 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
97
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
98 # return the bounding box of the polygon
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
99 # (smallest rectangle that contains it)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
100 sub bounds {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
101 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
102 my($top,$bottom,$left,$right) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
103 $top = 99999999;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
104 $bottom =-99999999;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
105 $left = 99999999;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
106 $right = -99999999;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
107 my $v;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
108 foreach $v ($self->vertices) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
109 $left = $v->[0] if $left > $v->[0];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
110 $right = $v->[0] if $right < $v->[0];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
111 $top = $v->[1] if $top > $v->[1];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
112 $bottom = $v->[1] if $bottom < $v->[1];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
113 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
114 return ($left,$top,$right,$bottom);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
115 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
116
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
117 # delete a vertex, returning it, just for fun
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
118 sub deletePt {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
119 my($self,$index) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
120 unless (($index>=0) && ($index<@{$self->{'points'}})) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
121 carp "Attempt to delete an undefined polygon vertex";
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
122 return undef;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
123 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
124 my($vertex) = splice(@{$self->{'points'}},$index,1);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
125 $self->{'length'}--;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
126 return @$vertex;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
127 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
128
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
129 # translate the polygon in space by deltaX and deltaY
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
130 sub offset {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
131 my($self,$dh,$dv) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
132 my $size = $self->length;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
133 my($i);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
134 for ($i=0;$i<$size;$i++) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
135 my($x,$y)=$self->getPt($i);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
136 $self->setPt($i,$x+$dh,$y+$dv);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
137 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
138 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
139
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
140 # map the polygon from sourceRect to destRect,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
141 # translating and resizing it if necessary
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
142 sub map {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
143 my($self,$srcL,$srcT,$srcR,$srcB,$destL,$destT,$destR,$destB) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
144 my($factorV) = ($destB-$destT)/($srcB-$srcT);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
145 my($factorH) = ($destR-$destL)/($srcR-$srcL);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
146 my($vertices) = $self->length;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
147 my($i);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
148 for ($i=0;$i<$vertices;$i++) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
149 my($x,$y) = $self->getPt($i);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
150 $x = int($destL + ($x - $srcL) * $factorH);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
151 $y = int($destT + ($y - $srcT) * $factorV);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
152 $self->setPt($i,$x,$y);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
153 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
154 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
155
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
156 # These routines added by Winfriend Koenig.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
157 sub toPt {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
158 my($self, $dx, $dy) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
159 unless ($self->length > 0) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
160 $self->addPt($dx,$dy);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
161 return;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
162 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
163 my ($x, $y) = $self->getPt($self->length-1);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
164 $self->addPt($x+$dx,$y+$dy);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
165 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
166
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
167 sub transform($$$$$$$) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
168 # see PostScript Ref. page 154
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
169 my($self, $a, $b, $c, $d, $tx, $ty) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
170 my $size = $self->length;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
171 for (my $i=0;$i<$size;$i++) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
172 my($x,$y)=$self->getPt($i);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
173 $self->setPt($i, $a*$x+$c*$y+$tx, $b*$x+$d*$y+$ty);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
174 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
175 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
176
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
177 sub scale {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
178 my($self, $sx, $sy, $cx, $cy) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
179 $self->offset(-$cx,-$cy) if defined $cx or defined $cy;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
180 $self->transform($sx,0,0,$sy,$cx,$cy);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
181 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
182
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
183 1;