5837
|
1 ## Copyright (C) 1999 David M. Doolin <doolin@ce.berkeley.edu> |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} polyarea (@var{x}, @var{y}) |
|
22 ## @deftypefnx {Function File} {} polyarea (@var{x}, @var{y}, @var{dim}) |
|
23 ## |
|
24 ## Determines area of a polygon by triangle method. The variables |
|
25 ## @var{x} and @var{y} define the vertex pairs, and must therefore have |
|
26 ## the same shape. Then might be either vectors or arrays. If they are |
|
27 ## arrays then the columns of @var{x} and @var{y} are treated seperately |
|
28 ## and an area returned for each. |
|
29 ## |
|
30 ## If the optional @var{dim} argument is given, then @code{polyarea} |
|
31 ## works along this dimension of the arrays @var{x} and @var{y}. |
|
32 ## |
|
33 ## @end deftypefn |
|
34 |
|
35 ## todo: Add moments for centroid, etc. |
|
36 ## |
|
37 ## bugs and limitations: |
|
38 ## Probably ought to be an optional check to make sure that |
|
39 ## traversing the vertices doesn't make any sides cross |
|
40 ## (Is simple closed curve the technical definition of this?). |
|
41 |
|
42 ## Author: David M. Doolin <doolin@ce.berkeley.edu> |
5838
|
43 ## Date: 1999-04-14 |
5837
|
44 ## Modified-by: |
|
45 ## 2000-01-15 Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
|
46 ## * use matlab compatible interface |
|
47 ## * return absolute value of area so traversal order doesn't matter |
|
48 ## 2005-10-13 Torsten Finke |
|
49 ## * optimization saving half the sums and multiplies |
|
50 |
|
51 function a = polyarea (x, y, dim) |
|
52 if (nargin != 2 && nargin != 3) |
|
53 print_usage (); |
5838
|
54 elseif (ndims (x) == ndims (y) && size (x) == size (y)) |
5837
|
55 if (nargin == 2) |
|
56 a = abs (sum (x .* (shift (y, -1) - shift (y, 1)))) / 2; |
|
57 else |
|
58 a = abs (sum (x .* (shift (y, -1, dim) - shift (y, 1, dim)), dim)) / 2; |
|
59 endif |
5838
|
60 else |
|
61 error ("polyarea: x and y must have the same shape"); |
5837
|
62 endif |
|
63 endfunction |
|
64 |
|
65 %!shared x, y |
|
66 %! x = [1;1;3;3;1]; |
|
67 %! y = [1;3;3;1;1]; |
|
68 %!assert (polyarea(x,y), 4, eps) |
|
69 %!assert (polyarea([x,x],[y,y]), [4,4], eps) |
|
70 %!assert (polyarea([x,x],[y,y],1), [4,4], eps) |
|
71 %!assert (polyarea([x,x]',[y,y]',2), [4;4], eps) |