From 77226b077dde01b4a1c1af69cb1ee1245c099f0e Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 22 Mar 2023 21:38:45 +0100 Subject: Challenge 027 task 1 --- challenge-027/jo-37/perl/ch-1.pl | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 challenge-027/jo-37/perl/ch-1.pl diff --git a/challenge-027/jo-37/perl/ch-1.pl b/challenge-027/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..6a759b6fa2 --- /dev/null +++ b/challenge-027/jo-37/perl/ch-1.pl @@ -0,0 +1,83 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0 '!float'; +use warnings FATAL => 'all'; +use PDL; +use PDL::NiceSlice; +use Data::Dump qw(dd pp); +use experimental qw(signatures postderef); + +our ($tests, $examples, $verbose); + +run_tests() if $tests || $examples; # does not return + +die <xchg(0, 1); + # A 2x2 rotation matrix: + state $rot = pdl [0, 1], [-1, 0]; + # Build orthogonal vectors as the rotated directions. + my $a = ($lines((1)) - $lines((0))) x $rot; + # Extract the starting points. + my $p = $lines((0))->xchg(0, 1); + # The right hand side. + my $b = ($a x $p)->diagonal(0,1); + # Find the intersection: + my $x = lu_backsub(lu_decomp($a), $b); + + $x->clump(-1)->unpdl; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + } + + SKIP: { + skip "tests" unless $tests; + + is line_intersection(1, 2, 5, 4, 2, 5, 4, 1), [3, 3], 'symmetric test'; + is line_intersection(-1, -1, 1, 1, -1, 1, 1, -1), [0, 0], 'origin 1'; + is line_intersection(-1, 1, 1, -1, -1, -1, 1, 1), [0, 0], 'origin 2'; + is line_intersection(2, 4, 8, 7, 3, 6, 7, 2), [4, 5], 'asymmetric test'; + } + + done_testing; + exit; +} -- cgit