Jump to content

Circles of given radius through two points: Difference between revisions

m
→‎{{header|Perl}}: tidying, show program output
m (→‎{{header|Perl}}: tidying, show program output)
Line 2,232:
=={{header|Perl}}==
{{trans|Python}}
<lang perl>usesub featurecircles 'say';{
sub circles_from_p1p2r {
my ($x1, $y1, $x2, $y2, $r) = @_;
 
diereturn '"Radius is zero'" if $r == 0.0;
diereturn 'coincident"Coincident points gives infinite number of Circles'circles" if $x1 == $x2 &&and $y1 == $y2;
 
# delta x, delta y between points
my ($dx, $dy) = ($x2 - $x1, $y2 - $y1);
my $q = sqrt($dx**2 + $dy**2);
diereturn 'separation"Separation of points >greater than diameter'" if $q > 2.0*$r;
 
# halfway point
my ($x3, $y3) = (($x1 + $x2) / 2, ($y1 + $y2) / 2);
# distance along the mirror line
my $d = sqrt($r**2-($q/2)**2);
# One answer
my @c1 = ($x3 - $d*$dy/$q, $y3 + $d*$dx/$q, $r);
# The other answer
my @c2 = ($x3 + $d*$dy/$q, $y3 - $d*$dx/$q, $r);
return (@c1, @c2);
}
 
# pair of solutions
my @arr = ([0.1234, 0.9876, 0.8765, 0.2345, 2.0],
[0 sprintf '(%.00004f, 2%.0000,4f) 0.0000,and 0(%.00004f, 1%.0]4f)',
my @c1 = ( $x3 - $d*$dy/$q, $y3 + $d*$dx/$q, $r);,
# [0.1234, 0.9876, 0.1234, 0.9876, 2.0],
my @c2 = ( $x3 + $d*$dy/$q, $y3 - $d*$dx/$q, $r);
# [0.1234, 0.9876, 0.8765, 0.2345, 0.5],
}
# [0.1234, 0.9876, 0.1234, 0.9876, 0.0]
 
);
for(my @arr) {= (
my @arr = ( [0.1234, 0.9876, 0.8765, 0.2345, 2.0],
my @res = circles_from_p1p2r @{$_};
[0.0000, 2.0000, 0.0000, 0.0000, 1.0],
say "@res";
# [0.1234, 0.9876, 0.1234, 0.9876, 2.0],
}</lang>
# [0.1234, 0.9876, 0.8765, 0.2345, 0.5],
# [0.1234, 0.9876, 0.1234, 0.9876, 0.0]
);
 
printf "(%.4f, %.4f) and (%.4f, %.4f): %s\n", @$_[0..3], join " ", circles @$_ for @arr;</lang>
{{out}}
<pre>(0.1234, 0.9876) and (0.8765, 0.2345): (1.8631, 1.9742) and (2.0000, -0.8632)
(0.0000, 2.0000) and (0.0000, 0.0000): (0.0000, 1.0000) and (1.0000, 0.0000)
(0.1234, 0.9876) and (0.1234, 0.9876): Coincident points gives infinite number of circles
(0.1234, 0.9876) and (0.8765, 0.2345): Separation of points greater than diameter
(0.1234, 0.9876) and (0.1234, 0.9876): Radius is zero
</pre>
 
=={{header|Perl 6}}==
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.