NAME

Polyplus.pm - adds methods to GD::Polygon


SYNOPSIS

        use Polyplus;

        $im = new GD::Image(200,200);

        $black = $im->colorAllocate(0,0,0);
        $red = $im->colorAllocate(255,0,0);
        $green = $im->colorAllocate(0,255,0);
        $blue = $im->colorAllocate(0,0,255);

        $poly = new Polyplus;

        $poly->addPts( 0,0, 1,0, .5,.866);

        $poly->offset(100,100);

        $poly->expand(50);
        $im->polygon ($poly, $red);

        $poly->spin(30);
        $im->polygon ($poly, $green);

        ($x, $y) = $poly->point_center;

        $poly->rotate( 180, $poly->getPt(0) ) ;

        $im->polygon ($poly, $blue);

        binmode STDOUT;

        print $im->png;


The output of that script is below:

[gif output of that script]


Download Information

It's only one file:

DESCRIPTION

A subclass of GD::Polygon adding some convenient methods.

addPts

Polyplus::addPts(x1, y1, x2, y2, ... ) object method

Like addPt, but adds an entire list at once.

getPts

Polyplus::getPts object method

Like getPt, but returns entire list at once.

open

Polyplus::open object method

An ugly hack. If you call it after adding all the points, appears to prevent the polygon from closing itself when drawn. What it really does is add points to the polygon in such a way that the polygon traces back over itself--making it an ``infinitely thin'' polygon which is, in fact, closed on itself, but looks like it isn't. Update: Hey, guess what? If you look at the code for the current polygon method, you will see that there is, in fact, an openPolygon method available! Just needs to be added to the POD, from what I can tell.

rotate

Polyplus::rotate(deg, xcenter, ycenter) object method

Rotates the polygon by deg degrees about the point xcenter, ycenter.

bounds_center

Polyplus::bounds_center object method

Returns the X and Y coordinates of the center of the smallest rectangle bounding the polygon.

point_center

Polyplus::point_center object method

Returns the X and Y coordinates of the ``centroid'' of the points of the polygon. (The centroid is the point (average of all the X coordinates, average of all Y coordinates) .)

spin

Polyplus::spin(deg) object method

Rotate the polygon deg degrees about the the center of the polygon's bounding rectangle.

centroid_spin

Polyplus::centroid_spin(degrees) object method

Rotate the polygon deg degrees about the the centroid of the polygon's points.

expand

Polyplus::expand(sx [,sy]) object method

Expands the polygon by a factor of sx in the X direction, and sy in the Y direction, but keeps the center of the bounding rectangle in the same place. Calling with one argument scales symmetrically in the X and Y directions. (Note that calling with fractional arguments shrinks the polygon. To a physicist, the brake is an accelerator.)

centroid_expand

Polyplus::centroid_expand(sx [,sy]) object method

Expands the polygon by a factor of sx in the X direction, and sy in the Y direction, but keeps the centroid of the polygon's points in the same place. Calling with one argument scales symmetrically in the X and Y directions.

An Unnecessarily Long Example:

        use Polyplus;

        $im = new GD::Image(200,200);

        $black = $im->colorAllocate (0,0,0);
        $red = $im->colorAllocate (255,0,0);

        $poly = new Polyplus;

        $poly->addPts(1,0, 1,1, 0,1, 0,0);

        $poly->offset(50,50); 

        $poly->expand(100);

        $im->polygon($poly, $red);

        foreach (1..10){
                $poly->expand( (1/50)**(1/10) );
                $poly->spin( 5 );
                $im->polygon($poly, $red);
        }

        #star of David

        $poly = new Polyplus;
        $poly->addPts(0,0, 1,0, .5,(sqrt(3) / 2) );
        $poly->offset(150,60); 
        $poly->expand(87);
        $im->polygon($poly, $red);
        $poly->centroid_spin(180);
        $im->polygon($poly, $red);

        # G
        $gee = new Polyplus;
        $gee-> addPts(
        4,2,
        4,1,
        3,0,
        1,0,
        0,1,
        0,6,
        1,7,
        3,7,
        4,6,
        4,5,
        3,5,
        5,5,
        );

        $gee->open;  #only for entertainment purposes.  Read the POD, Luke!

        #D
        $dee = new Polyplus;
        $dee->addPts(
        4,1,
        3,0,
        0,0,
        0,7,
        3,7,
        4,6,
        );

        $gee->offset(66,150); 
        $gee->expand(15);
        &zoom($gee);

        $dee->offset(133,150); 
        $dee->expand(15);
        &zoom($dee);

        open (PNG, ">test.png") or die "trying: $!\n";

        binmode PNG;

        print PNG $im->png;

        sub zoom {
                my $poly = shift;

                foreach (1..5){
                        $poly->expand( (1/50)**(1/10) );
                        $im->polygon($poly, $red);
                }
        }
The image:

[gif outpus of the above script]