Line circle intersection: Difference between revisions

m
→‎{{header|Perl}}: clean up punctuation
(Added Perl example)
m (→‎{{header|Perl}}: clean up punctuation)
Line 200:
my $b = 2 * ($f[0]*$d[0] + $f[1]*$d[1]);
my $c = sum(map { $_**2 } @f) - $radius**2;
my $D = $b**2 - (4*$a*$c);
 
return unless $D >= 0;
my ($t1, $t2) = ( (-$b - sqrt( $D)) / (2*$a), (-$b + sqrt( $D)) / (2*$a) );
return unless ( $t1 >= 0 and $t1 <= 1 ) or ( $t2 >= 0 and $t2 <= 1 );
 
my ($dx, $dy) = ($$P2[0] - $$P1[0], $$P2[1] - $$P1[1]);
Line 221:
);
 
sub rnd { map { (sprintf('%.2f', $_)) =~ s/\.00//r } @_ }
 
for my $d (@data) {
my @solution = find_intersection( @$d[0] , @$d[1] , @$d[2], @$d[3]);
say 'For input: ' . join( ', ', (map { '('. join(',', @$_) .')' } @$d[0,1,2]), @$d[3]);
say 'Solutions: ' . (@solution > 1 ? join ', ', map { '('. join(',', rnd( @$_)) .')' } @solution : "'None"');
say '';
}</lang>
2,392

edits