home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Polyline.pm < prev    next >
Encoding:
Text File  |  2002-08-05  |  22.9 KB  |  798 lines

  1. ############################################################################
  2. #
  3. # Polyline.pm
  4. #
  5. # Author:    Dan Harasty
  6. # Email:    harasty@cpan.org
  7. # Version:    0.2
  8. # Date:        2002/08/06
  9. #
  10. # For usage documentation: see POD at end of file
  11. #
  12. # For changes: see "Changes" file included with distribution
  13. #
  14.  
  15. use strict;
  16.  
  17. package GD::Polyline;
  18.  
  19. ############################################################################
  20. #
  21. # GD::Polyline
  22. #
  23. ############################################################################
  24. #
  25. # What's this?  A class with nothing but a $VERSION and and @ISA?
  26. # Below, this module overrides and adds several modules to 
  27. # the parent class, GD::Polygon.  Those updated/new methods 
  28. # act on polygons and polylines, and sometimes those behaviours
  29. # vary slightly based on whether the object is a polygon or polyine.
  30. #
  31.  
  32. use vars qw($VERSION @ISA);
  33. $VERSION = "0.2";
  34. @ISA = qw(GD::Polygon);
  35.  
  36.  
  37. package GD::Polygon;
  38.  
  39. ############################################################################
  40. #
  41. # new methods on GD::Polygon
  42. #
  43. ############################################################################
  44.  
  45. use GD;
  46. use Carp 'croak','carp';
  47.  
  48. use vars qw($bezSegs $csr);
  49. $bezSegs = 20;    # number of bezier segs -- number of segments in each portion of the spline produces by toSpline()
  50. $csr = 1/3;        # control seg ratio -- the one possibly user-tunable parameter in the addControlPoints() algorithm
  51.  
  52. sub scale {
  53.     my($self, $sx, $sy, $cx, $cy) = @_;
  54.     $self->offset(-$cx,-$cy) if $cx or $cy;
  55.     $self->transform($sx,0,0,$sy,$cx,$cy);
  56. }
  57.  
  58.  
  59. sub rotate {
  60.     my ($self, $angle, $cx, $cy) = @_;
  61.     $self->offset(-$cx,-$cy) if $cx or $cy;
  62.     $self->transform(cos($angle),sin($angle),-sin($angle),cos($angle),$cx,$cy);
  63. }
  64.  
  65. sub centroid {
  66.     my ($self, $scale) = @_;
  67.     my ($cx,$cy);
  68.     $scale = 1 unless defined $scale;
  69.     
  70.     map {$cx += $_->[0]; $cy += $_->[1]} $self->vertices();
  71.     
  72.     $cx *= $scale / $self->length();
  73.     $cy *= $scale / $self->length();
  74.  
  75.     return ($cx, $cy);    
  76. }
  77.  
  78.  
  79. sub segLength {
  80.     my $self = shift;
  81.     my @points = $self->vertices();
  82.  
  83.     my ($p1, $p2, @segLengths);
  84.     
  85.     $p1 = shift @points;
  86.     
  87.     # put the first vertex on the end to "close" a polygon, but not a polyline
  88.     push @points, $p1 unless $self->isa('GD::Polyline');
  89.     
  90.     while ($p2 = shift @points) {
  91.         push @segLengths, _len($p1, $p2);
  92.         $p1 = $p2;
  93.     }
  94.     
  95.     return @segLengths if wantarray;
  96.     
  97.     my $sum;
  98.     map {$sum += $_} @segLengths;
  99.     return $sum;
  100. }
  101.  
  102. sub segAngle {
  103.     my $self = shift;
  104.     my @points = $self->vertices();
  105.  
  106.     my ($p1, $p2, @segAngles);
  107.     
  108.     $p1 = shift @points;
  109.     
  110.     # put the first vertex on the end to "close" a polygon, but not a polyline
  111.     push @points, $p1 unless $self->isa('GD::Polyline');
  112.     
  113.     while ($p2 = shift @points) {
  114.         push @segAngles, _angle_reduce2(_angle($p1, $p2));
  115.         $p1 = $p2;
  116.     }
  117.     
  118.     return @segAngles;
  119. }
  120.  
  121. sub vertexAngle {
  122.     my $self = shift;
  123.     my @points = $self->vertices();
  124.  
  125.     my ($p1, $p2, $p3, @vertexAngle);
  126.  
  127.     $p1 = $points[$#points];    # last vertex
  128.     $p2 = shift @points;        # current point -- the first vertex
  129.  
  130.     # put the first vertex on the end to "close" a polygon, but not a polyline
  131.     push @points, $p2 unless $self->isa('GD::Polyline');
  132.     
  133.     while ($p3 = shift @points) {
  134.         push @vertexAngle, _angle_reduce2(_angle($p1, $p2, $p3));
  135.         ($p1, $p2) = ($p2, $p3);
  136.     }
  137.     
  138.     $vertexAngle[0] = undef if defined $vertexAngle[0] and $self->isa("GD::Polyline");
  139.     
  140.     return @vertexAngle if wantarray;
  141.     
  142. }
  143.  
  144.  
  145.  
  146. sub toSpline {
  147.     my $self = shift;
  148.     my @points = $self->vertices();
  149.  
  150.     # put the first vertex on the end to "close" a polygon, but not a polyline    
  151.     push @points, [$self->getPt(0)] unless $self->isa('GD::Polyline');
  152.  
  153.     unless (@points > 1 and @points % 3 == 1) {
  154.         carp "Attempt to call toSpline() with invalid set of control points";
  155.         return undef;
  156.     }
  157.     
  158.     my ($ap1, $dp1, $dp2, $ap2); # ap = anchor point, dp = director point
  159.     $ap1 = shift @points;
  160.  
  161.     my $bez = new ref($self);
  162.  
  163.     $bez->addPt(@$ap1);
  164.     
  165.     while (@points) {
  166.         ($dp1, $dp2, $ap2) = splice(@points, 0, 3);
  167.         
  168.         for (1..$bezSegs) {
  169.             my ($t0, $t1, $c1, $c2, $c3, $c4, $x, $y); 
  170.             
  171.             $t1 = $_/$bezSegs;
  172.             $t0 = (1 - $t1);
  173.             
  174.             # possible optimization:
  175.             # these coefficient could be calculated just once and 
  176.             # cached in an array for a given value of $bezSegs
  177.             
  178.             $c1 =     $t0 * $t0 * $t0;
  179.             $c2 = 3 * $t0 * $t0 * $t1;
  180.             $c3 = 3 * $t0 * $t1 * $t1;
  181.             $c4 =     $t1 * $t1 * $t1;
  182.     
  183.             $x = $c1 * $ap1->[0] + $c2 * $dp1->[0] + $c3 * $dp2->[0] + $c4 * $ap2->[0];
  184.             $y = $c1 * $ap1->[1] + $c2 * $dp1->[1] + $c3 * $dp2->[1] + $c4 * $ap2->[1];
  185.         
  186.             $bez->addPt($x, $y);
  187.         }
  188.         
  189.         $ap1 = $ap2;
  190.     }
  191.     
  192.     # remove the last anchor point if this is a polygon -- since it will autoclose without it
  193.     $bez->deletePt($bez->length()-1) unless $self->isa('GD::Polyline');
  194.     
  195.     return $bez;
  196. }
  197.  
  198. sub addControlPoints {
  199.     my $self = shift;
  200.     my @points = $self->vertices();
  201.  
  202.     unless (@points > 1) {
  203.         carp "Attempt to call addControlPoints() with too few vertices in polyline";
  204.         return undef;
  205.     }
  206.     
  207.     my $points = scalar(@points);
  208.     my @segAngles  = $self->segAngle();
  209.     my @segLengths = $self->segLength();
  210.  
  211.     my ($prevLen, $nextLen, $prevAngle, $thisAngle, $nextAngle);
  212.     my ($controlSeg, $pt, $ptX, $ptY, @controlSegs);
  213.  
  214.     # this loop goes about creating polylines -- here called control segments --
  215.     # that hold the control points for the final set of control points
  216.     
  217.     # each control segment has three points, and these are colinear
  218.     
  219.     # the first and last will ultimately be "director points", and
  220.     # the middle point will ultimately be an "anchor point"
  221.     
  222.     for my $i (0..$#points) {
  223.  
  224.         $controlSeg = new GD::Polyline;
  225.  
  226.         $pt = $points[$i];
  227.         ($ptX, $ptY) = @$pt;
  228.  
  229.         if ($self->isa('GD::Polyline') and ($i == 0 or $i == $#points)) {
  230.             $controlSeg->addPt($ptX, $ptY);    # director point
  231.             $controlSeg->addPt($ptX, $ptY);    # anchor point
  232.             $controlSeg->addPt($ptX, $ptY);    # director point
  233.             next;    
  234.         }
  235.                 
  236.         $prevLen = $segLengths[$i-1];
  237.         $nextLen = $segLengths[$i];
  238.         $prevAngle = $segAngles[$i-1];
  239.         $nextAngle = $segAngles[$i];        
  240.         
  241.         # make a control segment with control points (director points)
  242.         # before and after the point from the polyline (anchor point)
  243.         
  244.         $controlSeg->addPt($ptX - $csr * $prevLen, $ptY);    # director point
  245.         $controlSeg->addPt($ptX                  , $ptY);    # anchor point  
  246.         $controlSeg->addPt($ptX + $csr * $nextLen, $ptY);    # director point
  247.  
  248.         # note that:
  249.         # - the line is parallel to the x-axis, as the points have a common $ptY
  250.         # - the points are thus clearly colinear
  251.         # - the director point is a distance away from the anchor point in proportion to the length of the segment it faces
  252.         
  253.         # now, we must come up with a reasonable angle for the control seg
  254.         #  first, "unwrap" $nextAngle w.r.t. $prevAngle
  255.         $nextAngle -= 2*pi() until $nextAngle < $prevAngle + pi();
  256.         $nextAngle += 2*pi() until $nextAngle > $prevAngle - pi();
  257.         #  next, use seg lengths as an inverse weighted average
  258.         #  to "tip" the control segment toward the *shorter* segment
  259.         $thisAngle = ($nextAngle * $prevLen + $prevAngle * $nextLen) / ($prevLen + $nextLen);
  260.                        
  261.         # rotate the control segment to $thisAngle about it's anchor point
  262.         $controlSeg->rotate($thisAngle, $ptX, $ptY);
  263.         
  264.     } continue {
  265.         # save the control segment for later
  266.         push @controlSegs, $controlSeg;
  267.         
  268.     }
  269.     
  270.     # post process
  271.     
  272.     my $controlPoly = new ref($self);
  273.  
  274.     # collect all the control segments' points in to a single control poly
  275.     
  276.     foreach my $cs (@controlSegs) {
  277.         foreach my $pt ($cs->vertices()) {
  278.             $controlPoly->addPt(@$pt);
  279.         }
  280.     }
  281.     
  282.     # final clean up based on poly type
  283.     
  284.     if ($controlPoly->isa('GD::Polyline')) {
  285.         # remove the first and last control point
  286.         # since they are director points ... 
  287.         $controlPoly->deletePt(0);
  288.         $controlPoly->deletePt($controlPoly->length()-1);
  289.     } else {
  290.         # move the first control point to the last control point
  291.         # since it is supposed to end with two director points ... 
  292.         $controlPoly->addPt($controlPoly->getPt(0));
  293.         $controlPoly->deletePt(0);
  294.     }
  295.     
  296.     return $controlPoly;
  297. }
  298.  
  299.  
  300. # The following helper functions are for internal
  301. # use of this module.  Input arguments of "points"
  302. # refer to an array ref of two numbers, [$x, $y]
  303. # as is used internally in the GD::Polygon
  304. #
  305. # _len()
  306. # Find the length of a segment, passing in two points.
  307. # Internal function; NOT a class or object method.
  308. #
  309. sub _len {
  310. #    my ($p1, $p2) = @_;
  311. #    return sqrt(($p2->[0]-$p1->[0])**2 + ($p2->[1]-$p1->[1])**2);
  312.     my $pt = _subtract(@_);
  313.     return sqrt($pt->[0] ** 2 + $pt->[1] **2);
  314. }
  315.  
  316. use Math::Trig;
  317.  
  318. # _angle()
  319. # Find the angle of... well, depends on the number of arguments:
  320. # - one point: the angle from x-axis to the point (origin is the center)
  321. # - two points: the angle of the vector defined from point1 to point2
  322. # - three points: 
  323. # Internal function; NOT a class or object method.
  324. #
  325. sub _angle {
  326.     my ($p1, $p2, $p3) = @_;
  327.     my $angle = undef;
  328.     if (@_ == 1) {
  329.         return atan2($p1->[1], $p1->[0]);
  330.     }
  331.     if (@_ == 2) {
  332.         return _angle(_subtract($p1, $p2));
  333.     }
  334.     if (@_ == 3) {
  335.         return _angle(_subtract($p2, $p3)) - _angle(_subtract($p2, $p1));
  336.     }
  337. }
  338.  
  339. # _subtract()
  340. # Find the difference of two points; returns a point.
  341. # Internal function; NOT a class or object method.
  342. #
  343. sub _subtract {
  344.     my ($p1, $p2) = @_;
  345. #    print(_print_point($p2), "-", _print_point($p1), "\n");
  346.     return [$p2->[0]-$p1->[0], $p2->[1]-$p1->[1]];    
  347. }
  348.  
  349. # _print_point()
  350. # Returns a string suitable for displaying the value of a point.
  351. # Internal function; NOT a class or object method.
  352. #
  353. sub _print_point {
  354.     my ($p1) = @_;
  355.     return "[" . join(", ", @$p1) . "]";
  356. }
  357.  
  358. # _angle_reduce1()
  359. # "unwraps" angle to interval -pi < angle <= +pi
  360. # Internal function; NOT a class or object method.
  361. #
  362. sub _angle_reduce1 {
  363.     my ($angle) = @_;
  364.     $angle += 2 * pi() while $angle <= -pi();
  365.     $angle -= 2 * pi() while $angle >   pi();
  366.     return $angle;
  367. }
  368.  
  369. # _angle_reduce2()
  370. # "unwraps" angle to interval 0 <= angle < 2 * pi
  371. # Internal function; NOT a class or object method.
  372. #
  373. sub _angle_reduce2 {
  374.     my ($angle) = @_;
  375.     $angle += 2 * pi() while $angle <  0;
  376.     $angle -= 2 * pi() while $angle >= 2 * pi();
  377.     return $angle;
  378. }
  379.  
  380. ############################################################################
  381. #
  382. # new methods on GD::Image
  383. #
  384. ############################################################################
  385.  
  386. sub GD::Image::polyline {
  387.     my $self = shift;    # the GD::Image
  388.     my $p    = shift;    # the GD::Polyline (or GD::Polygon)
  389.     my $c    = shift;    # the color
  390.     
  391.     my @points = $p->vertices();
  392.     my $p1 = shift @points;
  393.     my $p2;
  394.     while ($p2 = shift @points) {
  395.         $self->line(@$p1, @$p2, $c);
  396.         $p1 = $p2;
  397.     }
  398. }        
  399.  
  400. sub GD::Image::polydraw {
  401.     my $self = shift;    # the GD::Image
  402.     my $p    = shift;    # the GD::Polyline or GD::Polygon
  403.     my $c    = shift;    # the color
  404.     
  405.        return $self->polyline($p, $c) if $p->isa('GD::Polyline');
  406.        return $self->polygon($p, $c);        
  407. }        
  408.  
  409.  
  410. 1;
  411. __END__
  412.  
  413. =pod
  414.  
  415. =head1 NAME
  416.  
  417. GD::Polyline - Polyline object and Polygon utilities (including splines) for use with GD
  418.  
  419. =head1 SYNOPSIS
  420.  
  421.     use GD;
  422.     use GD::Polyline;
  423.  
  424.     # create an image
  425.     $image = new GD::Image (500,300);
  426.     $white  = $image->colorAllocate(255,255,255);
  427.     $black  = $image->colorAllocate(  0,  0,  0);
  428.     $red    = $image->colorAllocate(255,  0,  0);
  429.     
  430.     # create a new polyline
  431.     $polyline = new GD::Polyline;
  432.             
  433.     # add some points
  434.     $polyline->addPt(  0,  0);
  435.     $polyline->addPt(  0,100);
  436.     $polyline->addPt( 50,125);
  437.     $polyline->addPt(100,  0);
  438.  
  439.     # polylines can use polygon methods (and vice versa)
  440.     $polyline->offset(200,100);
  441.     
  442.     # rotate 60 degrees, about the centroid
  443.     $polyline->rotate(3.14159/3, $polyline->centroid()); 
  444.     
  445.     # scale about the centroid
  446.     $polyline->scale(1.5, 2, $polyline->centroid());  
  447.     
  448.     # draw the polyline
  449.     $image->polydraw($polyline,$black);
  450.     
  451.     # create a spline, which is also a polyine
  452.     $spline = $polyline->addControlPoints->toSpline;
  453.     $image->polydraw($spline,$red);
  454.  
  455.     # output the png
  456.     binmode STDOUT;
  457.     print $image->png;
  458.  
  459. =head1 DESCRIPTION
  460.  
  461. B<Polyline.pm> extends the GD module by allowing you to create polylines.  Think
  462. of a polyline as "an open polygon", that is, the last vertex is not connected
  463. to the first vertex (unless you expressly add the same value as both points).
  464.  
  465. For the remainder of this doc, "polyline" will refer to a GD::Polyline,
  466. "polygon" will refer to a GD::Polygon that is not a polyline, and
  467. "polything" and "$poly" may be either.
  468.  
  469. The big feature added to GD by this module is the means
  470. to create splines, which are approximations to curves.
  471.  
  472. =head1 The Polyline Object
  473.  
  474. GD::Polyline defines the following class:
  475.  
  476. =over 5
  477.  
  478. =item C<GD::Polyline>
  479.  
  480. A polyline object, used for storing lists of vertices prior to
  481. rendering a polyline into an image.
  482.  
  483. =item C<new>
  484.  
  485. C<GD::Polyline-E<gt>new> I<class method>
  486.  
  487. Create an empty polyline with no vertices.
  488.  
  489.     $polyline = new GD::Polyline;
  490.  
  491.     $polyline->addPt(  0,  0);
  492.     $polyline->addPt(  0,100);
  493.     $polyline->addPt( 50,100);
  494.     $polyline->addPt(100,  0);
  495.  
  496.     $image->polydraw($polyline,$black);
  497.  
  498. In fact GD::Polyline is a subclass of GD::Polygon, 
  499. so all polygon methods (such as B<offset> and B<transform>)
  500. may be used on polylines.
  501. Some new methods have thus been added to GD::Polygon (such as B<rotate>)
  502. and a few updated/modified/enhanced (such as B<scale>) I<in this module>.  
  503. See section "New or Updated GD::Polygon Methods" for more info.
  504.  
  505. =back
  506.  
  507. Note that this module is very "young" and should be
  508. considered subject to change in future releases, and/or
  509. possibly folded in to the existing polygon object and/or GD module.
  510.  
  511. =head1 Updated Polygon Methods
  512.  
  513. The following methods (defined in GD.pm) are OVERRIDDEN if you use this module.
  514.  
  515. All effort has been made to provide 100% backward compatibility, but if you
  516. can confirm that has not been achieved, please consider that a bug and let the
  517. the author of Polyline.pm know.
  518.  
  519. =over 5
  520.  
  521. =item C<scale>
  522.  
  523. C<$poly-E<gt>scale($sx, $sy, $cx, $cy)> I<object method -- UPDATE to GD::Polygon::scale>
  524.  
  525. Scale a polything in along x-axis by $sx and along the y-axis by $sy,
  526. about centery point ($cx, $cy).
  527.  
  528. Center point ($cx, $cy) is optional -- if these are omitted, the function
  529. will scale about the origin.
  530.  
  531. To flip a polything, use a scale factor of -1.  For example, to
  532. flip the polything top to bottom about line y = 100, use:
  533.  
  534.     $poly->scale(1, -1, 0, 100);
  535.  
  536. =back
  537.  
  538. =head1 New Polygon Methods
  539.  
  540. The following methods are added to GD::Polygon, and thus can be used
  541. by polygons and polylines.
  542.  
  543. Don't forget: a polyline is a GD::Polygon, so GD::Polygon methods 
  544. like offset() can be used, and they can be used in
  545. GD::Image methods like filledPolygon().
  546.  
  547. =over 5
  548.  
  549. =item C<rotate>
  550.  
  551. C<$poly-E<gt>rotate($angle, $cx, $cy)> I<object method>
  552.  
  553. Rotate a polything through $angle (clockwise, in radians) about center point ($cx, $cy).
  554.  
  555. Center point ($cx, $cy) is optional -- if these are omitted, the function
  556. will rotate about the origin
  557.  
  558. In this function and other angle-oriented functions in GD::Polyline,
  559. positive $angle corrensponds to clockwise rotation.  This is opposite
  560. of the usual Cartesian sense, but that is because the raster is opposite
  561. of the usual Cartesian sense in that the y-axis goes "down".
  562.  
  563. =item C<centroid>
  564.  
  565. C<($cx, $cy) = $poly-E<gt>centroid($scale)> I<object method>
  566.  
  567. Calculate and return ($cx, $cy), the centroid of the vertices of the polything.
  568. For example, to rotate something 180 degrees about it's centroid:
  569.  
  570.     $poly->rotate(3.14159, $poly->centroid());
  571.  
  572. $scale is optional; if supplied, $cx and $cy are multiplied by $scale 
  573. before returning.  The main use of this is to shift an polything to the 
  574. origin like this:
  575.  
  576.     $poly->offset($poly->centroid(-1));
  577.  
  578. =item C<segLength>
  579.  
  580. C<@segLengths = $poly-E<gt>segLength()> I<object method>
  581.  
  582. In array context, returns an array the lengths of the segments in the polything.
  583. Segment n is the segment from vertex n to vertex n+1.
  584. Polygons have as many segments as vertices; polylines have one fewer.
  585.  
  586. In a scalar context, returns the sum of the array that would have been returned
  587. in the array context.
  588.  
  589. =item C<segAngle>
  590.  
  591. C<@segAngles = $poly-E<gt>segAngle()> I<object method>
  592.  
  593. Returns an array the angles of each segment from the x-axis.
  594. Segment n is the segment from vertex n to vertex n+1.
  595. Polygons have as many segments as vertices; polylines have one fewer.
  596.  
  597. Returned angles will be on the interval 0 <= $angle < 2 * pi and
  598. angles increase in a clockwise direction.
  599.  
  600. =item C<vertexAngle>
  601.  
  602. C<@vertexAngles = $poly-E<gt>vertexAngle()> I<object method>
  603.  
  604. Returns an array of the angles between the segment into and out of each vertex.
  605. For polylines, the vertex angle at vertex 0 and the last vertex are not defined;
  606. however $vertexAngle[0] will be undef so that $vertexAngle[1] will correspond to 
  607. vertex 1.
  608.  
  609. Returned angles will be on the interval 0 <= $angle < 2 * pi and
  610. angles increase in a clockwise direction.
  611.  
  612. Note that this calculation does not attempt to figure out the "interior" angle
  613. with respect to "inside" or "outside" the polygon, but rather, 
  614. just the angle between the adjacent segments
  615. in a clockwise sense.  Thus a polygon with all right angles will have vertex
  616. angles of either pi/2 or 3*pi/2, depending on the way the polygon was "wound".
  617.  
  618. =item C<toSpline>
  619.  
  620. C<$poly-E<gt>toSpline()> I<object method & factory method>
  621.  
  622. Create a new polything which is a reasonably smooth curve
  623. using cubic spline algorithms, often referred to as Bezier
  624. curves.  The "source" polything is called the "control polything".
  625. If it is a polyline, the control polyline must 
  626. have 4, 7, 10, or some number of vertices of equal to 3n+1.
  627. If it is a polygon, the control polygon must 
  628. have 3, 6, 9, or some number of vertices of equal to 3n.
  629.  
  630.     $spline = $poly->toSpline();    
  631.     $image->polydraw($spline,$red);
  632.  
  633. In brief, groups of four points from the control polyline
  634. are considered "control
  635. points" for a given portion of the spline: the first and
  636. fourth are "anchor points", and the spline passes through
  637. them; the second and third are "director points".  The
  638. spline does not pass through director points, however the
  639. spline is tangent to the line segment from anchor point to
  640. adjacent director point.
  641.  
  642. The next portion of the spline reuses the previous portion's
  643. last anchor point.  The spline will have a cusp
  644. (non-continuous slope) at an anchor point, unless the anchor
  645. points and its adjacent director point are colinear.
  646.  
  647. In the current implementation, toSpline() return a fixed
  648. number of segments in the returned polyline per set-of-four
  649. control points.  In the future, this and other parameters of
  650. the algorithm may be configurable.
  651.  
  652. =item C<addControlPoints>
  653.  
  654. C<$polyline-E<gt>addControlPoints()> I<object method & factory method>
  655.  
  656. So you say: "OK.  Splines sound cool.  But how can I
  657. get my anchor points and its adjacent director point to be
  658. colinear so that I have a nice smooth curves from my
  659. polyline?"  Relax!  For The Lazy: addControlPoints() to the
  660. rescue.
  661.  
  662. addControlPoints() returns a polyline that can serve
  663. as the control polyline for toSpline(), which returns
  664. another polyline which is the spline.  Is your head spinning
  665. yet?  Think of it this way:
  666.  
  667. =over 5
  668.  
  669. =item +
  670.  
  671. If you have a polyline, and you have already put your
  672. control points where you want them, call toSpline() directly.
  673. Remember, only every third vertex will be "on" the spline.
  674.  
  675. You get something that looks like the spline "inscribed" 
  676. inside the control polyline.
  677.  
  678. =item +
  679.  
  680. If you have a polyline, and you want all of its vertices on
  681. the resulting spline, call addControlPoints() and then
  682. toSpline():
  683.  
  684.     $control = $polyline->addControlPoints();    
  685.     $spline  = $control->toSpline();    
  686.     $image->polyline($spline,$red);
  687.  
  688. You get something that looks like the control polyline "inscribed" 
  689. inside the spline.
  690.  
  691. =back
  692.  
  693. Adding "good" control points is subjective; this particular 
  694. algorithm reveals its author's tastes.  
  695. In the future, you may be able to alter the taste slightly
  696. via parameters to the algorithm.  For The Hubristic: please 
  697. build a better one!
  698.  
  699. And for The Impatient: note that addControlPoints() returns a
  700. polyline, so you can pile up the the call like this, 
  701. if you'd like:
  702.  
  703.     $image->polyline($polyline->addControlPoints()->toSpline(),$mauve);
  704.  
  705. =back
  706.  
  707. =head1 New GD::Image Methods
  708.  
  709. =over 5
  710.  
  711. =item C<polyline>
  712.  
  713. C<$image-E<gt>polyline(polyline,color)> I<object method> 
  714.  
  715.     $image->polyline($polyline,$black)
  716.  
  717. This draws a polyline with the specified color.  
  718. Both real color indexes and the special 
  719. colors gdBrushed, gdStyled and gdStyledBrushed can be specified.
  720.  
  721. Neither the polyline() method or the polygon() method are very
  722. picky: you can call either method with either a GD::Polygon or a GD::Polyline.
  723. The I<method> determines if the shape is "closed" or "open" as drawn, I<not>
  724. the object type.
  725.  
  726. =item C<polydraw>
  727.  
  728. C<$image-E<gt>polydraw(polything,color)> I<object method> 
  729.  
  730.     $image->polydraw($poly,$black)
  731.  
  732. This method draws the polything as expected (polygons are closed, 
  733. polylines are open) by simply checking the object type and calling 
  734. either $image->polygon() or $image->polyline().
  735.  
  736. =back
  737.  
  738. =head1 Examples
  739.  
  740. Please see file "polyline-examples.pl" that is included with the distribution.
  741.  
  742. =head1 See Also
  743.  
  744. For more info on Bezier splines, see http://www.webreference.com/dlab/9902/bezier.html.
  745.  
  746. =head1 Future Features
  747.  
  748. On the drawing board are additional features such as:
  749.  
  750.     - polygon winding algorithms (to determine if a point is "inside" or "outside" the polygon)
  751.  
  752.     - new polygon from bounding box
  753.     
  754.     - find bounding polygon (tightest fitting simple convex polygon for a given set of vertices)
  755.     
  756.     - addPts() method to add many points at once
  757.     
  758.     - clone() method for polygon
  759.     
  760.     - functions to interwork GD with SVG
  761.     
  762. Please provide input on other possible features you'd like to see.
  763.  
  764. =head1 Author
  765.  
  766. This module has been written by Daniel J. Harasty.  
  767. Please send questions, comments, complaints, and kudos to him
  768. at harasty@cpan.org.
  769.  
  770. Thanks to Lincoln Stein for input and patience with me and this, 
  771. my first CPAN contribution.
  772.  
  773. =head1 Copyright Information
  774.  
  775. The Polyline.pm module is copyright 2002, Daniel J. Harasty.  It is
  776. distributed under the same terms as Perl itself.  See the "Artistic
  777. License" in the Perl source code distribution for licensing terms.
  778.  
  779. The latest version of Polyline.pm is available at 
  780. your favorite CPAN repository and/or 
  781. along with GD.pm by Lincoln D. Stein at http://stein.cshl.org/WWW/software/GD.
  782.  
  783. =cut
  784.  
  785. # future:
  786. #    addPts
  787. #    boundingPolygon
  788. #    addControlPoints('method' => 'fitToSegments', 'numSegs' => 10)
  789. #    toSpline('csr' => 1/4);
  790.  
  791. #    GD::Color
  792. #        colorMap('x11' | 'svg' | <filename> )
  793. #        colorByName($image, 'orange');
  794. #        setImage($image);
  795. #        cbn('orange');
  796. #
  797. #
  798. #