The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Shape::RegularPolygon - Object that treats the shape of the regular polygon

SYNOPSIS

  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

DESCRIPTION

Shape::RegularPolygon is a class that treats the shape of the regular polygon. This object creates and returns vertex list of specified regular polygon.

Construction

Simple construction
  new Shape::RegularPolygon;

When the parameter is omitted, the equilateral triangle is made in default. The parameter can be set later by using method.

Construction with named parameters
  new Shape::RegularPolygon(named parameters);

When you construct object, you can specify some parameters. See below about named parameters.

Parameters

CenterX, CenterY

Position of regular polygon

Sides

Building n-sides polygon. When four is specified, square is made. It must be a value of three or more.

Radius

Circumradius of regular polygon

Angle

Rotate a polygon in radians (clockwise).

METHODS

center(x, y)

Set position of polygon. Returns current position, when parameters are omitted.

  $shape->center(100, 200);
  ($x, $y) = $shape->center;
sides(n)

Set the number of sides. Returns the number of sides, when parameters are omitted.

  $shape->sides(5);
  $n = $shape->sides;
radius(r)

Set circumradius of polygon. Returns current circumradius, when parameters are omitted.

  $shape->radius(100);
  $r = $shape->radius;
angle(rad)

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;
points()

Returns vertexes list of polygon as follows.

  (
    {x => x0, y => y0},    # Vertex0
    {x => x1, y => y1},    # Vertex1
       :
       :
  )

Example

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;

SEE ALSO

None

AUTHOR

Kazuyoshi Tomita, <kztomita@bit-hive.com>

COPYRIGHT AND LICENSE

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.