function [R,C]=circle3pt(P1,P2,P3)
%Circle through 3 given points
%  [R,C]=circle3pt(P1,P2,P3) finds the center C and radius R of the circle
%  passing through the 3 points P1,P2,P3. P1,P2,P3 must be 3 component vectors.
%  
% examples:
%  >> [R,C]=circle3pt([2,0,0],[1,1,0],[0,0,0])
%  >> [R,C]=circle3pt([3,2,1],[2,1,3],[1,3,2])
%
% WARNING: DEMO ONLY, NOT FOOL-PROOF!
%
% Fabian Waleffe for Math 321, UW Feb. 2009

s1=P2-P1;                        % secant vector P1P2
s2=P3-P2;                        % secant vector P2P3
s3=P3-P1;                        % secant vector P1P3
n1=s2-dot(s1,s2)*s1/dot(s1,s1);  % normal to P1P2 vector
n1=n1/norm(n1);

t=0.5*dot(s2,s3)/dot(n1,s2);     % C comp in n1 direction

C=0.5*[P1+P2]+t*n1;               
R=norm(P1-C);
