Centre and radius of a circle passing through 3 points in a plane: Difference between revisions

From Rosetta Code
Content added Content deleted
(Create task and realize in F#)
 
(Added Phix)
Line 16: Line 16:
<pre>
<pre>
Centre = (18.97851566, 16.2654108), radius = 7.354312
Centre = (18.97851566, 16.2654108), radius = 7.354312
</pre>

=={{header|Phix}}==
{{trans|F#}}
<!--(phixonline)-->
<syntaxhighlight lang="phix">
with javascript_semantics
function circle(sequence p1, p2, p3)
atom {a,b} = p1, {c,d} = p2, {e,f} = p3,
cx = 0.5*((a*a+b*b)*(f-d)+(c*c+d*d)*(b-f)+(e*e+f*f)*(d-b))/(a*(f-d)+c*(b-f)+e*(d-b)),
cy = 0.5*((a*a+b*b)*(e-c)+(c*c+d*d)*(a-e)+(e*e+f*f)*(c-a))/(b*(e-c)+d*(a-e)+f*(c-a)),
r = sqrt(power(cx-e,2)+power(cy-f,2))/2
return {{cx,cy},r}
end function

atom {{cx,cy},r} = circle({22.83,2.07},{14.39,30.24},{33.65,17.31})
printf(1,"Centre = %.8f, %.7f, radius = %.6f\n",{cx,cy,r})
</syntaxhighlight>
{{out}}
<pre>
Centre = 18.97851566, 16.2654108, radius = 7.354312
</pre>
</pre>

Revision as of 20:49, 20 February 2024

Centre and radius of a circle passing through 3 points in a plane is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Write a function which returns the centre and radius of a circle passing through three point in a plane. Demonstrate the function using the points (22.82,2.07) (14.39,30.24) and (33.65,17.31)

F#

// Centre and radius of a circle passing through 3 points in a plane. Nigel Galloway: February 20th., 2024
let c (a,b) (c,d) (e,f)=(0.5*((a*a+b*b)*(f-d)+(c*c+d*d)*(b-f)+(e*e+f*f)*(d-b))/(a*(f-d)+c*(b-f)+e*(d-b)),
                         0.5*((a*a+b*b)*(e-c)+(c*c+d*d)*(a-e)+(e*e+f*f)*(c-a))/(b*(e-c)+d*(a-e)+f*(c-a)))
let d n g = let n,g=fst n-fst g,snd n-snd g in sqrt(n*n+g*g)/2.0
let circ P1 P2 P3 = let c=c P1 P2 P3 in (c,d c P1)

let centre,radius=circ (22.83, 2.07) (14.39, 30.24) (33.65, 17.31)
printfn $"Centre = %A{centre}, radius = %f{radius}"
Output:
Centre = (18.97851566, 16.2654108), radius = 7.354312

Phix

Translation of: F#
with javascript_semantics
function circle(sequence p1, p2, p3)
    atom {a,b} = p1, {c,d} = p2, {e,f} = p3,
         cx = 0.5*((a*a+b*b)*(f-d)+(c*c+d*d)*(b-f)+(e*e+f*f)*(d-b))/(a*(f-d)+c*(b-f)+e*(d-b)),
         cy = 0.5*((a*a+b*b)*(e-c)+(c*c+d*d)*(a-e)+(e*e+f*f)*(c-a))/(b*(e-c)+d*(a-e)+f*(c-a)),
         r = sqrt(power(cx-e,2)+power(cy-f,2))/2
    return {{cx,cy},r}
end function

atom {{cx,cy},r} = circle({22.83,2.07},{14.39,30.24},{33.65,17.31})
printf(1,"Centre = %.8f, %.7f, radius = %.6f\n",{cx,cy,r})
Output:
Centre = 18.97851566, 16.2654108, radius = 7.354312