$VERSION
=
'2.82'
;
*GD::Polygon::delete
= \
&deletePt
;
sub
new {
my
$class
=
shift
;
return
bless
{
'length'
=>0,
'points'
=>[] },
$class
;
}
sub
DESTROY {
my
$self
=
shift
;
undef
$self
->{
'points'
};
}
sub
clear {
my
$self
=
shift
;
$self
->{
'points'
} = [];
$self
->{
'length'
} = 0;
}
sub
addPt {
my
(
$self
,
$x
,
$y
) =
@_
;
push
(@{
$self
->{
'points'
}},[
$x
,
$y
]);
$self
->{
'length'
}++;
}
sub
getPt {
my
(
$self
,
$index
) =
@_
;
return
()
unless
(
$index
>= 0) && (
$index
<
$self
->{
'length'
});
return
@{
$self
->{
'points'
}->[
$index
]};
}
sub
setPt {
my
(
$self
,
$index
,
$x
,
$y
) =
@_
;
unless
((
$index
>=0) && (
$index
<
$self
->{
'length'
})) {
carp
"Attempt to set an undefined polygon vertex"
;
return
undef
;
}
@{
$self
->{
'points'
}->[
$index
]} = (
$x
,
$y
);
1;
}
sub
length
{
shift
->{
'length'
}
}
sub
vertices {
@{
shift
->{
'points'
}}
}
sub
bounds {
my
$self
=
shift
;
my
(
$top
,
$bottom
,
$left
,
$right
) =
@_
;
$top
= 99999999;
$bottom
=-99999999;
$left
= 99999999;
$right
= -99999999;
my
$v
;
foreach
$v
(
$self
->vertices) {
$left
=
$v
->[0]
if
$left
>
$v
->[0];
$right
=
$v
->[0]
if
$right
<
$v
->[0];
$top
=
$v
->[1]
if
$top
>
$v
->[1];
$bottom
=
$v
->[1]
if
$bottom
<
$v
->[1];
}
return
(
$left
,
$top
,
$right
,
$bottom
);
}
sub
deletePt {
my
(
$self
,
$index
) =
@_
;
unless
((
$index
>=0) && (
$index
<@{
$self
->{
'points'
}})) {
carp
"Attempt to delete an undefined polygon vertex"
;
return
undef
;
}
my
(
$vertex
) =
splice
(@{
$self
->{
'points'
}},
$index
,1);
$self
->{
'length'
}--;
return
@$vertex
;
}
sub
offset {
my
(
$self
,
$dh
,
$dv
) =
@_
;
my
$size
=
$self
->
length
;
my
(
$i
);
for
(
$i
=0;
$i
<
$size
;
$i
++) {
my
(
$x
,
$y
)=
$self
->getPt(
$i
);
$self
->setPt(
$i
,
$x
+
$dh
,
$y
+
$dv
);
}
}
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
->
length
;
my
(
$i
);
for
(
$i
=0;
$i
<
$vertices
;
$i
++) {
my
(
$x
,
$y
) =
$self
->getPt(
$i
);
$x
=
int
(
$destL
+ (
$x
-
$srcL
) *
$factorH
);
$y
=
int
(
$destT
+ (
$y
-
$srcT
) *
$factorV
);
$self
->setPt(
$i
,
$x
,
$y
);
}
}
sub
toPt {
my
(
$self
,
$dx
,
$dy
) =
@_
;
unless
(
$self
->
length
> 0) {
$self
->addPt(
$dx
,
$dy
);
return
;
}
my
(
$x
,
$y
) =
$self
->getPt(
$self
->
length
-1);
$self
->addPt(
$x
+
$dx
,
$y
+
$dy
);
}
sub
transform($$$$$$$) {
my
(
$self
,
$sx
,
$sy
,
$rx
,
$ry
,
$tx
,
$ty
) =
@_
;
my
$size
=
$self
->
length
;
for
(
my
$i
=0;
$i
<
$size
;
$i
++) {
my
(
$x
,
$y
) =
$self
->getPt(
$i
);
$self
->setPt(
$i
,
$x
*$sx
+
$y
*$ry
+
$tx
,
$x
*$rx
+
$y
*$sy
+
$ty
);
}
}
sub
scale {
my
(
$self
,
$sx
,
$sy
,
$tx
,
$ty
) =
@_
;
$sy
=
$sx
unless
defined
$sy
;
$self
->offset(-
$tx
,-
$ty
)
if
defined
$tx
or
defined
$ty
;
$self
->transform(
$sx
,
$sy
,0,0,
$tx
,
$ty
);
}
sub
rotate {
my
(
$self
,
$r
) =
@_
;
my
(
$s
,
$c
) = (
sin
(
$r
),
cos
(
$r
));
$self
->transform(
$c
,
$c
,-
$s
,
$s
, 0,0);
}
1;