Rotation in R^2 - CY's take on PWC#053 Task 1

This is a part of Perl Weekly Challenge(PWC) #053 and the followings are related to my solution. If you want to challenge yourself on Perl, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email) if possible, before reading my blog post.

Do tell me if I am wrong or you strongly oppose my statements!

Oh. Task #1 has been funner than what I thought. I would like to introduce the "advanced" version I coded; it requests a specific module to run; well, I write these codes while I am studying OO hence a package (or module?or class? Which word is more suitable?) exists).

I have supplied a simpler script on GitHub, where the idea is based on a spiral.
#the spiral for the simpler script
    3,  2,  1, 
    4,  X,  0, 
    5,  6,  7
- - - - - - - - - - - - - - -
The idea behind this so-called "advanced" version is based on linear transformations on plane. In words:

new_position_vector = ReverseTranslation(Rotation(Translation(old_position_vector))) .

Content inside xy.pm
package xy;
use strict;

sub new {
    my ($class) = @_;
    bless{
        _value=> $_[1],
        _x=>$_[2],
        _y=>$_[3],
      }, $class;
}

sub x{ $_[0]->{_x}}
sub y{ $_[0]->{_y}}
sub value{ $_[0]->{_value} }

1;
Highlights of the Content in ch-1a.pl
#!/usr/bin/perl
use strict;
use xy;
# Perl Weekly Challenge #053 Task #1
# 90/180/270 degree clockwise Rotation of a N x N square matrix
# Usage(example): put the module in proper place, then:
#                 $ ch-1a.pl 3 90 1 2 3 4 5 6 7 8 9

my $N = shift @ARGV;

my $ANGLE = shift @ARGV;

my $hN = $N/2 + 0.5;

#...
sub rcaqx {
#short for Rotation_Clockwise_A_Quarter, x stands for multiple if ($_[2]>=1) { my ($xcoord, $ycoord) = ($_[0], $_[1]); $_[0] = $ycoord; $_[1] = -$xcoord; return rcaqx($_[0], $_[1], $_[2]-1);
# I know we can skip the use of temporary variables to be more concise
# but the version now is more readable to me } else {return ($_[0], $_[1])} }
#...

sub position { return $_[0] + $N*($N-$_[1]) - 1 } for $i (1..$N*$N) { $newmatrix->[$i] = xy->new($matrix->[$i]->value, translation_add_negT(rcaqx ((translation_add_T( $matrix->[$i]->x, $matrix->[$i]->y) ), $ANGLE/90 )) ); $coordinateplane[position( $newmatrix->[$i]->x, $newmatrix->[$i]->y )] = $newmatrix->[$i]->value; }
# print out the result for (0..$N*$N-1) { if ($_ % $N == 0) {print "\n"}; printf "%3d " , $coordinateplane[$_]; }

# ref: https://en.wikipedia.org/wiki/Rotation_matrix # For N=3, the coordinates and the corresponding index of @coordinateplane: # (1,3) (2,3) (3,3) 0, 1, 2, # (1,2) (2,2) (3,2) 3, 4, 5, # (1,1) (2,1) (3,1) 6, 7, 8 # For N=5: # (1,5) (2,5) (3,5) (4,5) (5,5) 0, 1 ,2 ,3, 4, # (1,4) (2,4) (3,4) (4,4) (5,4) 5, 6, 7, 8, 9, # (1,3) (2,3) (3,3) (4,3) (5,3) 10,11,12,13,14, # (1,2) (2,2) (3,2) (4,2) (5,2) 15,16,17,18,19, # (1,1) (2,1) (3,1) (4,1) (5,1) 20,21,22,23,24 ============================
Notes/Aftermath:

  • Just discover that more than one source codes can be submitted for a same task in PWC; we just name the late comers by ch-1a.pl ch-1b.pl ch-1c.pl ...
  • Having been using the web interface of GitHub, I have a short-term goal: learning to use Git and GitHub in console.
  • Just read the review of my mentionable code for PWC #049 Task #1, reviewed by Ryan. Yet another short-term goal: Need to learn how to document and name the variables well. XP
  • Yet one more short-term goal: learn to use Test::More and Test::Deep .
  • ++: (optional) learn to use perl default debugger .
There are lots of things to be discovered in the serious coders' world... (17:43 in GMT+8 Timezone)


2 Comments

You are doing great, keep it up.

I agree with Mohammad. It's great to see you blogging now not once but twice per week, and really putting some thought into your solutions.

I hope you didn't feel I was too hard on you in Week 049. I really loved the solution.

Leave a comment

About C.-Y. Fung

user-pic a beginner in programming; passionate in mathematics, distance running and gender issues; from Hong Kong. https://twitter.com/e7_87