Home > matGeom > geom2d > createCircle.m

createCircle

PURPOSE ^

CREATECIRCLE Create a circle from 2 or 3 points.

SYNOPSIS ^

function circle = createCircle(varargin)

DESCRIPTION ^

CREATECIRCLE Create a circle from 2 or 3 points.

   C = createCircle(P1, P2, P3);
   Creates the circle passing through the 3 given points. 
   C is a 1*3 array of the form: [XC YX R].

   C = createCircle(P1, P2);
   Creates the circle whith center P1 and passing throuh the point P2.

   Works also when input are point arrays the same size, in this case the
   result has as many lines as the point arrays.

   Example
   % Draw a circle passing through 3 points.
     p1 = [10 15];
     p2 = [15 20];
     p3 = [10 25];
     circle = createCircle(p1, p2, p3);
     figure; hold on; axis equal; axis([0 50 0 50]);
     drawPoint([p1 ; p2; p3]);
     drawCircle(circle);

   See also:
   circles2d, createDirectedCircle

   ---------
   author : David Legland
   INRA - TPV URPOI - BIA IMASTE
   created the 31/10/2003.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function circle = createCircle(varargin)
0002 %CREATECIRCLE Create a circle from 2 or 3 points.
0003 %
0004 %   C = createCircle(P1, P2, P3);
0005 %   Creates the circle passing through the 3 given points.
0006 %   C is a 1*3 array of the form: [XC YX R].
0007 %
0008 %   C = createCircle(P1, P2);
0009 %   Creates the circle whith center P1 and passing throuh the point P2.
0010 %
0011 %   Works also when input are point arrays the same size, in this case the
0012 %   result has as many lines as the point arrays.
0013 %
0014 %   Example
0015 %   % Draw a circle passing through 3 points.
0016 %     p1 = [10 15];
0017 %     p2 = [15 20];
0018 %     p3 = [10 25];
0019 %     circle = createCircle(p1, p2, p3);
0020 %     figure; hold on; axis equal; axis([0 50 0 50]);
0021 %     drawPoint([p1 ; p2; p3]);
0022 %     drawCircle(circle);
0023 %
0024 %   See also:
0025 %   circles2d, createDirectedCircle
0026 %
0027 %   ---------
0028 %   author : David Legland
0029 %   INRA - TPV URPOI - BIA IMASTE
0030 %   created the 31/10/2003.
0031 %
0032 
0033 
0034 if nargin == 2
0035     % inputs are the center and a point on the circle
0036     p1 = varargin{1};
0037     p2 = varargin{2};
0038     x0 = p1(:,1);
0039     y0 = p1(:,2);
0040     r = hypot((p2(:,1)-x0), (p2(:,2)-y0));
0041     
0042 elseif nargin == 3
0043     % inputs are three points on the circle
0044     p1 = varargin{1};
0045     p2 = varargin{2};
0046     p3 = varargin{3};
0047 
0048     % compute circle center
0049     line1 = medianLine(p1, p2);
0050     line2 = medianLine(p1, p3);
0051     point = intersectLines(line1, line2);
0052     x0 = point(:, 1); 
0053     y0 = point(:, 2);
0054     
0055     % circle radius
0056     r = hypot((p1(:,1)-x0), (p1(:,2)-y0));
0057 end
0058     
0059 % create array for returning result
0060 circle = [x0 y0 r];

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019