Smallest enclosing circle problem: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Typo.) |
m (→{{header|Phix}}: minor simplifications) |
||
Line 358: | Line 358: | ||
function circle_from2(point a, b) |
function circle_from2(point a, b) |
||
-- return the smallest circle that intersects 2 points: |
-- return the smallest circle that intersects 2 points: |
||
point |
point midpoint = sq_div(sq_add(a,b),2) |
||
atom |
atom halfdiameter = distance(a,b)/2 |
||
circle res = { |
circle res = { midpoint, halfdiameter } |
||
return res |
return res |
||
end function |
end function |
||
Line 366: | Line 366: | ||
function circle_from3(point a, b, c) |
function circle_from3(point a, b, c) |
||
-- return a unique circle that intersects three points |
-- return a unique circle that intersects three points |
||
point bma = sq_sub(b,a), |
|||
cma = sq_sub(c,a) |
|||
atom {{aX,aY},{bX,bY},{cX,cY}} = {a,bma,cma}, |
|||
B = sum(sq_power(bma,2)), |
|||
C = sum(sq_power(cma,2)), |
|||
D = (bX*cY - bY*cX)*2 |
|||
point centre = {(cY*B - bY*C)/D + aX, |
|||
(bX*C - cX*B)/D + aY } |
|||
atom radius = distance(centre,a) -- (=== b,c) |
|||
circle res = { centre, radius } |
|||
return res |
return res |
||
end function |
end function |
||
Line 396: | Line 399: | ||
procedure welzl(sequence p) |
procedure welzl(sequence p) |
||
circle c = welzlr(shuffle(p),{}) |
|||
string s = sprintf("centre %v radius %.14g",c) |
|||
printf(1,"Points %v ==> %s\n",{p,s}) |
|||
end procedure |
end procedure |
||
constant tests = {{ |
constant tests = {{{0, 0},{ 0, 1},{ 1,0}}, |
||
{ |
{{5,-2},{-3,-2},{-2,5},{1,6},{0,2}}} |
||
papply(tests,welzl)</lang> |
papply(tests,welzl)</lang> |
||
{{out}} |
{{out}} |