diff options
| author | e78783 <fungcheokyin@gmail.com> | 2022-05-17 17:10:15 +0800 |
|---|---|---|
| committer | e78783 <fungcheokyin@gmail.com> | 2022-05-17 17:10:15 +0800 |
| commit | 8b554f562d36c27e8c3968cf181c889dc6a09a6b (patch) | |
| tree | 37156c13380e0cab77fdcaf001fe5cc515fa1cdf | |
| parent | 5780515988f680cc30048e8a27dc880569888631 (diff) | |
| download | perlweeklychallenge-club-8b554f562d36c27e8c3968cf181c889dc6a09a6b.tar.gz perlweeklychallenge-club-8b554f562d36c27e8c3968cf181c889dc6a09a6b.tar.bz2 perlweeklychallenge-club-8b554f562d36c27e8c3968cf181c889dc6a09a6b.zip | |
Week 165
| -rw-r--r-- | challenge-165/cheok-yin-fung/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-165/cheok-yin-fung/perl/ch-2.pl | 59 |
2 files changed, 101 insertions, 0 deletions
diff --git a/challenge-165/cheok-yin-fung/perl/ch-1.pl b/challenge-165/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..ec333aae85 --- /dev/null +++ b/challenge-165/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +# The Weekly Challenge 165 +# Task 1 SVG +# Usage: $ ch-1.pl < DATA > IMG_FILE_NAME.svg + +use v5.24.0; +use warnings; +use SVG; + +my $img = SVG->new; + +while (<STDIN>) { + chomp; + my @nums = split ",", $_; + if (scalar @nums == 2) { + new_point(@nums); + } + elsif (scalar @nums == 4) { + new_line(@nums); + } +} + + + +sub new_point { + my ($cx, $cy) = ($_[0], $_[1]); + $img->circle(cx => $cx, cy => $cy, r => 2, style => {fill => 'red'}); +} + + + +sub new_line { + my ($x1, $y1, $x2, $y2) = ($_[0], $_[1], $_[2], $_[3]); + $img->line( x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, + style => { + 'fill-opacity' => 0, + 'stroke' => 'green' + }); +} + + +print $img->xmlify(-namespace=>'svg'); diff --git a/challenge-165/cheok-yin-fung/perl/ch-2.pl b/challenge-165/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..4cc8f19c0a --- /dev/null +++ b/challenge-165/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl +# The Weekly Challenge 165 +# Task 2 Line of Best Fit +# Usage: $ ch-2.pl +use v5.24.0; +use warnings; +use List::Util qw/min max sum/; +use ARGV::OrDATA; + +my @points; + +while (<>) { + chomp; + my @new_points = /\S+/g; + @new_points = map { [split ",", $_]} @new_points; + push @points, @new_points; +} + +my $n = scalar @points; + +my $slope = + ( $n * sum(map {$_->[0]*$_->[1]} @points) + - sum(map{$_->[0]} @points) * sum(map{$_->[1]} @points) ) + / ( $n * sum(map{$_->[0]**2} @points) + - (sum( map { $_->[0]} @points ))**2) ; + + +my $intercept = + ( sum( map {$_->[1]} @points ) + - $slope * sum( map {$_->[0]} @points) ) + / $n ; + + +sub y_best_fit { + my $x = $_[0]; + return $slope*$x + $intercept; +} + +my $minx = min( map {$_->[0]} @points ); +my $maxx = max( map {$_->[0]} @points ); + +my $line_of_best_fit = + join ",", $minx,y_best_fit($minx),$maxx,y_best_fit($maxx); + +open FH, "> BEST_FIT_DATA" or die $!; +say FH $line_of_best_fit; +say FH $_->[0],",",$_->[1] for @points; + +system("perl ch-1.pl < BEST_FIT_DATA > new.svg"); # applicable on Linux + + + +__DATA__ +333,129 39,189 140,156 292,134 393,52 160,166 362,122 13,193 +341,104 320,113 109,177 203,152 343,100 225,110 23,186 282,102 +284,98 205,133 297,114 292,126 339,112 327,79 253,136 61,169 +128,176 346,72 316,103 124,162 65,181 159,137 212,116 337,86 +215,136 153,137 390,104 100,180 76,188 77,181 69,195 92,186 +275,96 250,147 34,174 213,134 186,129 189,154 361,82 363,89 |
