calcualte radius and origin of circle based on three points in matlab

To calculate the radius and origin of circle based on three points in MATLAB, we can use the following steps:

  1. Calculate the slopes and midpoints of the two chords formed by the three points
  2. Calculate the intersection point of the two perpendicular bisectors of the chords
  3. Calculate the distance between the intersection point and any of the three points to get the radius
  4. Calculate the coordinates of the origin by adding the x and y components of the intersection point to the negative of each other.

Here's the code to implement these steps:

main.m
% input three points
x = [x1 x2 x3]; % x coordinates
y = [y1 y2 y3]; % y coordinates

% calculate slopes and midpoints of chords
slope1 = (y(2) - y(1)) / (x(2) - x(1));
slope2 = (y(3) - y(2)) / (x(3) - x(2));
mid1 = [(x(1) + x(2)) / 2, (y(1) + y(2)) / 2];
mid2 = [(x(2) + x(3)) / 2, (y(2) + y(3)) / 2];

% calculate intersection point of perpendicular bisectors
slopePerp1 = -1 / slope1;
slopePerp2 = -1 / slope2;
bPerp1 = mid1(2) - slopePerp1 * mid1(1);
bPerp2 = mid2(2) - slopePerp2 * mid2(1);
xInt = (bPerp2 - bPerp1) / (slopePerp1 - slopePerp2);
yInt = slopePerp1 * xInt + bPerp1;

% calculate radius and origin
radius = sqrt((xInt - x(1))^2 + (yInt - y(1))^2);
origin = [xInt, yInt];
689 chars
22 lines

Note that this approach assumes that the given points indeed form a circle (i.e., they are not collinear and all lie on the same plane). Otherwise, the calculation may not produce meaningful results.

related categories

gistlibby LogSnag