vtk.js

vtk.js


判断一个点是否落在多边形区域内

<pre><code class="language-javascript">/** * 判断一个点是否落在多边形区域内 * @param {*} polygon * @param {*} pointX * @param {*} pointY * @returns polygon---组成多边形的点 [x1, y1, x2, y2, x3, y3, ...] */ export function ContainsPoint(polygon, pointX, pointY) { const n = polygon.length &amp;gt;&amp;gt; 1; let ax, lup; let ay = polygon[2 * n - 3] - pointY; let bx = polygon[2 * n - 2] - pointX; let by = polygon[2 * n - 1] - pointY; if (bx === 0 &amp;amp;&amp;amp; by === 0) return false; // point on edge // let lup = by &amp;gt; ay; for (let ii = 0; ii &amp;lt; n; ii++) { ax = bx; ay = by; bx = polygon[2 * ii] - pointX; by = polygon[2 * ii + 1] - pointY; if (bx === 0 &amp;amp;&amp;amp; by === 0) return false; // point on edge if (ay === by) continue; lup = by &amp;gt; ay; } let depth = 0; for (let i = 0; i &amp;lt; n; i++) { ax = bx; ay = by; bx = polygon[2 * i] - pointX; by = polygon[2 * i + 1] - pointY; if (ay &amp;lt; 0 &amp;amp;&amp;amp; by &amp;lt; 0) continue; // both 'up' or both 'down' if (ay &amp;gt; 0 &amp;amp;&amp;amp; by &amp;gt; 0) continue; // both 'up' or both 'down' if (ax &amp;lt; 0 &amp;amp;&amp;amp; bx &amp;lt; 0) continue; // both points on the left if (ay === by &amp;amp;&amp;amp; Math.min(ax, bx) &amp;lt; 0) return true; if (ay === by) continue; const lx = ax + ((bx - ax) * -ay) / (by - ay); if (lx === 0) return false; // point on edge if (lx &amp;gt; 0) depth++; if (ay === 0 &amp;amp;&amp;amp; lup &amp;amp;&amp;amp; by &amp;gt; ay) depth--; // hit vertex, both up if (ay === 0 &amp;amp;&amp;amp; !lup &amp;amp;&amp;amp; by &amp;lt; ay) depth--; // hit vertex, both down lup = by &amp;gt; ay; } return (depth &amp;amp; 1) === 1; }</code></pre>

页面列表

ITEM_HTML