Python的polyclip函数驱动程序
项目描述
PyPolyClip
一个用于在像素网格上裁剪多边形的Python包。
polyclip函数最初是为CUBISM项目开发的 Smith et al. 2007 (PASP 119, 1133)。
安装
可以通过命令行使用pip安装此包
pip install pypolyclip
描述
polyclip代码采用Sutherland-Hodgman算法将简单多边形裁剪成由正方形像素组成的镶嵌网格。因此,这与通常在两个任意多边形之间进行裁剪的类似包不同。
可以通过运行测试模块 test_pypolyclip.py 来生成以下示例图形
在每张图中,与给定多边形重叠的每个像素的笛卡尔坐标都会标注出该像素被覆盖的区域面积(像素面积定义为1)。因此,每个多边形各个像素面积的总和应该等于该多边形的总面积。
Pypolyclip 使用一个坐标网格,其中整数像素坐标位于每个像素的左下角,从0开始。要在整数像素坐标位于像素中心的坐标网格上裁剪多边形,需要将输入多边形的x和y顶点各加0.5像素。
第一张图显示了具有不同顶点数的多边形的裁剪,这内部需要使用“for循环”。然而,如果所有多边形的顶点数相同(如第二图所示),则内部使用 NumPy 来提高性能,提升几个百分点。
示例用法
这个第一个例子演示了具有相同顶点数的多边形
import numpy as np
from pypolyclip import clip_multi
# define the size of the pixel grid
naxis = (100, 100)
# create 3 polygons to clip
# the x-vertices of the polygon
px = np.array([[3.4, 3.4, 4.4, 4.4],
[3.5, 3.5, 4.3, 4.3],
[3.1, 3.1, 3.9, 3.9]])
# the y-vertices of the polygon
py = np.array([[1.4, 1.9, 1.9, 1.4],
[3.7, 4.4, 4.4, 3.7],
[2.1, 2.9, 2.9, 2.1]])
# call the clipper
xc, yc, area, slices = clip_multi(px, py, naxis)
# xc, yc are the grid indices with overlapping pixels.
# area is the overlapping area on a given pixel.
# slices is a list of slice objects to link between the input polygons
# and the clipped pixel grid.
# the slices object can be used to get the area of each polygon
for i, s in enumerate(slices):
print(f'total area for polygon {i}={np.sum(area[s])}')
这个第二个例子演示了裁剪具有不同顶点数的多边形。请注意,与第一个例子中的NumPy数组不同,这里的 px
和 py
是列表的列表。
import numpy as np
from pypolyclip import clip_multi
# define the size of the pixel grid
naxis = (100, 100)
# create 3 polygons to clip
# the x-vertices of the polygon
px = [[3.4, 3.4, 4.4, 4.8, 4.4],
[3.5, 3.5, 4.3, 4.3],
[3.1, 3.8, 3.1]]
# the y-vertices of the polygon
py = [[1.4, 1.9, 1.9, 1.65, 1.4],
[3.7, 4.4, 4.4, 3.7],
[2.1, 2.1, 3.4]]
# call the clipper
xc, yc, area, slices = clip_multi(px, py, naxis)
# xc, yc are the grid indices with overlapping pixels.
# area is the overlapping area on a given pixel.
# slices is a list of slice objects to link between the input polygons
# and the clipped pixel grid.
# the slices object can be used to get the area of each polygon
for i, s in enumerate(slices):
print(f'total area for polygon {i}={np.sum(area[s])}')
项目详情
关闭
pypolyclip-1.1.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 312e31034a5b8c4ef4226d391ace649c2475b35eb81795cc315e3bc66366ddb5 |
|
MD5 | 2fc4ae78dfd60de23813810e0e451af0 |
|
BLAKE2b-256 | 283383b01ff926e926de0a9693b3bf5867e8500293ec26da2465e0a8569a991c |
关闭
pypolyclip-1.1.0-cp312-cp312-win_amd64.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | dd3ce07e4220dbb521898bcc06bd73a4d65702ec5af52455840122f05d407c12 |
|
MD5 | b0f6bbd6a6504d99d28cbf8486c59aaa |
|
BLAKE2b-256 | bf86e33c8f2307aa41c255dff75b17afce9025a45a214afae2ba53ec8cfa85fa |
关闭
pypolyclip-1.1.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 9c538b65b212e15efe816a34f66e11599476606c53401ff9c0ce9b8009a758a1 |
|
MD5 | e29169db7aabf2841e3a4567474cd0dd |
|
BLAKE2b-256 | ee56deef8779f20841a0bc2aa1faab84a13f407886e03eb2b9ab865a1192c6b2 |
关闭
pypolyclip-1.1.0-cp312-cp312-macosx_11_0_arm64.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | d1bf0c6473cce778d7152233d2d2c374a79d8335953a8de68db365cd0d192a89 |
|
MD5 | 644748df91e456b9985301c59d81a3bc |
|
BLAKE2b-256 | c3592f89ec0f65466e21aa43dedabbddba9680b2fba797b1105a4e14491652d4 |
关闭
pypolyclip-1.1.0-cp312-cp312-macosx_10_9_x86_64.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 2640de8e33f9cb43b9fc7ff67d2cc4da7bef74f54a95b732fa38caf2eaa7b234 |
|
MD5 | 416c0affe03b6928ab8335adb323cc0f |
|
BLAKE2b-256 | 072588ed0dad2101f8f66f71b56162689f4f7e4c3a50571ab0aab37d245af8c3 |
关闭
pypolyclip-1.1.0-cp311-cp311-win_amd64.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 2c72223e5ecc8894ea8d5e01a4bfe5b355dd0be85b0132b87b3baaba590fbc30 |
|
MD5 | 96c003f3cb20b24ea8ae50d932991d66 |
|
BLAKE2b-256 | 7ec8a19274e0ba2985df273502870f1e8f3b226cae3876d74c559625c963e0b1 |
关闭
pypolyclip-1.1.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 4475d61a715ff9a39d97af69ccc882be661a4f21f5c13a106e3b5abda713d5ec |
|
MD5 | 6487dad66453d37472ed35fef98c6806 |
|
BLAKE2b-256 | 3637aa97cf6754b5d895eb10513afc9a3d666d7da5e20ba88de3583c3e99024e |
关闭
pypolyclip-1.1.0-cp311-cp311-macosx_11_0_arm64.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b123639e48a72828a2dee6aa73c7686206a0ebce0800f60987638a0be2ccbab2 |
|
MD5 | 2972ed0a9f35515803b79c44c2342b84 |
|
BLAKE2b-256 | 96b32897937e1ab152e6226308fbb0d810356845fa1f8631f4437e0af5a9751b |
关闭
pypolyclip-1.1.0-cp311-cp311-macosx_10_9_x86_64.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 68ade279b466183c51834cb27cfcf44b52facfea215443e57bb280457f137d2e |
|
MD5 | 6b7b7f120420d6ea2fec34841558249e |
|
BLAKE2b-256 | ceb66320bc73f3a0eb06a92bc2451e25f9a647cbe1e3a5ecbdcb64906480e897 |
关闭
哈希值 用于 pypolyclip-1.1.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ac2b5bf2875524f372e39b1fcf8fc5391803d18d44ec208220018cf2e0aa3903 |
|
MD5 | cf07026c652533fb326c4951b7509a42 |
|
BLAKE2b-256 | 42fe49b108fcf850b1cdd67318c40084254517c0e929c341637c27f49038aec4 |
关闭
哈希值 用于 pypolyclip-1.1.0-cp310-cp310-macosx_11_0_arm64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 4994dd61d5df3f6a13c7f0944225ae65f6ede2a4d46a3be9a62f3be8ca134292 |
|
MD5 | 3acddc0d3879d4046efc6441cf8be3bc |
|
BLAKE2b-256 | 168d87b895f7e95bd2be77ee4e5d5f15b62c0f5d0dde460a82c4317b41d5390e |
关闭
哈希值 用于 pypolyclip-1.1.0-cp310-cp310-macosx_10_9_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ef67a2cc62dffd4b1f20d35e0efbbd0c7460847768e43e7dbbe54bb6288b185b |
|
MD5 | 06930adc3f39c3c98c089781ae549246 |
|
BLAKE2b-256 | 2a46b9e3ec31f81a5c84e34609397fff80319a81413de7f32709924e7f3e7851 |
关闭
哈希值 用于 pypolyclip-1.1.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b081f13043cf2db051edf56cef1b0dcbc7a97d917dd097e498fd1e9eefd19d42 |
|
MD5 | e7bf7d787ab343af6d12bf241c4f91b7 |
|
BLAKE2b-256 | 6c043e4ef467e1c2780bd646912e62adaca8ce546ba574ed1557361bd8e8d840 |
关闭
哈希值 用于 pypolyclip-1.1.0-cp39-cp39-macosx_11_0_arm64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | cb57afb0fc3a2044cc2b934675ca884fbc4cda1705a6be09e3612201b73d41d2 |
|
MD5 | 0b8239cc044c5d335f6ad7f0e385654a |
|
BLAKE2b-256 | 5c8c754fe7fa257d200ff10e80187b43c6776c728515453d508ff6a36f8cf893 |
关闭
哈希值 用于 pypolyclip-1.1.0-cp39-cp39-macosx_10_9_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 16e88f23fa27d89041b80c5b87150a0438b6617ecaf61e3e7a965b4554c2e28b |
|
MD5 | f409e6e0b4da01bc93b57a2d479e7b64 |
|
BLAKE2b-256 | b06ed30be2e169ff9f81bd189fbab6ff44b583c1bf68a58af80fe6aa999dfe43 |