计算网格中每个单元格与 2D 对象重叠的比例
我有一个任意矩形笛卡尔网格,分为可能 10^6 个左右的矩形单元。 (任意意味着 $x$ 网格沿着点 $x_1,...x_n$ ,$y$ 网格也是如此。)我想在它上面绘制一个任意对象(比如一个旋转的矩形,或圆圈),并有效地计算每个单元格与对象重叠的比例:如果单元格完全在对象的边界内,则为 1.0;如果单元完全在外部,则为 0.0;如果一半的单元格被物体覆盖,则为 0.5。如果将其显示为图像并对其进行缩放,其中 1 为黑色,0 为白色,则结果将看起来像黑色对象的抗锯齿绘图。
我对这个问题的应用程序是用Python编写的,看起来这个功能可能是由一些现有的图形库提供的。是否有一个 Python 模块可以测试矩形和任意对象的分数交集?是否有一个 Python 库至少可以有效地测试一个点是否位于任意对象(如旋转的矩形)内?
I have an arbitrary rectangular Cartesian grid divided into potentially 10^6 or so rectangular cells. (Arbitrary means that the $x$ grid is along points $x_1,...x_n$ and the same goes for the $y$ grid.) I would like to draw an arbitrary object on top of it (say a rotated rectangle, or a circle), and efficiently calculate what fraction of each cell is overlapped by the object: if the cell is entirely inside the bounds of the object, 1.0; if the cell is entirely outside, 0.0; if half of the cell is covered by the object, 0.5. If you displayed this as an image and scaled it where 1 is black and 0 is white, the result would look like an antialiased drawing of the black object.
My application for this question is in Python, and it seems like this capability might be provided by some existing graphics library. Is there a Python module that will test for the fractional intersection of a rectangle and an arbitrary object? Is there a Python library that can at least efficiently test if a point is inside an arbitrary object like a rotated rectangle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 PyCairo,它具有快速的本机例程来进行绘图。默认情况下它是抗锯齿的。
用 Python 实现绘图算法会非常慢。
You could use PyCairo, which has fast native routines to do its drawing. It's antialiased by default.
Implementing the drawing algorithms in Python would be very slow.
要查找由多边形与正方形相交得到的梯形面积,您可以按照 Sean Barrett 在 中描述的过程进行操作https://nothings.org/gamedev/rasterize/
shapely
Python 库可以找到梯形的面积并执行对象中的点测试。然而,为了获得最佳性能,这听起来像是您想要用 C/C++ 编写并提供 numpy 绑定的东西。To find the area of a trapezoid resulting from a polygon-square intersection, you can follow the process described by Sean Barrett at https://nothings.org/gamedev/rasterize/
The
shapely
Python library can find the area of a trapezoid and perform point-in-object tests. However, for best performance this sounds like something that you'd want to write in C/C++ and provide numpy bindings.