Mercurial > hg > octave-lyh
annotate scripts/geometry/inpolygon.m @ 10412:d2ac9433cd09
append AM_CPPFLAGS to libcruft_la_CPPFLAGS
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 15 Mar 2010 16:56:14 -0400 |
parents | 1bf0ce0930be |
children | 95c3e38098bf |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2006, 2007, 2008, 2009 Frederick (Rick) A Niles |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
2 ## and S�ren Hauberg |
6847 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
6847 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6847 | 19 |
20 ## -*- texinfo -*- | |
7650 | 21 ## @deftypefn {Function File} {[@var{in}, @var{on}] =} inpolygon (@var{x}, @var{y}, @var{xv}, @var{xy}) |
6847 | 22 ## |
23 ## For a polygon defined by @code{(@var{xv}, @var{yv})} points, determine | |
24 ## if the points @code{(@var{x}, @var{y})} are inside or outside the polygon. | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## The variables @var{x}, @var{y}, must have the same dimension. The optional |
6847 | 26 ## output @var{on} gives the points that are on the polygon. |
27 ## | |
28 ## @end deftypefn | |
29 | |
30 ## Author: Frederick (Rick) A Niles <niles@rickniles.com> | |
31 ## Created: 14 November 2006 | |
32 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
33 ## Vectorized by S�ren Hauberg <soren@hauberg.org> |
6847 | 34 |
35 ## The method for determining if a point is in in a polygon is based on | |
36 ## the algorithm shown on | |
37 ## http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/ and is | |
38 ## credited to Randolph Franklin. | |
39 | |
40 function [IN, ON] = inpolygon (X, Y, xv, yv) | |
41 | |
7125 | 42 if (nargin != 4) |
43 print_usage (); | |
44 endif | |
45 | |
46 if (! (isreal (X) && isreal (Y) && ismatrix (Y) && ismatrix (Y) | |
47 && size_equal (X, Y))) | |
6847 | 48 error ("inpolygon: first two arguments must be real matrices of same size"); |
7125 | 49 elseif (! (isreal (xv) && isreal (yv) && isvector (xv) && isvector (yv) |
50 && size_equal (xv, yv))) | |
6847 | 51 error ("inpolygon: last two arguments must be real vectors of same size"); |
52 endif | |
53 | |
7125 | 54 npol = length (xv); |
6847 | 55 do_boundary = (nargout >= 2); |
56 | |
57 IN = zeros (size(X), "logical"); | |
58 if (do_boundary) | |
59 ON = zeros (size(X), "logical"); | |
60 endif | |
61 | |
62 j = npol; | |
63 for i = 1 : npol | |
64 delta_xv = xv(j) - xv(i); | |
65 delta_yv = yv(j) - yv(i); | |
66 ## distance = [distance from (X,Y) to edge] * length(edge) | |
67 distance = delta_xv .* (Y - yv(i)) - (X - xv(i)) .* delta_yv; | |
68 ## | |
69 ## is Y between the y-values of edge i,j | |
70 ## AND (X,Y) on the left of the edge ? | |
8507 | 71 idx1 = (((yv(i) <= Y & Y < yv(j)) | (yv(j) <= Y & Y < yv(i))) |
72 & 0 < distance.*delta_yv); | |
6847 | 73 IN (idx1) = !IN (idx1); |
74 | |
75 ## Check if (X,Y) are actually ON the boundary of the polygon. | |
76 if (do_boundary) | |
8507 | 77 idx2 = (((yv(i) <= Y & Y <= yv(j)) | (yv(j) <= Y & Y <= yv(i))) |
78 & ((xv(i) <= X & X <= xv(j)) | (xv(j) <= X & X <= xv(i))) | |
79 & (0 == distance | !delta_xv)); | |
6847 | 80 ON (idx2) = true; |
81 endif | |
82 j = i; | |
83 endfor | |
7125 | 84 |
6847 | 85 endfunction |
86 | |
87 %!demo | |
88 %! xv=[ 0.05840, 0.48375, 0.69356, 1.47478, 1.32158, \ | |
89 %! 1.94545, 2.16477, 1.87639, 1.18218, 0.27615, \ | |
90 %! 0.05840 ]; | |
91 %! yv=[ 0.60628, 0.04728, 0.50000, 0.50000, 0.02015, \ | |
92 %! 0.18161, 0.78850, 1.13589, 1.33781, 1.04650, \ | |
93 %! 0.60628 ]; | |
94 %! xa=[0:0.1:2.3]; | |
95 %! ya=[0:0.1:1.4]; | |
96 %! [x,y]=meshgrid(xa,ya); | |
97 %! [IN,ON]=inpolygon(x,y,xv,yv); | |
98 %! | |
99 %! inside=IN & !ON; | |
100 %! plot(xv,yv) | |
101 %! hold on | |
102 %! plot(x(inside),y(inside),"@g") | |
103 %! plot(x(~IN),y(~IN),"@m") | |
104 %! plot(x(ON),y(ON),"@b") | |
105 %! hold off | |
106 %! disp("Green points are inside polygon, magenta are outside,"); | |
107 %! disp("and blue are on boundary."); | |
108 | |
109 %!demo | |
110 %! xv=[ 0.05840, 0.48375, 0.69356, 1.47478, 1.32158, \ | |
111 %! 1.94545, 2.16477, 1.87639, 1.18218, 0.27615, \ | |
112 %! 0.05840, 0.73295, 1.28913, 1.74221, 1.16023, \ | |
113 %! 0.73295, 0.05840 ]; | |
114 %! yv=[ 0.60628, 0.04728, 0.50000, 0.50000, 0.02015, \ | |
115 %! 0.18161, 0.78850, 1.13589, 1.33781, 1.04650, \ | |
116 %! 0.60628, 0.82096, 0.67155, 0.96114, 1.14833, \ | |
117 %! 0.82096, 0.60628]; | |
118 %! xa=[0:0.1:2.3]; | |
119 %! ya=[0:0.1:1.4]; | |
120 %! [x,y]=meshgrid(xa,ya); | |
121 %! [IN,ON]=inpolygon(x,y,xv,yv); | |
122 %! | |
123 %! inside=IN & ~ ON; | |
124 %! plot(xv,yv) | |
125 %! hold on | |
126 %! plot(x(inside),y(inside),"@g") | |
127 %! plot(x(~IN),y(~IN),"@m") | |
128 %! plot(x(ON),y(ON),"@b") | |
129 %! hold off | |
130 %! disp("Green points are inside polygon, magenta are outside,"); | |
131 %! disp("and blue are on boundary."); | |
132 |