############################################################################
#
# Polyline.pm
#
# Author: Dan Harasty
# Email: harasty@cpan.org
# Version: 0.2
# Date: 2002/08/06
#
# For usage documentation: see POD at end of file
#
# For changes: see "Changes" file included with distribution
#
use strict;
package GD::Polyline;
############################################################################
#
# GD::Polyline
#
############################################################################
#
# What's this? A class with nothing but a $VERSION and @ISA?
# Below, this module overrides and adds several modules to
# the parent class, GD::Polygon. Those updated/new methods
# act on polygons and polylines, and sometimes those behaviours
# vary slightly based on whether the object is a polygon or polyline.
#
use vars qw($VERSION @ISA);
$VERSION = "0.2";
@ISA = qw(GD::Polygon);
package GD::Polygon;
############################################################################
#
# new methods on GD::Polygon
#
############################################################################
use GD;
use Carp 'croak','carp';
use vars qw($bezSegs $csr);
$bezSegs = 20; # number of bezier segs -- number of segments in each portion of the spline produces by toSpline()
$csr = 1/3; # control seg ratio -- the one possibly user-tunable parameter in the addControlPoints() algorithm
sub rotate {
my ($self, $angle, $cx, $cy) = @_;
$self->offset(-$cx,-$cy) if $cx or $cy;
$self->transform(cos($angle),sin($angle),-sin($angle),cos($angle),$cx,$cy);
}
sub centroid {
my ($self, $scale) = @_;
my ($cx,$cy);
$scale = 1 unless defined $scale;
map {$cx += $_->[0]; $cy += $_->[1]} $self->vertices();
$cx *= $scale / $self->length();
$cy *= $scale / $self->length();
return ($cx, $cy);
}
sub segLength {
my $self = shift;
my @points = $self->vertices();
my ($p1, $p2, @segLengths);
$p1 = shift @points;
# put the first vertex on the end to "close" a polygon, but not a polyline
push @points, $p1 unless $self->isa('GD::Polyline');
while ($p2 = shift @points) {
push @segLengths, _len($p1, $p2);
$p1 = $p2;
}
return @segLengths if wantarray;
my $sum;
map {$sum += $_} @segLengths;
return $sum;
}
sub segAngle {
my $self = shift;
my @points = $self->vertices();
my ($p1, $p2, @segAngles);
$p1 = shift @points;
# put the first vertex on the end to "close" a polygon, but not a polyline
push @points, $p1 unless $self->isa('GD::Polyline');
while ($p2 = shift @points) {
push @segAngles, _angle_reduce2(_angle($p1, $p2));
$p1 = $p2;
}
return @segAngles;
}
sub vertexAngle {
my $self = shift;
my @points = $self->vertices();
my ($p1, $p2, $p3, @vertexAngle);
$p1 = $points[$#points]; # last vertex
$p2 = shift @points; # current point -- the first vertex
# put the first vertex on the end to "close" a polygon, but not a polyline
push @points, $p2 unless $self->isa('GD::Polyline');
while ($p3 = shift @points) {
push @vertexAngle, _angle_reduce2(_angle($p1, $p2, $p3));
($p1, $p2) = ($p2, $p3);
}
$vertexAngle[0] = undef if defined $vertexAngle[0] and $self->isa("GD::Polyline");
return @vertexAngle if wantarray;
}
sub toSpline {
my $self = shift;
my @points = $self->vertices();
# put the first vertex on the end to "close" a polygon, but not a polyline
push @points, [$self->getPt(0)] unless $self->isa('GD::Polyline');
unless (@points > 1 and @points % 3 == 1) {
carp "Attempt to call toSpline() with invalid set of control points";
return undef;
}
my ($ap1, $dp1, $dp2, $ap2); # ap = anchor point, dp = director point
$ap1 = shift @points;
my $bez = new ref($self);
$bez->addPt(@$ap1);
while (@points) {
($dp1, $dp2, $ap2) = splice(@points, 0, 3);
for (1..$bezSegs) {
my ($t0, $t1, $c1, $c2, $c3, $c4, $x, $y);
$t1 = $_/$bezSegs;
$t0 = (1 - $t1);
# possible optimization:
# these coefficient could be calculated just once and
# cached in an array for a given value of $bezSegs
$c1 = $t0 * $t0 * $t0;
$c2 = 3 * $t0 * $t0 * $t1;
$c3 = 3 * $t0 * $t1 * $t1;
$c4 = $t1 * $t1 * $t1;
$x = $c1 * $ap1->[0] + $c2 * $dp1->[0] + $c3 * $dp2->[0] + $c4 * $ap2->[0];
$y = $c1 * $ap1->[1] + $c2 * $dp1->[1] + $c3 * $dp2->[1] + $c4 * $ap2->[1];
$bez->addPt($x, $y);
}
$ap1 = $ap2;
}
# remove the last anchor point if this is a polygon -- since it will autoclose without it
$bez->deletePt($bez->length()-1) unless $self->isa('GD::Polyline');
return $bez;
}
sub addControlPoints {
my $self = shift;
my @points = $self->vertices();
unless (@points > 1) {
carp "Attempt to call addControlPoints() with too few vertices in polyline";
return undef;
}
my $points = scalar(@points);
my @segAngles = $self->segAngle();
my @segLengths = $self->segLength();
my ($prevLen, $nextLen, $prevAngle, $thisAngle, $nextAngle);
my ($controlSeg, $pt, $ptX, $ptY, @controlSegs);
# this loop goes about creating polylines -- here called control segments --
# that hold the control points for the final set of control points
# each control segment has three points, and these are colinear
# the first and last will ultimately be "director points", and
# the middle point will ultimately be an "anchor point"
for my $i (0..$#points) {
$controlSeg = new GD::Polyline;
$pt = $points[$i];
($ptX, $ptY) = @$pt;
if ($self->isa('GD::Polyline') and ($i == 0 or $i == $#points)) {
$controlSeg->addPt($ptX, $ptY); # director point
$controlSeg->addPt($ptX, $ptY); # anchor point
$controlSeg->addPt($ptX, $ptY); # director point
next;
}
$prevLen = $segLengths[$i-1];
$nextLen = $segLengths[$i];
$prevAngle = $segAngles[$i-1];
$nextAngle = $segAngles[$i];
# make a control segment with control points (director points)
# before and after the point from the polyline (anchor point)
$controlSeg->addPt($ptX - $csr * $prevLen, $ptY); # director point
$controlSeg->addPt($ptX , $ptY); # anchor point
$controlSeg->addPt($ptX + $csr * $nextLen, $ptY); # director point
# note that:
# - the line is parallel to the x-axis, as the points have a common $ptY
# - the points are thus clearly colinear
# - the director point is a distance away from the anchor point in proportion to the length of the segment it faces
# now, we must come up with a reasonable angle for the control seg
# first, "unwrap" $nextAngle w.r.t. $prevAngle
$nextAngle -= 2*pi() until $nextAngle < $prevAngle + pi();
$nextAngle += 2*pi() until $nextAngle > $prevAngle - pi();
# next, use seg lengths as an inverse weighted average
# to "tip" the control segment toward the *shorter* segment
$thisAngle = ($nextAngle * $prevLen + $prevAngle * $nextLen) / ($prevLen + $nextLen);
# rotate the control segment to $thisAngle about it's anchor point
$controlSeg->rotate($thisAngle, $ptX, $ptY);
} continue {
# save the control segment for later
push @controlSegs, $controlSeg;
}
# post process
my $controlPoly = new ref($self);
# collect all the control segments' points in to a single control poly
foreach my $cs (@controlSegs) {
foreach my $pt ($cs->vertices()) {
$controlPoly->addPt(@$pt);
}
}
# final clean up based on poly type
if ($controlPoly->isa('GD::Polyline')) {
# remove the first and last control point
# since they are director points ...
$controlPoly->deletePt(0);
$controlPoly->deletePt($controlPoly->length()-1);
} else {
# move the first control point to the last control point
# since it is supposed to end with two director points ...
$controlPoly->addPt($controlPoly->getPt(0));
$controlPoly->deletePt(0);
}
return $controlPoly;
}
# The following helper functions are for internal
# use of this module. Input arguments of "points"
# refer to an array ref of two numbers, [$x, $y]
# as is used internally in the GD::Polygon
#
# _len()
# Find the length of a segment, passing in two points.
# Internal function; NOT a class or object method.
#
sub _len {
# my ($p1, $p2) = @_;
# return sqrt(($p2->[0]-$p1->[0])**2 + ($p2->[1]-$p1->[1])**2);
my $pt = _subtract(@_);
return sqrt($pt->[0] ** 2 + $pt->[1] **2);
}
use Math::Trig;
# _angle()
# Find the angle of... well, depends on the number of arguments:
# - one point: the angle from x-axis to the point (origin is the center)
# - two points: the angle of the vector defined from point1 to point2
# - three points:
# Internal function; NOT a class or object method.
#
sub _angle {
my ($p1, $p2, $p3) = @_;
my $angle = undef;
if (@_ == 1) {
return atan2($p1->[1], $p1->[0]);
}
if (@_ == 2) {
return _angle(_subtract($p1, $p2));
}
if (@_ == 3) {
return _angle(_subtract($p2, $p3)) - _angle(_subtract($p2, $p1));
}
}
# _subtract()
# Find the difference of two points; returns a point.
# Internal function; NOT a class or object method.
#
sub _subtract {
my ($p1, $p2) = @_;
# print(_print_point($p2), "-", _print_point($p1), "\n");
return [$p2->[0]-$p1->[0], $p2->[1]-$p1->[1]];
}
# _print_point()
# Returns a string suitable for displaying the value of a point.
# Internal function; NOT a class or object method.
#
sub _print_point {
my ($p1) = @_;
return "[" . join(", ", @$p1) . "]";
}
# _angle_reduce1()
# "unwraps" angle to interval -pi < angle <= +pi
# Internal function; NOT a class or object method.
#
sub _angle_reduce1 {
my ($angle) = @_;
$angle += 2 * pi() while $angle <= -pi();
$angle -= 2 * pi() while $angle > pi();
return $angle;
}
# _angle_reduce2()
# "unwraps" angle to interval 0 <= angle < 2 * pi
# Internal function; NOT a class or object method.
#
sub _angle_reduce2 {
my ($angle) = @_;
$angle += 2 * pi() while $angle < 0;
$angle -= 2 * pi() while $angle >= 2 * pi();
return $angle;
}
############################################################################
#
# new methods on GD::Image
#
############################################################################
sub GD::Image::polyline {
my $self = shift; # the GD::Image
my $p = shift; # the GD::Polyline (or GD::Polygon)
my $c = shift; # the color
my @points = $p->vertices();
my $p1 = shift @points;
my $p2;
while ($p2 = shift @points) {
$self->line(@$p1, @$p2, $c);
$p1 = $p2;
}
}
sub GD::Image::polydraw {
my $self = shift; # the GD::Image
my $p = shift; # the GD::Polyline or GD::Polygon
my $c = shift; # the color
return $self->polyline($p, $c) if $p->isa('GD::Polyline');
return $self->polygon($p, $c);
}
1;
__END__
=pod
=head1 NAME
GD::Polyline - Polyline object and Polygon utilities (including splines) for use with GD
=head1 SYNOPSIS
use GD;
use GD::Polyline;
# create an image
$image = GD::Image->new (500,300);
$white = $image->colorAllocate(255,255,255);
$black = $image->colorAllocate( 0, 0, 0);
$red = $image->colorAllocate(255, 0, 0);
# create a new polyline
$polyline = GD::Polyline->new;
# add some points
$polyline->addPt( 0, 0);
$polyline->addPt( 0,100);
$polyline->addPt( 50,125);
$polyline->addPt(100, 0);
# polylines can use polygon methods (and vice versa)
$polyline->offset(200,100);
# rotate 60 degrees, about the centroid
$polyline->rotate(3.14159/3, $polyline->centroid());
# scale about the centroid
$polyline->scale(1.5, 2, $polyline->centroid());
# draw the polyline
$image->polydraw($polyline,$black);
# create a spline, which is also a polyine
$spline = $polyline->addControlPoints->toSpline;
$image->polydraw($spline,$red);
# output the png
binmode STDOUT;
print $image->png;
=head1 DESCRIPTION
B extends the GD module by allowing you to create polylines. Think
of a polyline as "an open polygon", that is, the last vertex is not connected
to the first vertex (unless you expressly add the same value as both points).
For the remainder of this doc, "polyline" will refer to a GD::Polyline,
"polygon" will refer to a GD::Polygon that is not a polyline, and
"polything" and "$poly" may be either.
The big feature added to GD by this module is the means
to create splines, which are approximations to curves.
=head1 The Polyline Object
GD::Polyline defines the following class:
=over 5
=item C
A polyline object, used for storing lists of vertices prior to
rendering a polyline into an image.
=item C
Cnew> I
Create an empty polyline with no vertices.
$polyline = GD::Polyline->new;
$polyline->addPt( 0, 0);
$polyline->addPt( 0,100);
$polyline->addPt( 50,100);
$polyline->addPt(100, 0);
$image->polydraw($polyline,$black);
In fact GD::Polyline is a subclass of GD::Polygon,
so all polygon methods (such as B and B)
may be used on polylines.
Some new methods have thus been added to GD::Polygon (such as B)
and a few updated/modified/enhanced (such as B) I.
See section "New or Updated GD::Polygon Methods" for more info.
=back
Note that this module is very "young" and should be
considered subject to change in future releases, and/or
possibly folded in to the existing polygon object and/or GD module.
=head1 Updated Polygon Methods
The following methods (defined in GD.pm) are OVERRIDDEN if you use this module.
All effort has been made to provide 100% backward compatibility, but if you
can confirm that has not been achieved, please consider that a bug and let the
the author of Polyline.pm know.
=over 5
=item C
C<$poly-Escale($sx, $sy, $cx, $cy)> I