Circles of given radius through two points: Difference between revisions

→‎{{header|11l}}: make Error a non-fatal exception
(Fixed error where addition was used instead of subtraction.)
(→‎{{header|11l}}: make Error a non-fatal exception)
(8 intermediate revisions by 7 users not shown)
Line 50:
.msg = msg
 
F circles_from_p1p2r(p1, p2, r) X(Error)
‘Following explanation at http://mathforum.org/library/drmath/view/53027.html’
I r == 0.0
Line 81:
V (c1, c2) = circles_from_p1p2r(p1, p2, r)
print(" #.\n #.\n".format(c1, c2))
X.catchhandle Error v
print(" ERROR: #.\n".format(v.msg))</syntaxhighlight>
 
Line 123:
ERROR: radius of zero
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
Line 330 ⟶ 331:
One circle : radius: 0.0000 @( 0.1234, 0.9876)
</pre>
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">getPoint: function [p]-> ~{(x: |p\0|, y: |p\1|)}
getCircle: function [c]-> ~{(x: |c\0|, y: |c\1|, r: |c\2|)}
 
circles: function [p1, p2, r][
if r = 0 -> return "radius of zero"
if p1 = p2 -> return "coincident points gives infinite number of circles"
 
[dx, dy]: @[p2\0 - p1\0, p2\1 - p1\1]
q: sqrt add dx*dx dy*dy
if q > 2*r -> return "separation of points > diameter"
 
p3: @[(p1\0 + p2\0)/ 2, (p1\1 + p2\1) / 2]
d: sqrt (r*r) - (q/2)*(q/2)
return @[
@[(p3\0 - d*dy/q), (p3\1 + d*dx/q), abs r],
@[(p3\0 + d*dy/q), (p3\1 - d*dx/q), abs r]
]
]
 
loop [
[[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]
] 'tr [
[p1, p2, r]: tr
print ["Through points:\n " getPoint p1 "\n " getPoint p2]
print ["and radius" (to :string r)++"," "you can construct the following circles:"]
if? string? cic: <= circles p1 p2 r -> print [" ERROR:" cic]
else [
[c1, c2]: cic
print [" " getCircle c1]
print [" " getCircle c2]
]
print ""
]</syntaxhighlight>
 
{{out}}
 
<pre>Through points:
(x: 0.1234, y: 0.9876)
(x: 0.8764999999999999, y: 0.2345)
and radius 2.0, you can construct the following circles:
(x: 1.863111801658189, y: 1.974211801658189, r: 2.0)
(x: -0.8632118016581896, y: -0.7521118016581892, r: 2.0)
 
Through points:
(x: 0.0, y: 2.0)
(x: 0.0, y: 0.0)
and radius 1.0, you can construct the following circles:
(x: 0.0, y: 1.0, r: 1.0)
(x: 0.0, y: 1.0, r: 1.0)
 
Through points:
(x: 0.1234, y: 0.9876)
(x: 0.1234, y: 0.9876)
and radius 2.0, you can construct the following circles:
ERROR: coincident points gives infinite number of circles
 
Through points:
(x: 0.1234, y: 0.9876)
(x: 0.8764999999999999, y: 0.2345)
and radius 0.5, you can construct the following circles:
ERROR: separation of points > diameter
 
Through points:
(x: 0.1234, y: 0.9876)
(x: 0.1234, y: 0.9876)
and radius 0.0, you can construct the following circles:
ERROR: radius of zero</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">CircleCenter(x1, y1, x2, y2, r){
Line 369 ⟶ 444:
0.1234 0.9876 0.1234 0.9876 0.0 > No circles can be drawn, points are identical
</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
Line 411 ⟶ 487:
0.1234 0.9876 0.1234 0.9876 0.00 radius of zero gives no circles
</pre>
=={{header|BASIC256BASIC}}==
==={{header|BASIC256}}===
{{trans|Liberty BASIC}}
<syntaxhighlight lang="basic256">
Line 462 ⟶ 539:
end
</syntaxhighlight>
 
=={{header|C}}==
<syntaxhighlight lang="c">#include<stdio.h>
Line 696 ⟶ 774:
{{out}}
<pre>
Two solutions: 1.9634486311 21.0745497421 and -0.963536863212 -0.852436752112
Only one solution: 0 1
Infinitely many circles can be drawn
Line 891 ⟶ 969:
readln;
end.</syntaxhighlight>
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight>
func$ fmt a b .
return "(" & a & " " & b & ")"
.
proc test m1x m1y m2x m2y r . .
print "Points: " & fmt m1x m1y & " " & fmt m2x m2y & " Radius: " & r
if r = 0
print "Radius of zero gives no circles"
print ""
return
.
x = (m2x - m1x) / 2
y = (m2y - m1y) / 2
bx = m1x + x
by = m1y + y
pb = sqrt (x * x + y * y)
if pb = 0
print "Coincident points give infinite circles"
print ""
return
.
if pb > r
print "Points are too far apart for the given radius"
print ""
return
.
cb = sqrt (r * r - pb * pb)
x1 = y * cb / pb
y1 = x * cb / pb
print "Circles: " & fmt (bx - x1) (by + y1) & " " & fmt (bx + x1) (by - y1)
print ""
.
test 0.1234 0.9876 0.8765 0.2345 2.0
test 0.0000 2.0000 0.0000 0.0000 1.0
test 0.1234 0.9876 0.1234 0.9876 2.0
test 0.1234 0.9876 0.8765 0.2345 0.5
test 0.1234 0.9876 0.1234 0.9876 0.0
</syntaxhighlight>
 
=={{header|Elixir}}==
{{trans|Ruby}}
Line 968 ⟶ 1,087:
{0.1234, 0.9876, 0.0}
</pre>
 
=={{header|ERRE}}==
<syntaxhighlight lang="text">
Line 2,898 ⟶ 3,018:
0.1234 0.9876 0.8765 0.2345 0.5 points are too far apart for the given radius
0.1234 0.9876 0.1234 0.9876 0.0 radius of zero gives no circles.</pre>
=={{header|OpenSCAD}}==
<syntaxhighlight lang="OpenSCAD">
// distance between two points
function distance(p1, p2) = sqrt((difference(p2.x, p1.x)) ^ 2 + (difference(p2.y, p1.y) ^ 2));
// difference between two values in any order
function difference(a, b) = let(x = a > b ? a - b : b - a) x;
 
// function to find the circles of given radius through two points
function circles_of_given_radius_through_two_points(p1, p2, radius) =
let(mid = (p1 + p2)/2, q = distance(p1, p2), x_dist = sqrt(radius ^ 2 - (q / 2) ^ 2) * (p1.y - p2.y) / q,
y_dist = sqrt(radius ^ 2 - (q / 2) ^ 2) * (p2.x - p1.x) / q)
// point 1 and point 2 must not be the same point
assert(p1 != p2)
// radius must be more than 0
assert(radius > 0)
// distance between points cannot be more than diameter
assert(q < radius * 2)
// return both qualifying centres
[mid + [ x_dist, y_dist ], mid - [ x_dist, y_dist ]];
 
// test module for circles_of_given_radius_through_two_points
module test_circles_of_given_radius_through_two_points()
{
tests = [
[ [ -10, -10, 0 ], [ 50, 0, 0 ], 100 ], [ [ 200, 0, 0 ], [ 220, -20, 0 ], 30 ],
[ [ 300, 100, 0 ], [ 350, 200, 0 ], 80 ]
];
 
for (t = tests)
{
let(start = t[0], end = t[1], radius = t[2])
{
// plot start and end dots - these should be at the intersections of the circles
color("green") translate(start) cylinder(h = 3, r = 4);
color("green") translate(end) cylinder(h = 3, r = 4);
 
// call function
centres = circles_of_given_radius_through_two_points(start, end, radius);
echo("centres", centres);
// plot results
color("yellow") translate(centres[0]) cylinder(h = 1, r = radius);
color("red") translate(centres[1]) cylinder(h = 2, r = radius);
};
};
// The following tests will stop all execution. To run them, uncomment one at a time
// should fail - same points
// echo(circles_of_given_radius_through_two_points([0,0],[0,0],1));
// should fail - points are more than diameter apart
// echo(circles_of_given_radius_through_two_points(p1 = [0,0], p2 = [0,101], radius = 50));
// should fail - radius must be greater than 0
// echo(circles_of_given_radius_through_two_points(p1= [1,1], p2 = [10,1], radius = 0));
}
 
test_circles_of_given_radius_through_two_points();
</syntaxhighlight>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">circ(a, b, r)={
Line 2,915 ⟶ 3,091:
%4 = [0.370374144 + 0.740625856*I, 0.629525856 + 0.481474144*I]
%5 = "impossible"</pre>
 
=={{header|Perl}}==
{{trans|Python}}
Line 4,541 ⟶ 4,718:
{{trans|Go}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Math
 
var Two = "Two circles."
Line 4,636 ⟶ 4,813:
Center: (0.1234, 0.9876)
</pre>
 
=={{header|XPL0}}==
An easy way to solve this:
1,481

edits