简体   繁体   English

有没有一种聪明的方法来确定任意形状内部的点?

[英]Is there a clever way to determine the points that are inside of an arbitrary shape?

My goal is to determine if points lie inside of a shape.我的目标是确定点是否位于形状内。 Consider the following example:考虑以下示例:

import numpy as np
from matplotlib import pyplot as plt
import warnings
warnings.filterwarnings('ignore', 'invalid value encountered in sqrt')

r1 = 10
r2 = 4
a = 12  # x shift for circle 2
b = -4  # y shift for circle 2

theta = np.arange(0, 2*np.pi, 0.0006)

r1_complex = r1*np.exp(1j*theta)
r1_x, r1_y = np.real(r1_complex), np.imag(r1_complex)

r2_complex = r2*np.exp(1j*theta)
r2_x, r2_y = np.real(r2_complex) + a, np.imag(r2_complex) + b

fig, ax = plt.subplots()

ax.plot(r1_x, r1_y)
ax.plot(r2_x, r2_y)

ax.set_aspect('equal')
ax.grid()
plt.show()

output output 在此处输入图像描述

I want to find the points of the blue circle that are inside of the orange circle.我想找到橙色圆圈内的蓝色圆圈的点。 It would be best to try and find it without iteration if possible.如果可能的话,最好在没有迭代的情况下尝试找到它。

For this case, I can easily determine the points that are inside of the orange circle because I know the equation of a circle.对于这种情况,我可以很容易地确定橙色圆圈内的点,因为我知道圆的方程。 Amending the code to this:将代码修改为:

import numpy as np
from matplotlib import pyplot as plt
import warnings
warnings.filterwarnings('ignore', 'invalid value encountered in sqrt')

r1 = 10
r2 = 4
a = 12  # x shift for circle 2
b = -4  # y shift for circle 2

theta = np.arange(0, 2*np.pi, 0.0006)

r1_complex = r1*np.exp(1j*theta)
r1_x, r1_y = np.real(r1_complex), np.imag(r1_complex)

r1_inside_y = np.logical_and(r1_y < np.sqrt(r2**2 - (r1_x - a)**2) + b, r1_y > -np.sqrt(r2**2 - (r1_x - a)**2) + b)

r2_complex = r2*np.exp(1j*theta)
r2_x, r2_y = np.real(r2_complex) + a, np.imag(r2_complex) + b

fig, ax = plt.subplots()

ax.plot(r1_x, r1_y)
ax.plot(r2_x, r2_y)
ax.plot(r1_x[r1_inside_y], r1_y[r1_inside_y])

ax.set_aspect('equal')
ax.grid()
plt.show()

output output 在此处输入图像描述

produces what I'm looking for.产生我正在寻找的东西。 Is there a way to get this same result without knowing the equation for a circle?有没有办法在不知道圆方程的情况下得到同样的结果? Perhaps an algorithm, or clever way with numpy operations?也许是一种算法,或者numpy操作的巧妙方法?

edit编辑

What I mean by arbitrary shape is an kind of closed shape with N number of points.我所说的任意形状是一种具有 N 个点的封闭形状。 Consider this image:考虑这张图片: 在此处输入图像描述

I would like to know the points from the black line that lie inside the bounds of the red line.我想知道红线范围内的黑线点。 For this example, there are two points that this algorithm should find, the x4 and x5 points in blue.对于这个例子,这个算法应该找到两个点,蓝色的 x4 和 x5 点。 And the points x1, x2, ... xN would be coordinate points where both shapes share the same origin.并且点 x1, x2, ... xN 将是两个形状共享相同原点的坐标点。

It turns out, this algorithm has already been standardized in the matplotlib.path module.事实证明,这个算法已经在 matplotlib.path 模块中进行了标准化。 You can produce the same results using the Path class.您可以使用Path class 产生相同的结果。 Consider the following changes to the above code:考虑对上述代码进行以下更改:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import path

r1 = 10
r2 = 4
a = 12  # x shift for circle 2
b = -4  # y shift for circle 2

theta = np.arange(0, 2*np.pi, 0.0006)

r1_complex = r1*np.exp(1j*theta)
r1_x, r1_y = np.real(r1_complex), np.imag(r1_complex)
stacked1 = np.stack((r1_x, r1_y), axis=1)  # A list of coordinates

r2_complex = r2*np.exp(1j*theta)
r2_x, r2_y = np.real(r2_complex) + a, np.imag(r2_complex) + b
stacked2 = np.stack((r2_x, r2_y), axis=1)  # A list of coordinates

p = path.Path(stacked2)
r1_inside = p.contains_points(stacked1)

fig, ax = plt.subplots()

ax.plot(r1_x, r1_y)
ax.plot(r2_x, r2_y)
ax.plot(r1_x[r1_inside], r1_y[r1_inside])

ax.set_aspect('equal')
ax.grid()
plt.show()

This produces the same image without knowledge of the mathematical properties of the shapes.这会在不了解形状的数学属性的情况下生成相同的图像。

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

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