octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

New Function rectint.m


From: Bill Denney
Subject: New Function rectint.m
Date: Sun, 02 Mar 2008 23:47:03 -0500
User-agent: Thunderbird 2.0.0.12 (Windows/20080213)

Hi,

I was feeling like doing something so I wrote the rectint.m function. Changelog is in the changeset.

Have a good day,

Bill
# HG changeset patch
# User address@hidden
# Date 1204519435 18000
# Node ID 8625e205e8d5672ad875ccbc6ef9f210ad65a107
# Parent  b84c5cbc081243fabec4026636f0c947c587c372
rectint.m: added

diff -r b84c5cbc0812 -r 8625e205e8d5 scripts/ChangeLog
--- a/scripts/ChangeLog Fri Feb 29 04:09:03 2008 -0500
+++ b/scripts/ChangeLog Sun Mar 02 23:43:55 2008 -0500
@@ -1,3 +1,7 @@ 2008-02-29  John W. Eaton  <address@hidden
+2008-03-02  Bill Denney  <address@hidden>
+
+       * geometry/rectint.m: New function.
+
 2008-02-29  John W. Eaton  <address@hidden>
 
        * plot/print.m: Handle gif and jpg devices.
diff -r b84c5cbc0812 -r 8625e205e8d5 scripts/geometry/rectint.m
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/geometry/rectint.m        Sun Mar 02 23:43:55 2008 -0500
@@ -0,0 +1,86 @@
+## Copyright (C) 2008 Bill Denney
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} address@hidden =} rectint (@var{a}, @var{b})
+##
+## Compute the area of intersection of rectangles in @var{a} and
+## rectangles in @var{b}.  Rectangles are defined as [x y width height]
+## where x and y are the minimum values of the two orthogonal
+## dimensions.
+##
+## If @var{a} or @var{b} are matrices, then the output, @var{area}, is a
+## matrix where the ith row corresponds to the ith row of a and the jth
+## column corresponds to the jth row of b.
+##
+## @seealso{polyarea}
+## @end deftypefn
+
+## Author: Bill Denney <address@hidden>
+
+function [area] = rectint (a, b)
+       
+  if (nargin ~= 2)
+    print_usage ();
+  elseif size (a, 2) ~= 4
+    error ("a must have 4 columns")
+  elseif size (b, 2) ~= 4
+    error ("b must have 4 columns")
+  endif
+
+  area = zeros (size (a, 1), size (b, 1));
+  for i = 1:size (area, 1)
+    r1 = a(i,:);
+    for j = 1:size (area, 2)
+      r2 = b(j,:);
+      area(i,j) = overlap(r1([1 3]), r2([1 3])) * overlap(r1([2 4]), r2([2 
4]));
+    endfor
+  endfor
+
+endfunction
+
+function [amt] = overlap(r1, r2)
+  ## Determine if two ranges overlap.  Ranges are given as [value
+  ## offset]
+  amt = 0;
+
+  ## make sure that the values are in order
+  r1 = sort([r1(1) r1(1)+r1(2)]);
+  r2 = sort([r2(1) r2(1)+r2(2)]);
+
+  ## is the first point in range 1 in the middle of range 2?
+  p1 = sum(r1(1) <= r2) == 1;
+  ## is the second?
+  p2 = sum(r1(2) <= r2) == 1;
+  if p1
+    if p2
+      amt = r1(2) - r1(1);
+    else
+      amt = r2(2) - r1(1);
+    endif
+  elseif sum(r1(2) < r2) == 1
+    amt = r1(2) - r2(1);
+  endif
+endfunction
+
+## Tests
+%!assert(rectint([0 0 1 1], [1 1 2 2]), 0)
+%!assert(rectint([0 0 1 1], [0.5 0.5 2 2]), 0.25)
+%!assert(rectint([0 0 1 1;0.5 0.5 1 1], [1 1 2 2]), [0;0.25])
+%!assert(rectint([1 1 2 2], [0 0 1 1;0.5 0.5 1 1]), [0 0.25])
+

reply via email to

[Prev in Thread] Current Thread [Next in Thread]