简体   繁体   English

查找点是否包含在两条曲线之间

[英]Finding if points are contained between two curves

I came accross a bit of a problem and couldn't find a satisfying answer anywhere.我遇到了一点问题,在任何地方都找不到令人满意的答案。

I'm looking to find the points contained between two curves, with the fact that both curves are kind of parametric, and don't have the same number of x-elements.我正在寻找两条曲线之间包含的点,事实上两条曲线都是参数化的,并且没有相同数量的 x 元素。

The matplotlib function fill works perfectly to fill with polygons the area between the curves. matplotlib 函数fill可以完美地用多边形填充曲线之间的区域。 I've stumbled upon the contains_point function from matplotlib_path , but I can't find a way to get the properties of the matplotlib.patches.Polygon output from fill to use it.我偶然发现了来自matplotlib_pathcontains_point函数,但我找不到从fill获取matplotlib.patches.Polygon输出的属性以使用它的方法。

Is there a way to use this, or should I take another approach possible, by doing directly what the fill function does to obtain the polygon (but how) ?有没有办法使用它,或者我应该采取另一种可能的方法,通过直接执行fill函数来获取多边形(但如何)?

Plot illustating the problem:说明问题的图:绘图说明问题

For anyone interested, I managed to resolve my problem.对于任何感兴趣的人,我设法解决了我的问题。 Using shapely and descartes (a simple pip install do the trick),使用shapelydescartes (一个简单的 pip install 就可以了),

from shapely.geometry.polygon import Polygon
from shapely.geometry import Point
from descartes import PolygonPatch

you can create the needed polygon with Polygon function by feeding it a tuple filed with x and y of both curves appended.您可以使用Polygon函数创建所需的多边形,方法是向它提供一个附加了两条曲线的 x 和 y 的元组。 You can create the polygon patch with the PolygonPatch function from shapely and plot it using:您可以使用PolygonPatch函数从PolygonPatch创建多边形补丁并使用以下方法绘制它:

ax.add_patch(name_of_your_patch)

To check if a point is included inside the polygon, you can create a Point object with shapely and the function:要检查多边形内是否包含一个点,您可以使用 shapely 和函数创建一个Point对象:

name_of_your_polygon.contains(point) 

will return a boolean.将返回一个布尔值。

Here is an example script (remember to import shapely and descartes ):下面是一个示例脚本(记得要导入shapelydescartes ):

main=rand(4,2)
main=tuple(map(tuple,main))    #Transform into tuple
poly=Polygon(main)             #Create filling Polygon
poly_patch=PolygonPatch(poly)  #Create Polygon Patch

fig = plt.figure()
ax = fig.add_subplot(111)

ax.add_patch(poly_patch)       #Draws the patch
point=Point(0.5,0.5)
print(poly.contains(point))
ax.scatter(0.5,0.5,s=10000,color='gold',marker='+')

In the case I plotted below, the poly.contains(point) function returns True .在我在下面绘制的情况下, poly.contains(point)函数返回True

Example:例子:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM