diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-09-29 21:08:47 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-29 21:08:47 +0100 |
| commit | a992c9b230b68f2fce5e0c69b4a2d95f2c99e33f (patch) | |
| tree | 623054506b1ffbd1f8381f26271f324475b85ca8 /challenge-027 | |
| parent | 7f222166850ce29af7df0e10af3791e15e19f0a4 (diff) | |
| parent | 4020f0dac0e178b25e1e22684463ad0a501f1f9a (diff) | |
| download | perlweeklychallenge-club-a992c9b230b68f2fce5e0c69b4a2d95f2c99e33f.tar.gz perlweeklychallenge-club-a992c9b230b68f2fce5e0c69b4a2d95f2c99e33f.tar.bz2 perlweeklychallenge-club-a992c9b230b68f2fce5e0c69b4a2d95f2c99e33f.zip | |
Merge pull request #683 from choroba/ech27
Add blogpost for 027 by E. Choroba
Diffstat (limited to 'challenge-027')
| -rw-r--r-- | challenge-027/e-choroba/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-027/e-choroba/perl5/ch-1.pl | 17 |
2 files changed, 13 insertions, 5 deletions
diff --git a/challenge-027/e-choroba/blog.txt b/challenge-027/e-choroba/blog.txt new file mode 100644 index 0000000000..a7c6fab7eb --- /dev/null +++ b/challenge-027/e-choroba/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/e_choroba/2019/09/perl-weekly-challenge-027-intersection-of-straight-lines-and-historical-data.html diff --git a/challenge-027/e-choroba/perl5/ch-1.pl b/challenge-027/e-choroba/perl5/ch-1.pl index 916098c0c0..0375971980 100755 --- a/challenge-027/e-choroba/perl5/ch-1.pl +++ b/challenge-027/e-choroba/perl5/ch-1.pl @@ -13,8 +13,9 @@ sub line { ($A, $B, $C) = (1, 0, 0); } } else { - ($A, $B, $C) = (($y2 - $y1) / ($x1 - $x2), 1, - -($x1 * ($y2 - $y1) / ($x1 - $x2) + $y1)); + ($A, $B, $C) = (($y2 - $y1) / ($x1 - $x2), + 1, + $x1 * ($y1 - $y2) / ($x1 - $x2) - $y1); } return $A, $B, $C } @@ -25,8 +26,9 @@ sub intersection { die 'No intersection' if $c1 != $c2; die 'Identical lines' if $c1 == $c2; } - my $y = ($a2 * $c1 / $a1 - $c2) * $a1 / ($b2 * $a1 - $a2 * $b1); - my $x = (-$b1 * $y - $c1) / $a1; + my $y = ($a2 * $c1 - $c2 * $a1) / ($a1 * $b2 - $a2 * $b1); + my $x = $a1 ? (-$b1 * $y - $c1) / $a1 + : (-$b2 * $y - $c2) / $a2; return $x, $y } @@ -66,6 +68,11 @@ is_deeply [ intersection( line(4, 19, 3.5, 17.5) ) ], [4.5, 20.5]; +is_deeply [ intersection( + line(1, 1, 5, 1), + line(1, 1, 1, 5), +) ], [ 1, 1 ]; + throws_ok { intersection( line(0, 0, 1, 1), line(2, 2, 3, 3) @@ -76,4 +83,4 @@ throws_ok { intersection( line(0, 2, 1, 3) ) } qr/No intersection/; -done_testing(13); +done_testing(14); |
