#!/usr/bin/perl

use warnings;
use strict;
use GD;
use GD::Polyline;
use constant RAD90 => 2 * atan2(1, 1);

sub square_init {
   my @squares;

   LINE: while ( <DATA> )
   {
      # allow for much needed comments and whitespace!
      next LINE if m/^\s*#/ || m/^\s+$/;
      last LINE if m/^STOP/;     # force end of list

      while ( s/\\\s*$// ) { $_ .= <DATA>; }  # join continued

      my ( $id, $points ) =
           m/^(\d+): \s+ (?: \(cent(?:re|er)\) )? \s+ (.*)$/ix;

      # create polygon object:
      my $poly = new GD::Polygon;
      for my $pair ( split /\s*;\s*/, $points )
      {
         my ( $x, $y ) = split /\s*,\s*/, $pair;
         $poly->addPt($x * $SQSIZE, $y * $SQSIZE);
      }

      my $centre = ( m/\( cent(?:re|er) \)/ix )? 1 : 0;
      push @squares, {
            centre => $centre,
            poly => $poly,
                };
      next if $centre;

      for ( 1..3 )
      {
         # create polygon object:
         my $rotpol = new GD::Polygon;
         for my $pair ( split /\s*;\s*/, $points )
         {
            my ( $x, $y ) = split /\s*,\s*/, $pair;
            $rotpol->addPt($x * $SQSIZE, $y * $SQSIZE);
         }

         $rotpol->rotate( RAD90 * $_, 0.5 * $SQSIZE, 0.5 * $SQSIZE );

         push @squares, {
            poly => $rotpol,
         }
      }

   }

   my @centres = grep { $_->{ 'centre' } } @squares;

   return \@squares, \@centres;
}

__DATA__

# id: centre  points (relative to 1x1 unit square)
#     centre  indicates squares that are rotationally 
#             symetrical
# -------------------------------------------------
1:  (centre) 0, 0; 1, 0; 1, 1; 0, 1
2:           0, 1; 1, 1; 1, 0
3:           0, 0; 0.5, 1; 1, 0
4:  (centre) 0, 0.5; 0.5, 1; 1, 0.5; 0.5, 0
5:           0.5, 0; 0.5, 1; 1, 1; 1, 0
6:           0, 0; 0, 0.5; 1, 1; 0.5, 0
7:           0.25, 0.5; 0.5, 0; 0, 0; 0.5, 0.99; \
             1, 0; 0.5, 0; 0.75, 0.5; 0.25, 0.5
8:           0, 0.5; 1, 1; 0.5, 0
9:  (centre) 0.25, 0.25; 0.25, 0.75; 0.75, 0.75; 0.75, 0.25
10:          0, 1; 0.5, 1; 0.5, 0.5; 1, 0.5; 1, 0
11:          0.5, 0.5; 0.5, 1; 1, 1; 1, 0.5
12:          0, 0.5; 1, 0.5; 0.5, 0
13:          0, 0; 0.5, 0.5; 1, 0
14:          0.5, 0.5; 0.5 ,1; 1, 0.5
15:          0.5, 1; 1, 1; 1, 0.5
16: (center)
STOP
17:          0, 0; 0, 0.5; 0.5, 0.5; 0.5, 1; 1, 1; \
             1, 0.5; 0.5, 0.5; 0.5, 0; 0, 0
18:          0, 0; 0, 1; 1, 1; 1, 0.5; 0.5, 0.5; 0.5, 0; 0, 0
19:          0, 0; 0.25, 0.5; 0.5, 0; 0.75, 0.5; 1, 0; 0, 0
20:          0.5, 0; 0, 0.5; 0.5, 0.5; 0.5, 1;1, 0.5; 0.5, 0.5; 0.5, 0
21: (centre) 0.33, 0; 0.33, 0.33; 0, 0.33; 0, 0.66; 0.33, 0.66; 0.33, 1; \
        0.66, 1; 0.66, 0.66; 1, 0.66; 1, 0.33; 0.66, 0.33; 0.66, 0; \
        0.33, 0
22:          0, 0; 0.33, 0.5; 0.66, 0.5; 1, 0; 0, 0
23:        0, 0; 0.33, 0.5; 1, 0.5; 1, 0; 0, 0