擅长:python、mysql、java
<p>我编写此函数是为了返回一个数组,如<code>matlab</code><code>inpolygon</code>函数中所示。但这将只返回给定多边形内的点。使用此函数无法在多边形的边上找到点。</p>
<pre><code>import numpy as np
from matplotlib import path
def inpolygon(xq, yq, xv, yv):
shape = xq.shape
xq = xq.reshape(-1)
yq = yq.reshape(-1)
xv = xv.reshape(-1)
yv = yv.reshape(-1)
q = [(xq[i], yq[i]) for i in range(xq.shape[0])]
p = path.Path([(xv[i], yv[i]) for i in range(xv.shape[0])])
return p.contains_points(q).reshape(shape)
</code></pre>
<p>可以将函数调用为:</p>
<pre><code>xv = np.array([0.5,0.2,1.0,0,0.8,0.5])
yv = np.array([1.0,0.1,0.7,0.7,0.1,1])
xq = np.array([0.1,0.5,0.9,0.2,0.4,0.5,0.5,0.9,0.6,0.8,0.7,0.2])
yq = np.array([0.4,0.6,0.9,0.7,0.3,0.8,0.2,0.4,0.4,0.6,0.2,0.6])
print(inpolygon(xq, yq, xv, yv))
</code></pre>
<p>在<a href="https://in.mathworks.com/help/matlab/ref/inpolygon.html" rel="nofollow noreferrer">^{<cd1>} documentation</a>这个函数中</p>
<blockquote>
<p>returns in indicating if the query points specified by xq and yq are inside <s>or on the edge</s> of the polygon area defined by xv and yv. </p>
</blockquote>