rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
circumcenter_triangle.m
1 function c = circumcenter_triangle(q, p1, p2)
2 %function c = circumcenter_triangle(q, p1, p2)
3 %
4 % function computing the circumcenter of the triangle q,p1,p2.
5 %
6 % If q,p1,p2 is a matrix with columnwise points, then a matrix of
7 % rownwise circumcenters is generated.
8 
9 % Bernard Haasdonk 10.5.2007
10 
11 if size(p1,1)== 2
12  p1 = p1';
13 end;
14 
15 if size(p2,1)== 2
16  p2 = p2';
17 end;
18 
19 if size(q,1)== 2
20  q = q';
21 end;
22 
23 if (size(p1,2)~=2) || (size(p2,2)~=2) || (size(q,2)~=2)
24  error('Only 2d points acceptable in circumcenter computation');
25 end;
26 
27 
28 % let n1 be the normal to line q-p1
29 % let n2 be the normal to line q-p2
30 % then obviously c = (q+p1)/2 + lambda1 n1 = (q+p2) + lambda2 n2
31 %
32 % let N = [n1 -n2]
33 %
34 % => lambda = [lambda1; lambda 2] = N^-1 (p2-p1)/2
35 % where N^-1 = 1/Det(N) * [-n2y n2x; -n1y n1x];
36 %
37 % so only one component lambda1 is required
38 
39 n1 = [- (p1(:,2)-q(:,2)) , p1(:,1)-q(:,1)];
40 n2 = [- (p2(:,2)-q(:,2)) , p2(:,1)-q(:,1)];
41 detN = -n1(:,1).* n2(:,2) + n1(:,2).*n2(:,1);
42 
43 lambda1 = 0.5 * detN.^(-1) .* (-n2(:,2).* (p2(:,1)-p1(:,1)) + ...
44  n2(:,1).* (p2(:,2)-p1(:,2)) );
45 
46 c = 0.5 * (p1+q);
47 c(:,1) = c(:,1) + lambda1.* n1(:,1);
48 c(:,2) = c(:,2) + lambda1.* n1(:,2);
49 
50 %| \docupdate