diff options
| -rw-r--r-- | challenge-165/wambash/raku/ch-1.raku | 75 | ||||
| -rw-r--r-- | challenge-165/wambash/raku/ch-2.raku | 31 |
2 files changed, 106 insertions, 0 deletions
diff --git a/challenge-165/wambash/raku/ch-1.raku b/challenge-165/wambash/raku/ch-1.raku new file mode 100644 index 0000000000..5d4409f1ce --- /dev/null +++ b/challenge-165/wambash/raku/ch-1.raku @@ -0,0 +1,75 @@ +#!/usr/bin/env raku + +role SVG { + method svg () { + '<' + ~ self.^name.gist.lc + ~ ' ' + ~ self.Capture.hash.sort.map( { .key ~ '=' ~ '"' ~ .value ~ '"' } ) + ~ ' ' + ~ '/>' + } +} + +class Circle does SVG { + has $.cx; + has $.cy; + has $.r = 2; + has $.fill = 'orange'; +} + +class Line does SVG { + has $.x1; + has $.y1; + has $.x2; + has $.y2; + has $.stroke= 'blue'; + has $.stroke-width = 1; +} + +sub svg ( +@svg, :$height = 400, :$width = 600 ) { + sprintf( q:to/END/ +<svg +version="1.1" +xmlns="http://www.w3.org/2000/svg" +xmlns:xlink="http://www.w3.org/1999/xlink" +height="%d" width="%d" +> +END +, $height, $width) + ~ @svg».svg.fmt( "\t%s", "\n" ) + ~ "\n" + ~ '</svg>' +} + +multi to-point-line (+@ ($cx, $cy) ) { + Circle.new: :$cx, :$cy +} + +multi to-point-line (+@ ($x1, $y1, $x2, $y2)) { + Line.new: :$x1, :$y1, :$x2, :$y2 +} + +multi MAIN (Int :$height = 400,Int :$width = 600 ) { + words() + andthen .map: *.split: ',' + andthen .map: &to-point-line + andthen svg $_, :$height, :$width + andthen .say +} + +multi MAIN (Bool :test($)!) { + use Test; + with to-point-line 23,10 { + isa-ok $_, Circle; + is (.cx,.cy),(23,10); + is .svg, '<circle cx="23" cy="10" fill="orange" r="2" />'; + } + with to-point-line 53,12, 23,10 { + isa-ok $_, Line; + is (.x1,.y1),(53,12); + is (.x2,.y2),(23,10); + is .svg, '<line stroke="blue" stroke-width="1" x1="53" x2="23" y1="12" y2="10" />'; + } + done-testing; +} diff --git a/challenge-165/wambash/raku/ch-2.raku b/challenge-165/wambash/raku/ch-2.raku new file mode 100644 index 0000000000..c27bb024ac --- /dev/null +++ b/challenge-165/wambash/raku/ch-2.raku @@ -0,0 +1,31 @@ +#!/usr/bin/env raku + + + + +sub line-of-best-fit (+@point) { + @point + andthen .map: { .[0]²,.[0], .[0],1, .[0] * .[1],.[1]}\ + andthen [Z,] $_ + andthen .map: *.sum + andthen (.[4]*.[3]-.[5]*.[2]),(.[0]*.[5]-.[1]*.[4]) X/ (.[0]*.[3]-.[1]*.[2]) +} + +multi MAIN (Bool :test($)!) { + use Test; + is line-of-best-fit((0,0),(1,1)),(1,0); + is line-of-best-fit((0,0),(1,2)),(2,0); + is line-of-best-fit((0,10),(1,20),(1,0)),(0,10); + done-testing; +} + +#| echo "333,129 39,189 140,156 292,134 393,52 160,166 362,122 363,89" | raku ch-2.raku -from=0 -to=400 | raku ch-1.raku -height=200 -width=400 > point-line.svg +multi MAIN (Numeric :$from=0,Numeric :$to=600) { + my $points= slurp(); + say $points; + $points.words() + andthen .map: *.split: ',' + andthen line-of-best-fit($_) + andthen $from,.[0]*$from+.[1], $to,.[0]*$to+.[1] + andthen .join(',').say +} |
