Shape::RegularPolygon - Object that treats the shape of the regular polygon
use Shape::RegularPolygon; # Create a regular polygon $polygon = new Shape::RegularPolygon; $polygon->center(100, 50); $polygon->sides(3); $polygon->radius(100); $polygon->angle(3.14 / 6); # By named parameter $polygon = new Shape::RegularPolygon(CenterX => 100, CenterY => 50, Sides => 3, Radius => 100, Angle => 3.14 / 6); # get vertexes @points = $polygon->points
Shape::RegularPolygon is a class that treats the shape of the regular polygon. This object creates and returns vertex list of specified regular polygon.
new Shape::RegularPolygon;
When the parameter is omitted, the equilateral triangle is made in default. The parameter can be set later by using method.
new Shape::RegularPolygon(named parameters);
When you construct object, you can specify some parameters. See below about named parameters.
Position of regular polygon
Building n-sides polygon. When four is specified, square is made. It must be a value of three or more.
Circumradius of regular polygon
Rotate a polygon in radians (clockwise).
Set position of polygon. Returns current position, when parameters are omitted.
$shape->center(100, 200); ($x, $y) = $shape->center;
Set the number of sides. Returns the number of sides, when parameters are omitted.
$shape->sides(5); $n = $shape->sides;
Set circumradius of polygon. Returns current circumradius, when parameters are omitted.
$shape->radius(100); $r = $shape->radius;
Set rotation angle. Returns current angle, when parameters are omitted. The rad is in radian. and clockwise.
$shape->angle(3.14 / 6); # 30 degree $rad = $shape->angle;
Returns vertexes list of polygon as follows.
( {x => x0, y => y0}, # Vertex0 {x => x1, y => y1}, # Vertex1 : : )
The following script builds png image file that regular pentagon was drawn.
#!/usr/bin/perl use strict; use warnings; use Shape::RegularPolygon; use GD; my $shape = new Shape::RegularPolygon; $shape->center(150, 100); $shape->sides(5); my $im = new GD::Image(300, 200); $im->fill(0, 0, $im->colorAllocate(0xff, 0xff, 0xff)); my $poly = new GD::Polygon; $poly->addPt($_->{x}, $_->{y}) foreach $shape->points; $im->filledPolygon($poly, $im->colorAllocate(0x80, 0xe0, 0x80)); binmode STDOUT; print $im->png;
None
Kazuyoshi Tomita, <kztomita@bit-hive.com>
Copyright (C) 2007 by Kazuyoshi Tomita
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.
To install Shape::RegularPolygon, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Shape::RegularPolygon
CPAN shell
perl -MCPAN -e shell install Shape::RegularPolygon
For more information on module installation, please visit the detailed CPAN module installation guide.