rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
area_triangle.m
1 function a = area_triangle(q, p1, p2)
2 %function a = area_triangle(q, p1, p2)
3 %
4 % function computing the signed (!) area of the triangle q,p1,p2.
5 % is positive, if q,p1,p2 is counterclockwise, is negative, if
6 % q,p1,p2 is clockwise ordered.
7 %
8 % If q,p1,p2 is a matrix with rowwise points, then a vector of
9 % areas is generated.
10 
11 % Bernard Haasdonk 10.5.2007
12 
13 if size(p1,1)== 2
14  p1 = p1';
15 end;
16 
17 if size(p2,1)== 2
18  p2 = p2';
19 end;
20 
21 if size(q,1)== 2
22  q = q';
23 end;
24 
25 if (size(p1,2)~=2) || (size(p2,2)~=2) || (size(q,2)~=2)
26  error('Only 2d points acceptable in area computation');
27 end;
28 
29 %a = zeros(size(p1,2),1);
30 
31 % a = 0.5 * | Det (p1-q , p2-q) |
32 a11 = p1(:,1)-q(:,1);
33 a21 = p1(:,2)-q(:,2);
34 
35 a12 = p2(:,1)-q(:,1);
36 a22 = p2(:,2)-q(:,2);
37 
38 a = 0.5 * (a11.*a22 - a21.*a12);
39 
40 
41 
42 %| \docupdate