aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFung Cheok Yin <61836418+E7-87-83@users.noreply.github.com>2020-03-29 16:32:56 +0800
committerGitHub <noreply@github.com>2020-03-29 16:32:56 +0800
commit2709bdea964d2f0a1c93a05bd8d9b575e2e93663 (patch)
treec5189f5f9b0ca4080da81a557acac83b3e39324c
parentadc7db7138fb01bdc0eb0a346529b0bca5de8f0c (diff)
downloadperlweeklychallenge-club-2709bdea964d2f0a1c93a05bd8d9b575e2e93663.tar.gz
perlweeklychallenge-club-2709bdea964d2f0a1c93a05bd8d9b575e2e93663.tar.bz2
perlweeklychallenge-club-2709bdea964d2f0a1c93a05bd8d9b575e2e93663.zip
Add files via upload
-rw-r--r--ch-1a.pl93
-rw-r--r--xy.pm15
2 files changed, 108 insertions, 0 deletions
diff --git a/ch-1a.pl b/ch-1a.pl
new file mode 100644
index 0000000000..a64e7386f5
--- /dev/null
+++ b/ch-1a.pl
@@ -0,0 +1,93 @@
+#!/usr/bin/perl
+use strict;
+use xy; #upload the module with the perl script
+
+# 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;
+
+my @temp = (-$hN, -$hN);
+my $T = \@temp;
+
+
+sub translation_add_negT {
+ $_[0] -= $T->[0];
+ $_[1] -= $T->[1];
+ return ($_[0], $_[1])
+}
+
+sub translation_add_T {
+ $_[0] += $T->[0];
+ $_[1] += $T->[1];
+ return ($_[0], $_[1])
+}
+
+
+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);
+ } else {return ($_[0], $_[1])}
+}
+
+my $xcoord = 0;
+my $ycoord = $N;
+my $i = 0;
+my $matrix;
+foreach (@ARGV) {
+ $i++;
+ $xcoord++;
+ $matrix->[$i] = xy->new($_, $xcoord, $ycoord);
+ if ($xcoord == $N) {
+ $ycoord--;
+ $xcoord=0;
+ }
+}
+
+die "Not a square matrix" unless $i==$N*$N;
+
+my $newmatrix;
+
+my @coordinateplane;
+
+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;
+}
+
+
+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, the coordinates are:
+# (1,5) (2,5) (3,5) (4,5) (5,5)
+# (1,4) (2,4) (3,4) (4,4) (5,4)
+# (1,3) (2,3) (3,3) (4,3) (5,3)
+# (1,2) (2,2) (3,2) (4,2) (5,2)
+# (1,1) (2,1) (3,1) (4,1) (5,1)
diff --git a/xy.pm b/xy.pm
new file mode 100644
index 0000000000..fe161b8ced
--- /dev/null
+++ b/xy.pm
@@ -0,0 +1,15 @@
+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} }