Mercurial > octave-antonio
comparison scripts/geometry/rectint.m @ 7551:5ed0cb9e9584
rectint.m: added
author | bill@denney.ws |
---|---|
date | Tue, 04 Mar 2008 15:07:44 -0500 |
parents | |
children | ea2344c4140b |
comparison
equal
deleted
inserted
replaced
7550:bffb1e2ab732 | 7551:5ed0cb9e9584 |
---|---|
1 ## Copyright (C) 2008 Bill Denney | |
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 3 of the License, or (at | |
8 ## your option) 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, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {@var{area} =} rectint (@var{a}, @var{b}) | |
21 ## | |
22 ## Compute the area of intersection of rectangles in @var{a} and | |
23 ## rectangles in @var{b}. Rectangles are defined as [x y width height] | |
24 ## where x and y are the minimum values of the two orthogonal | |
25 ## dimensions. | |
26 ## | |
27 ## If @var{a} or @var{b} are matrices, then the output, @var{area}, is a | |
28 ## matrix where the ith row corresponds to the ith row of a and the jth | |
29 ## column corresponds to the jth row of b. | |
30 ## | |
31 ## @seealso{polyarea} | |
32 ## @end deftypefn | |
33 | |
34 ## Author: Bill Denney <bill@denney.ws> | |
35 | |
36 function area = rectint (a, b) | |
37 | |
38 if (nargin != 2) | |
39 print_usage (); | |
40 elseif (ndims (a) != 2 || ndims (b) != 2) | |
41 error ("rectint: expecting arguments to be 2-d arrays"); | |
42 elseif (columns (a) != 4) | |
43 error ("rectint: a must have 4 columns"); | |
44 elseif (columns (b) != 4) | |
45 error ("rectint: b must have 4 columns"); | |
46 endif | |
47 | |
48 area = zeros (rows (a), rows (b)); | |
49 for i = 1:rows (area) | |
50 r1 = a(i,:); | |
51 for j = 1:columns (area) | |
52 r2 = b(j,:); | |
53 area(i,j) = (overlap (r1([1, 3]), r2([1, 3])) | |
54 * overlap (r1([2, 4]), r2([2, 4]))); | |
55 endfor | |
56 endfor | |
57 | |
58 endfunction | |
59 | |
60 function amt = overlap (r1, r2) | |
61 | |
62 ## Determine whether two ranges overlap. Ranges are given as [value | |
63 ## offset] | |
64 amt = 0; | |
65 | |
66 ## Make sure that the values are in order. | |
67 r1 = sort ([r1(1), r1(1)+r1(2)]); | |
68 r2 = sort ([r2(1), r2(1)+r2(2)]); | |
69 | |
70 ## Is the first point in range 1 in the middle of range 2? | |
71 p1 = sum (r1(1) <= r2) == 1; | |
72 ## is the second? | |
73 p2 = sum (r1(2) <= r2) == 1; | |
74 if (p1) | |
75 if (p2) | |
76 amt = r1(2) - r1(1); | |
77 else | |
78 amt = r2(2) - r1(1); | |
79 endif | |
80 elseif (sum (r1(2) < r2) == 1) | |
81 amt = r1(2) - r2(1); | |
82 endif | |
83 | |
84 endfunction | |
85 | |
86 ## Tests | |
87 %!assert(rectint([0 0 1 1], [1 1 2 2]), 0) | |
88 %!assert(rectint([0 0 1 1], [0.5 0.5 2 2]), 0.25) | |
89 %!assert(rectint([0 0 1 1;0.5 0.5 1 1], [1 1 2 2]), [0;0.25]) | |
90 %!assert(rectint([1 1 2 2], [0 0 1 1;0.5 0.5 1 1]), [0 0.25]) | |
91 |