Code covered by the BSD License  

Highlights from
Triangle Area and Angles v1.3

4.66667

4.7 | 4 ratings Rate this file 25 Downloads (last 30 days) File Size: 2.4 KB File ID: #16448

Triangle Area and Angles v1.3

by Dirk-Jan Kroon

 

19 Sep 2007 (Updated 08 Nov 2007)

Calculates the area and angles of any triangle described by 2D,3D..nD points

| Watch this File

File Information
Description

This simple function use three points as input which can be 2D,3D...nD. And will calculate the area and angles of this triangle / polygon.

The last version also support the precise 'Orthogonal-triangular decomposition' area calculation method.

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
19 Sep 2007 Scott Miller

Angles will be returned in degrees.

Scott

19 Sep 2007 John D'Errico

A couple of things to note here. First is as Scott noted, that triangle_angles has non-standard return for Matlab. It returns degrees, not radians. This is undocumented in the help, and should be repaired.

Triangle_area uses Heron's formula for the area of a triangle. Is this a good choice? The numerical analyst in me says to beware. Anytime that you square and square root some numbers, expect trouble. The problems will be seen when two of the triangle vertices are very near to each other. Try out this example:

format long g
pts = [0 0;0 1;1.e-9 0];

I've chosen this to be a right triangle, so the area is easy to compute. It will be exactly 5e-10.

triangle_area(pts)
ans =
      5.00000041370185e-10

As expected, we see a problem. Could this have been done more accurately? Its easy to do it for the 2-d case, but when you move to more dimensions, how might we solve the problem? This little code fragment will do it for you, in ANY number of dimensions, 2 or more. And it will have a high accuracy.

[q,r,e] = qr((pts(2:3,:) - repmat(pts(1,:),2,1))');
abs(prod(diag(r)))/2
ans =
                    5e-10

I used a pivoted QR here to ensure success even on nasty problems. Yes, its true that for most problems, there will be little difference. Its only on the nasty ones that you might worry.

pts = rand(3,10);
[q,r,e] = qr((pts(2:3,:) - repmat(pts(1,:),2,1))');
abs(prod(diag(r)))/2
ans =
         0.499463557359012

triangle_area(pts)
ans =
         0.499463557359011

But this is what numerical analysis does for you. It helps you to deal with those rare but nasty problems. Yes, triangle area was correct on the 10-d problem. How about this one, where the triangle is extremely flat, yet no two points are close to each other?

pts = [0 0;eps 1; 0 2];
triangle_area(pts)
ans =
     0

[q,r,e] = qr((pts(2:3,:) - repmat(pts(1,:),2,1))');
abs(prod(diag(r)))/2
ans =
      2.22044604925031e-16

In my choice of a rating for this submission, I considered these two flaws in the codes, as detailed above. Then I looked at the help. Its reasonable, except for the degrees versus radians problem. What the codes lack is error checks. Yes, there is only one input argument. It should be a numeric array, of size 3xn. Test for those properties. If the user has made a mistake in their use of the code, be nice and tell them that, rather than let them get possibly random results, or a confusing error message. I'd also recommend that the author explain what methods they have used. Yes, I recognized Heron's formula. But some other users might not do so.

The sum of these problems convinces me to rate it a 3. However when repairs are made, I'm always happy to change a rating.

22 Sep 2007 John D'Errico

Now repaired, you can choose between methods, with the QR as the default. The only downside to the QR is a slight increase in time, by my check, .0004 seconds. So if you want to sacrifice some potential accuracy for a wee bit of time, you now have the option. The angles tool now also allows you to specify radians or degrees.

Its got error checks on the arguments, this submission looks ok to me now.

16 Jan 2008 Andres Toennesmann

Thank you! Just as a suggestion:
You may add a new method that is a stable alternative to Heron's formula (see Wikipedia...) and that still works at about double speed compared to the orthogonal-triangular decomposition:

...
elseif strcmpi(method,'hs') % http://en.wikipedia.org/wiki/Heron%27s_formula, http://http.cs.berkeley.edu/~wkahan/Triangle.pdf
    L=[sqrt(sum((P(1,:)-P(2,:)).^2)) sqrt(sum((P(2,:)-P(3,:)).^2)) sqrt(sum((P(3,:)-P(1,:)).^2))];
    L=sort(L);
    % Area calculation with stabilized Heron's formula
    area = sqrt( (L(3)+(L(2)+L(1)))*(L(1)-(L(3)-L(2)))*(L(1)+(L(3)-L(2)))*(L(3)+(L(2)-L(1))) )/4;
...

Cheers
Andres

18 Jul 2008 Jane Guo  
Please login to add a comment or rating.
Updates
20 Sep 2007

insert help: degrees

21 Sep 2007

Updated with input values check, and 'Orthogonal-triangular decomposition' method, after usefull comment of John D'Errico

08 Nov 2007

Solved bug in angle calculation

08 Nov 2007

Angle bug fix for stretched triangles ...

Tag Activity for this File
Tag Applied By Date/Time
triangle Dirk-Jan Kroon 22 Oct 2008 09:27:35
polygon Dirk-Jan Kroon 22 Oct 2008 09:27:35
area Dirk-Jan Kroon 22 Oct 2008 09:27:35
angles Dirk-Jan Kroon 22 Oct 2008 09:27:35
points Dirk-Jan Kroon 22 Oct 2008 09:27:35
angle Dirk-Jan Kroon 22 Oct 2008 09:27:35
orthogonal Dirk-Jan Kroon 22 Oct 2008 09:27:35
3d Dirk-Jan Kroon 22 Oct 2008 09:27:35
2d Dirk-Jan Kroon 22 Oct 2008 09:27:35
angles Communcators 2007 28 Feb 2009 08:40:21
3d Phillip 27 Mar 2009 12:07:20
2d Laurent 16 Aug 2011 10:35:41

Contact us at files@mathworks.com