# Re: Fit circle to 3 points

"Peter Bone" <peterbone@xxxxxxxxxxx> wrote in message
<fqoipl\$blh\$1@xxxxxxxxxxxxxxxxxx>...
I've written a function to calculate the circle that
passes ...

Another solution which I found in my own archive:

function [xy,r] = points2circle(A,B,C)
% POINTS2CIRCLE - find circle given three points
%
% [XY,R] = POINTS2CIRCLE(A,B,C) finds the center point XY
% of the circle passing through the three points A, B, and C
%
% POINTS2CIRCLE(P) in which P = [A ; B ; C] can also be
used. P is a 3-by-2
% matrix.
%
% Example:
% POINTS2CIRCLE([1 0],[-4 3],[3,2])

if nargin==1,
P = A ;
if size(P,1) ~=3 | size(P,2) ~=2,
error ('A single input should be a 3-by-2 matrix') ;
end
elseif ~isequal(numel(A),numel(B),numel(C),2),
error('The three points should all have 2 elements.')
else
P = [A(:) B(:) C(:)] .' ;
end

% matrix
M = [...
1 1 1 1 ; ...
(P(1,1).^2 + P(1,2).^2) P(1,1) P(1,2) 1 ; ...
(P(2,1).^2 + P(2,2).^2) P(2,1) P(2,2) 1 ; ...
(P(3,1).^2 + P(3,2).^2) P(3,1) P(3,2) 1 ...
] ;

M11 = local_minordet(M,1,1) ;
if M11==0,
xy = [] ;
r = [] ;
warning('No solution! Points may be on a straight line.') ;
else
xy(1) = 0.5 * (local_minordet(M,1,2) ./ M11) ;
xy(2) = -0.5 * (local_minordet(M,1,3) ./ M11) ;
r = sqrt(xy(1).^2 + xy(2).^2 + (local_minordet(M,1,4) ./
M11)) ;
end

function md = local_minordet(M,i,j) ;
% minor determinant
M(i,:)=[] ;
M(:,j)=[] ;
md = det(M) ;

% END OF FUNCTION

beware of line wraps ...

hth
Jos

.

## Relevant Pages

• [SUMMARY] The Smallest Circle (#157)
... about the smallest enclosing circle problem... ... def initialize(*coords) ... y_big is the minimum radius of a circle ... always been the square of the radius, we finally take its square root ...
(comp.lang.ruby)
• Re: need help with homework
... So if you solve for circumference you get pi x diameter, ... Now if you know relation between diameter and radius, ... Center of circle splits diameter into two radii. ... circular disk, i.e. a disk whose perimiter is a circle. ...
(sci.math)