Circles of given radius through two points: Difference between revisions

m (BASIC256 moved to the BASIC section.)
Line 2,975:
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">
// 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 (p1 - p2), X=0, Y=1, 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(){
radius = 100;
start = [1,1];
end = [100,100];
 
//plot start and end dots
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([0,0],[100,101],radius);
 
//plot both 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,992 ⟶ 3,038:
%4 = [0.370374144 + 0.740625856*I, 0.629525856 + 0.481474144*I]
%5 = "impossible"</pre>
 
=={{header|Perl}}==
{{trans|Python}}
2

edits