TRIANGLEAREA Signed area of a triangle. AREA = triangleArea(P1, P2, P3) Computes area of the triangle whose vertices are given by P1, P2 and P3. Each vertex is a 1-by-2 row vector. AREA = triangleArea(PTS) Concatenates vertex coordinates in a 3-by-2 array. Each row of the array contains coordinates of one vertex. Example % Compute area of a Counter-Clockwise (CCW) oriented triangle triangleArea([10 10], [30 10], [10 40]) ans = 300 % Compute area of a Clockwise (CW) oriented triangle triangleArea([10 40], [30 10], [10 10]) ans = -300 See also polygonArea, triangleArea3d
0001 function area = triangleArea(pt1, pt2, pt3) 0002 %TRIANGLEAREA Signed area of a triangle. 0003 % 0004 % AREA = triangleArea(P1, P2, P3) 0005 % Computes area of the triangle whose vertices are given by P1, P2 and 0006 % P3. Each vertex is a 1-by-2 row vector. 0007 % 0008 % AREA = triangleArea(PTS) 0009 % Concatenates vertex coordinates in a 3-by-2 array. Each row of the 0010 % array contains coordinates of one vertex. 0011 % 0012 % 0013 % Example 0014 % % Compute area of a Counter-Clockwise (CCW) oriented triangle 0015 % triangleArea([10 10], [30 10], [10 40]) 0016 % ans = 0017 % 300 0018 % 0019 % % Compute area of a Clockwise (CW) oriented triangle 0020 % triangleArea([10 40], [30 10], [10 10]) 0021 % ans = 0022 % -300 0023 % 0024 % See also 0025 % polygonArea, triangleArea3d 0026 0027 % ------ 0028 % Author: David Legland 0029 % E-mail: david.legland@inrae.fr 0030 % Created: 2011-08-23, using Matlab 7.9.0.529 (R2009b) 0031 % Copyright 2011-2024 INRA - Cepia Software Platform 0032 0033 % if data is given as one array, split vertices 0034 if nargin == 1 0035 pt2 = pt1(2,:); 0036 pt3 = pt1(3,:); 0037 pt1 = pt1(1,:); 0038 end 0039 0040 % compute individual vectors 0041 v12 = bsxfun(@minus, pt2, pt1); 0042 v13 = bsxfun(@minus, pt3, pt1); 0043 0044 % compute area from cross product 0045 area = (v13(:,2) .* v12(:,1) - v13(:,1) .* v12(:,2)) / 2;