d3-polygon
此模块提供了一些二维多边形的基本几何运算。每个多边形都表示为一个由双元素数组 [[x0, y0], [x1, y1], …] 组成的数组,可以是闭合的(其中第一个点和最后一个点相同),也可以是开放的(其中第一个点和最后一个点不同)。Typically polygons are in counterclockwise order, assuming a coordinate system where the origin is in the top-left corner.
¥This module provides a few basic geometric operations for two-dimensional polygons. Each polygon is represented as an array of two-element arrays [[x0, y0], [x1, y1], …], and may either be closed (wherein the first and last point are the same) or open (wherein they are not). Typically polygons are in counterclockwise order, assuming a coordinate system where the origin is in the top-left corner.
polygonArea(polygon)
d3.polygonArea([[1, 1], [1.5, 0], [2, 1]]) // -0.5
源代码 · 返回指定多边形的有符号面积。如果多边形的顶点按逆时针方向排列(假设坐标系的原点位于左上角),则返回的面积为正数;否则为负数或零。
¥Source · Returns the signed area of the specified polygon. If the vertices of the polygon are in counterclockwise order (assuming a coordinate system where the origin is in the top-left corner), the returned area is positive; otherwise it is negative, or zero.
polygonCentroid(polygon)
d3.polygonCentroid([[1, 1], [1.5, 0], [2, 1]]) // [1.5, 0.6666666666666666]
¥Source · Returns the centroid of the specified polygon.
polygonHull(points)
d3.polygonHull(points) // [[3.0872864263338777, -1.300100095019402], [1.6559368816733773, -2.5092525689499605], …]
源代码 · 使用 Andrew 的单调链算法 返回指定点的 凸包。返回的外壳表示为一个数组,其中包含按逆时针顺序排列的输入点子集。如果 points 的元素少于三个,则返回 null。
¥Source · Returns the convex hull of the specified points using Andrew’s monotone chain algorithm. The returned hull is represented as an array containing a subset of the input points arranged in counterclockwise order. Returns null if points has fewer than three elements.
polygonContains(polygon, point)
d3.polygonContains([[1, 1], [1.5, 0], [2, 1]], [1.5, 0.667]) // true
源代码 · 当且仅当指定的点位于指定的多边形内,则返回 true。
¥Source · Returns true if and only if the specified point is inside the specified polygon.
polygonLength(polygon)
d3.polygonLength([[1, 1], [1.5, 0], [2, 1]]) // 3.23606797749979
源代码 · 返回指定多边形的周长。
¥Source · Returns the length of the perimeter of the specified polygon.