aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2019-09-28 15:00:04 +0200
committerLubos Kolouch <lubos@kolouch.net>2019-09-28 15:00:04 +0200
commit126470a354832f75436397a2b0dd960a518ad84b (patch)
treebf91667a3e7a9b5dcb7ea00146463b92c21d1390
parentf4fe2f01fc44195496bd0e0892f5418029241478 (diff)
downloadperlweeklychallenge-club-126470a354832f75436397a2b0dd960a518ad84b.tar.gz
perlweeklychallenge-club-126470a354832f75436397a2b0dd960a518ad84b.tar.bz2
perlweeklychallenge-club-126470a354832f75436397a2b0dd960a518ad84b.zip
Solutions #27 LK
-rw-r--r--challenge-027/lubos-kolouch/ch-1.pl64
-rw-r--r--challenge-027/lubos-kolouch/ch-2.pl41
2 files changed, 105 insertions, 0 deletions
diff --git a/challenge-027/lubos-kolouch/ch-1.pl b/challenge-027/lubos-kolouch/ch-1.pl
new file mode 100644
index 0000000000..2e6c0130f9
--- /dev/null
+++ b/challenge-027/lubos-kolouch/ch-1.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-1.pl
+#
+# USAGE: ./ch-1.pl
+#
+# DESCRIPTION: Perl Weekly Challenge - 027
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-027/
+#
+#
+# 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).
+#
+# OPTIONS: ---
+# REQUIREMENTS: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: Lubos Kolouch
+# ORGANIZATION:
+# VERSION: 1.0
+# CREATED: 09/28/2019 01:50:52 PM
+# REVISION: ---
+#===============================================================================
+
+use strict;
+use warnings;
+use feature qw/say/;
+
+die 'Usage: ch-1.pl a b c d p q r s' unless @ARGV == 8;
+
+sub get_intersection {
+
+ my ( $a, $b, $c, $d, $p, $q, $r, $s ) = @_;
+ # x1 y1 x2 y2 x3 y3 x4 y4
+
+ my $det = ( $a - $c ) * ( $q - $s ) - ( $b - $d ) * ( $p - $r );
+ return 0 if $det == 0;
+
+ # Let's assume the lines are infinitely long
+ my $px = ( $a * $d - $b * $c ) * ( $p - $r ) - ( $a - $c ) * ( $p * $s - $q * $r ) / $det;
+
+ my $py = ( $a * $d - $b * $c ) * ( $q - $s ) - ( $b - $d ) * ( $p * $s - $q * $r ) / $det;
+
+ return ( [ $px, $py ] );
+}
+
+my ($result) = &get_intersection(@ARGV);
+say "Intersection : $result->[0] , $result->[1]";
+
+# TESTS
+use Test::More;
+
+is_deeply( \&get_intersection( 0, 0, 2, 0, -4, -5, 8, 1 ), \[ 6, 0 ] );
+is_deeply( \&get_intersection( 0, 0, 2, 0, -4, -5, 8, 0 ), \[ 8, 0 ] );
+is_deeply( \&get_intersection( 0, 0, 2, 1, -4, -5, 2, 1 ), \[ 2, 1 ] );
+
+is( &get_intersection( 1, 2, 3, 4, 5, 6, 7, 8 ), 0 );
+
+done_testing;
+
diff --git a/challenge-027/lubos-kolouch/ch-2.pl b/challenge-027/lubos-kolouch/ch-2.pl
new file mode 100644
index 0000000000..d22227cda0
--- /dev/null
+++ b/challenge-027/lubos-kolouch/ch-2.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-2.pl
+#
+# USAGE: ./ch-2.pl
+#
+# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-027/
+#
+#
+#
+# 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;
+#
+#
+#
+# OPTIONS: ---
+# REQUIREMENTS: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: Lubos Kolouch
+# ORGANIZATION:
+# VERSION: 1.0
+# CREATED: 09/28/2019 02:48:06 PM
+# REVISION: ---
+#===============================================================================
+
+use strict;
+use warnings;
+use Data::Dumper;
+
+my $what = 'my $x = 10; $x = 20; $x -= 5';
+my @history;
+
+my $x;
+for (split /;/msx, $what) {
+ push @history, eval || die 'error in expression';
+}
+
+warn Dumper \@history;