Circles of given radius through two points: Difference between revisions

→‎{{header|11l}}: make Error a non-fatal exception
(→‎{{header|11l}}: make Error a non-fatal exception)
(4 intermediate revisions by 4 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 968 ⟶ 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 1,045 ⟶ 1,087:
{0.1234, 0.9876, 0.0}
</pre>
 
=={{header|ERRE}}==
<syntaxhighlight lang="text">
Line 2,977 ⟶ 3,020:
=={{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 = norm distance(p1 -, p2), X=0, Y=1, x_dist = sqrt(radius ^ 2 - (q / 2) ^ 2) * (p1[Y].y - p2[Y].y) / q, y_dist = sqrt(radius^2-(q/2)^2)*(p2[X]-p1[X])/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(){
{
radius = 100;
starttests = [1,1];
[ [ -10, -10, 0 ], [ 50, 0, 0 ], 100 ], [ [ 200, 0, 0 ], [ 220, -20, 0 ], 30 ],
end = [100,100];
[ [ 300, 100, 0 ], [ 350, 200, 0 ], 80 ]
];
 
//plotfor start(t and= end dotstests)
{
color("green") translate(start) cylinder(h=3, r=4);
let(start = t[0], end = t[1], radius = t[2])
color("green") translate(end) cylinder(h=3, r=4);
{
// 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([0start,0],[100,101] end, radius);
echo("centres", centres);
 
//plot bothplot 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}}==
Line 4,665 ⟶ 4,718:
{{trans|Go}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Math
 
var Two = "Two circles."
Line 4,760 ⟶ 4,813:
Center: (0.1234, 0.9876)
</pre>
 
=={{header|XPL0}}==
An easy way to solve this:
1,481

edits