Circles of given radius through two points: Difference between revisions

Add Seed7 example
(Add Seed7 example)
Line 756:
You can construct the following circles with radius 0.0:
[#<struct Circle x=0.1234, y=0.9876, r=0.0>]
</pre>
 
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
 
const type: point is new struct
var float: x is 0.0;
var float: y is 0.0;
end struct;
 
const func point: point (in float: x, in float: y) is func
result
var point: aPoint is point.value;
begin
aPoint.x := x;
aPoint.y := y;
end func;
 
const func float: distance (in point: p1, in point: p2) is
return sqrt((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2);
 
const proc: findCircles (in point: p1, in point: p2, in float: radius) is func
local
var float: separation is 0.0;
var float: mirrorDistance is 0.0;
begin
separation := distance(p1, p2);
if separation = 0.0 then
if radius = 0.0 then
write("Radius of zero. No circles can be drawn through (");
else
write("Infinitely many circles can be drawn through (");
end if;
writeln(p1.x digits 4 <& ", " <& p1.y digits 4 <& ")");
elsif separation = 2.0 * radius then
writeln("Given points are opposite ends of a diameter of the circle with center (" <&
(p1.x + p2.x) / 2.0 digits 4 <& ", " <& (p1.y + p2.y) / 2.0 digits 4 <& ") and radius " <&
radius digits 4);
elsif separation > 2.0 * radius then
writeln("Given points are farther away from each other than a diameter of a circle with radius " <&
radius digits 4);
else
mirrorDistance := sqrt(radius ** 2 - (separation / 2.0) ** 2);
writeln("Two circles are possible.");
writeln("Circle C1 with center (" <&
(p1.x + p2.x) / 2.0 + mirrorDistance*(p1.y - p2.y) / separation digits 4 <& ", " <&
(p1.y + p2.y) / 2.0 + mirrorDistance*(p2.x - p1.x) / separation digits 4 <& "), radius " <&
radius digits 4);
writeln("Circle C2 with center (" <&
(p1.x + p2.x) / 2.0 - mirrorDistance*(p1.y - p2.y) / separation digits 4 <& ", " <&
(p1.y + p2.y) / 2.0 - mirrorDistance*(p2.x - p1.x) / separation digits 4 <& "), radius " <&
radius digits 4);
end if;
end func;
 
const proc: main is func
local
const array array float: cases is [] (
[] (0.1234, 0.9876, 0.8765, 0.2345, 2.0),
[] (0.0000, 2.0000, 0.0000, 0.0000, 1.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));
var integer: index is 0;
begin
for index range 1 to 5 do
writeln("Case " <& index <& ":");
findCircles(point(cases[index][1], cases[index][2]),
point(cases[index][3], cases[index][4]), cases[index][5]);
end for;
end func;</lang>
 
{{out}}
<pre>
Case 1:
Two circles are possible.
Circle C1 with center (1.8631, 1.9742), radius 2.0000
Circle C2 with center (-0.8632, -0.7521), radius 2.0000
Case 2:
Given points are opposite ends of a diameter of the circle with center (0.0000, 1.0000) and radius 1.0000
Case 3:
Infinitely many circles can be drawn through (0.1234, 0.9876)
Case 4:
Given points are farther away from each other than a diameter of a circle with radius 0.5000
Case 5:
Radius of zero. No circles can be drawn through (0.1234, 0.9876)
</pre>