简体   繁体   English

计算网格中每个单元格与 2D 对象重叠的比例

[英]Calculating the fraction of each cell in a grid overlapped by a 2D object

I have an arbitrary rectangular Cartesian grid divided into potentially 10^6 or so rectangular cells.我有一个任意矩形笛卡尔网格,分为潜在的 10^6 个左右的矩形单元格。 (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; (任意意味着 $x$ 网格沿着点 $x_1,...x_n$ 并且 $y$ 网格也是如此。)我想在它上面绘制一个任意对象(比如一个旋转的矩形,或圆),并有效地计算每个单元格与对象重叠的部分:如果单元格完全在对象的边界内,则为 1.0; if the cell is entirely outside, 0.0;如果单元格完全在外面,则为 0.0; if half of the cell is covered by the object, 0.5.如果单元格的一半被对象覆盖,则为 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.如果将其显示为图像并将其缩放为 1 为黑色,0 为白色,则结果看起来像黑色对象的抗锯齿绘图。

My application for this question is in Python, and it seems like this capability might be provided by some existing graphics library.我对这个问题的应用程序是用 Python 编写的,而且似乎某些现有图形库可能会提供此功能。 Is there a Python module that will test for the fractional intersection of a rectangle and an arbitrary object?是否有一个 Python 模块可以测试矩形和任意对象的分数交集? Is there a Python library that can at least efficiently test if a point is inside an arbitrary object like a rotated rectangle?是否有一个 Python 库至少可以有效地测试一个点是否在任意对象(如旋转矩形)内?

You could use PyCairo , which has fast native routines to do its drawing.您可以使用PyCairo ,它具有快速的本机例程来进行绘图。 It's antialiased by default.默认情况下它是抗锯齿的。

Implementing the drawing algorithms in Python would be very slow.在 Python 中实现绘图算法会很慢。

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/要找到由多边形正方形相交产生的梯形面积,您可以按照 Sean Barrett 在https://nothings.org/gamedev/rasterize/ 上描述的过程进行操作

The shapely Python library can find the area of a trapezoid and perform point-in-object tests. shapely Python 库可以找到梯形的面积并执行对象内点测试。 However, for best performance this sounds like something that you'd want to write in C/C++ and provide numpy bindings.但是,为了获得最佳性能,这听起来像是您想用 C/C++ 编写并提供 numpy 绑定的内容。

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

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