-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitCir3P.m
44 lines (43 loc) · 1.37 KB
/
fitCir3P.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function [r, c] = fitCir3P(p1, p2, p3)
% Get circle passing through 3 given points
% Author: Milos Petrasinovic <[email protected]>
% PR-DC, Republic of Serbia
% ----- INPUTS -----
% p1 - point 1
% p2 - point 2
% p3 - point 3
% ----- OUTPUTS -----
% r - circle radius
% c - circle center coordinates
% --------------------
%
% Copyright (C) 2021 PR-DC <[email protected]>
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as
% published by the Free Software Foundation, either version 3 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public License
% along with this program. If not, see <https://www.gnu.org/licenses/>.
%
% --------------------
a = (p1+p2)/2;
b = (p2+p3)/2;
u = p1-p2;
v = p2-p3;
d = a-b;
vu = v(:, 1).*u(:, 2)-v(:, 2).*u(:, 1);
if(any(vu == 0)) % Points are collinear, so no unique solution
error('Points are collinear!');
end
g = -(d(:, 1).*u(:, 1)+d(:, 2).*u(:, 2))./vu;
c = [b(:, 1)+g.*v(:, 2), b(:, 2)-g.*v(:, 1)];
r = norm(c-p1, 'rows');
end