diff options
| author | andrezgz <andrezgz@gmail.com> | 2019-09-24 17:21:00 -0300 |
|---|---|---|
| committer | andrezgz <andrezgz@gmail.com> | 2019-09-24 17:21:00 -0300 |
| commit | fa16306034b25e220fdbd125f24dda30994bf9dc (patch) | |
| tree | b9cc4db122766d8517a797b4acf83255ab7ec261 | |
| parent | 64c81748bc33fdfba85006cf786b18c5181137b1 (diff) | |
| download | perlweeklychallenge-club-fa16306034b25e220fdbd125f24dda30994bf9dc.tar.gz perlweeklychallenge-club-fa16306034b25e220fdbd125f24dda30994bf9dc.tar.bz2 perlweeklychallenge-club-fa16306034b25e220fdbd125f24dda30994bf9dc.zip | |
challenge-027 andrezgz solution
| -rw-r--r-- | challenge-027/andrezgz/perl5/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-027/andrezgz/perl5/ch-2.pl | 18 |
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-027/andrezgz/perl5/ch-1.pl b/challenge-027/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..154fb49efc --- /dev/null +++ b/challenge-027/andrezgz/perl5/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-027/ +# Task #1 +# Write a script to find the intersection of two straight lines. +# The co-ordinates of the two lines should be provided as command line parameter. For example: +# The two ends of Line 1 are represented as co-ordinates (a,b) and (c,d). +# The two ends of Line 2 are represented as co-ordinates (p,q) and (r,s). +# The script should print the co-ordinates of point of intersection of the above two lines. + +use strict; +use warnings; + +# From https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection + +my $usage_messsage = <<EOT; +Usage: $0 a,b c,d p,q r,s +* Each pair represents the co-ordinates of a point. +* (a,b) (c,d) define the first segment. +* (p,q) (r,s) define the second segment. + +Example: +$0 1,1 5,1 2,0 2,6 +Intersection point (2.00,1.00) + +EOT +die $usage_messsage if @ARGV != 4; + +my ($x1,$y1) = split /,/, shift; +my ($x2,$y2) = split /,/, shift; +my ($x3,$y3) = split /,/, shift; +my ($x4,$y4) = split /,/, shift; + +my $d = ($x1-$x2) * ($y3-$y4) - ($y1-$y2) * ($x3-$x4); +die "Segments are parallels" unless $d; + +my $t = ( ($x1-$x3) * ($y3-$y4) - ($y1-$y3) * ($x3-$x4) ) / $d; +die "Intersection point out of first segment" if ($t>1 || $t<0) ; + +my $u = -1 * ( ($x1-$x2) * ($y1-$y3) - ($y1-$y2) * ($x1-$x3) ) / $d; +die "Intersection point out of second segment" if ($u>1 || $u<0) ; + +my $px = $x1 + $t * ($x2-$x1); +my $py = $y1 + $t * ($y2-$y1); + +printf "Intersection point (%.2f,%.2f)",$px,$py; diff --git a/challenge-027/andrezgz/perl5/ch-2.pl b/challenge-027/andrezgz/perl5/ch-2.pl new file mode 100644 index 0000000000..9e1ed73652 --- /dev/null +++ b/challenge-027/andrezgz/perl5/ch-2.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-027/ +# Task #2 +# Write a script that allows you to capture/display historical data. +# It could be an object or a scalar. For example +# my $x = 10; $x = 20; $x -= 5; +# After the above operations, it should list $x historical value in order. + +use strict; +use warnings; + +my $x; +my $x_ops= 'my $x = 10; $x = 20; $x -= 5;'; + +my @log; +push @log, eval $_ for (split /;/, $x_ops); +print join "\n",@log; |
