Circles of given radius through two points: Difference between revisions
Circles of given radius through two points (view source)
Revision as of 19:26, 12 November 2018
, 5 years ago→{{header|Perl}}: tidying, show program output
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: tidying, show program output) |
|||
Line 2,232:
=={{header|Perl}}==
{{trans|Python}}
<lang perl>
my ($x1, $y1, $x2, $y2, $r) = @_;
# delta x, delta y between points
my ($dx, $dy) = ($x2 - $x1, $y2 - $y1);
my $q = sqrt($dx**2 + $dy**2);
# halfway point
my ($x3, $y3) = (($x1 + $x2) / 2, ($y1 + $y2) / 2);
# distance along the mirror line
my $d = sqrt($r**2-($q/2)**2);
my @c1 = ($x3 - $d*$dy/$q, $y3 + $d*$dx/$q, $r);▼
my @c2 = ($x3 + $d*$dy/$q, $y3 - $d*$dx/$q, $r);▼
# pair of solutions
my @arr = ([0.1234, 0.9876, 0.8765, 0.2345, 2.0],▼
# [0.1234, 0.9876, 0.1234, 0.9876, 2.0],▼
# [0.1234, 0.9876, 0.8765, 0.2345, 0.5],▼
}
# [0.1234, 0.9876, 0.1234, 0.9876, 0.0]▼
[0.0000, 2.0000, 0.0000, 0.0000, 1.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}}==
|