Scipy 简明教程
SciPy - Spatial
scipy.spatial package 可以利用 Qhull library 来计算一组点的三角剖分、Voronoi 图和凸包。此外,它还包含 KDTree implementations ,用于最近邻点查询和在各种度量中进行距离计算的实用程序。
The scipy.spatial package can compute Triangulations, Voronoi Diagrams and Convex Hulls of a set of points, by leveraging the Qhull library. Moreover, it contains KDTree implementations for nearest-neighbor point queries and utilities for distance computations in various metrics.
Delaunay Triangulations
让我们了解一下什么是 Delaunay 三角剖分,以及它们如何在 SciPy 中使用。
Let us understand what Delaunay Triangulations are and how they are used in SciPy.
What are Delaunay Triangulations?
在数学和计算几何中,对于平面中的一组离散点 P ,Delaunay 三角剖分是一个三角剖分 DT(P) ,使得 P 中的任何点都不在 DT(P) 中的任何三角形的圆内。
In mathematics and computational geometry, a Delaunay triangulation for a given set P of discrete points in a plane is a triangulation DT(P) such that no point in P is inside the circumcircle of any triangle in DT(P).
我们可以通过 SciPy 计算出相同的内容。我们考虑以下示例。
We can the compute the same through SciPy. Let us consider the following example.
from scipy.spatial import Delaunay
points = np.array([[0, 4], [2, 1.1], [1, 3], [1, 2]])
tri = Delaunay(points)
import matplotlib.pyplot as plt
plt.triplot(points[:,0], points[:,1], tri.simplices.copy())
plt.plot(points[:,0], points[:,1], 'o')
plt.show()
上述程序将生成以下输出。
The above program will generate the following output.
Coplanar Points
让我们了解共面点是什么以及它们如何在 SciPy 中使用。
Let us understand what Coplanar Points are and how they are used in SciPy.
What are Coplanar Points?
共面点是位于同一平面的三个或更多点。回忆一下,平面是一个平坦的表面,它无限地向各个方向延伸。在数学教科书中,通常将它表示为一个四边形。
Coplanar points are three or more points that lie in the same plane. Recall that a plane is a flat surface, which extends without end in all directions. It is usually shown in math textbooks as a four-sided figure.
让我们看看如何使用 SciPy 找到这一点。我们考虑以下示例。
Let us see how we can find this using SciPy. Let us consider the following example.
from scipy.spatial import Delaunay
points = np.array([[0, 0], [0, 1], [1, 0], [1, 1], [1, 1]])
tri = Delaunay(points)
print tri.coplanar
上述程序将生成以下输出。
The above program will generate the following output.
array([[4, 0, 3]], dtype = int32)
这意味着点 4 位于三角形 0 和顶点 3 附近,但未包含在三角剖分中。
This means that point 4 resides near triangle 0 and vertex 3, but is not included in the triangulation.
Convex hulls
让我们了解什么是凸包以及它们如何在 SciPy 中使用。
Let us understand what convex hulls are and how they are used in SciPy.
What are Convex Hulls?
在数学中,欧几里得平面或欧几里得空间(或更普遍地,实数上的仿射空间)中的一组点 X 的 convex hull 或 convex envelope 是包含 X 的最小 convex set 。
In mathematics, the convex hull or convex envelope of a set of points X in the Euclidean plane or in a Euclidean space (or, more generally, in an affine space over the reals) is the smallest convex set that contains X.
让我们考虑以下示例来详细理解它。
Let us consider the following example to understand it in detail.
from scipy.spatial import ConvexHull
points = np.random.rand(10, 2) # 30 random points in 2-D
hull = ConvexHull(points)
import matplotlib.pyplot as plt
plt.plot(points[:,0], points[:,1], 'o')
for simplex in hull.simplices:
plt.plot(points[simplex,0], points[simplex,1], 'k-')
plt.show()
上述程序将生成以下输出。
The above program will generate the following output.