Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please, do some research, the amount of useful hits on google is literally overwhelming: </p> <ol> <li><a href="http://www.softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm" rel="nofollow noreferrer">http://www.softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm</a></li> <li><a href="http://www.mathworks.com/matlabcentral/fileexchange/10391-fast-points-in-polygon-test" rel="nofollow noreferrer">http://www.mathworks.com/matlabcentral/fileexchange/10391-fast-points-in-polygon-test</a></li> <li><a href="https://stackoverflow.com/questions/924171/geo-fencing-point-inside-outside-polygon">Geo Fencing - point inside/outside polygon</a></li> <li><a href="http://www.mathworks.com/matlabcentral/fileexchange/12744-distance-from-a-point-to-polygon" rel="nofollow noreferrer">http://www.mathworks.com/matlabcentral/fileexchange/12744-distance-from-a-point-to-polygon</a></li> </ol> <p>etc. </p> <p>Having said that: your code suggests you only want to determine whether a <em>single</em> point lies inside the polygon. In that case, why bother, since <code>inpolygon</code> can determine that in under 5 seconds for a million-vertex polygon. </p> <p>Now if you want to do it for a large number of points, but not-so-many vertices in the polygon (or the other way around), you're better off passing a points <strong>array</strong> into the function:</p> <pre><code>function in = pointInPolygon(points, poly) nn = size(poly,1); in = false(nn,1); for ii = 1:size(points,2) x = points(ii,1); y = points(ii,2); yn = false; for jj = 1:size(poly,1) if (poly(jj,2)&lt;y &amp;&amp; poly(nn,2)&gt;=y || ... poly(nn,2)&lt;y &amp;&amp; poly(jj,2)&gt;=y) if (poly(jj,1)+(y-poly(jj,2))/(poly(nn,2)-poly(jj,2))*... (poly(nn,1)-poly(jj,1))&lt;x) yn = ~yn; end end nn = jj; end in(ii) = yn; end end </code></pre> <p>since this enabled Matlab to use JIT to both loops. A simple test: </p> <pre><code>poly = rand(6e6,2); poly = [poly ; poly(1,:)]; xy = rand(1e3,2); tic in = pointInPolygon(xy, poly); toc </code></pre> <p>Results: </p> <pre><code>Elapsed time is 0.324801 seconds. </code></pre> <p>Now, if you want to do the test for a <em>large</em> number of points, with a <em>large</em> number of vertices in the polygon, you're really better off translating all this to C and write a mex file. It is after all a fairly heavy algorithm, and when you're also placing heavy demands on it, you'll have to switch your tool set. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload