查找至少部分位于任意方向矩形内的所有像素
我有一个带有实值顶点 (x0,y0)
、(x1,y1)
、(x2,y2)
、(x3,y3)
,它可以在平面上以任何角度定向。我正在寻找一种有效的方法来查找(或迭代)至少部分位于该矩形内的所有像素(即 1x1 正方形)。
对于正交定向的矩形来说,执行此操作很简单,并且检查任何特定像素是否在矩形内也很简单。我可以检查矩形边界框中的每个像素,但在最坏的情况下,当目标矩形内只有 O(n) 时,我将检查 O(n^2) 个像素。 (这是当目标矩形处于 45 度且非常长且窄时的情况。)
I have a rectangle with real-valued vertices (x0,y0)
, (x1,y1)
, (x2,y2)
, (x3,y3)
, which may be oriented at any angle in the plane. I'm looking for an efficient way of finding (or iterating over) all pixels (i.e., 1x1 squares) which are at least partially within this rectangle.
It's trivial to do this for rectangles which are oriented orthogonally, and it's also trivial to check whether any particular pixel is within the rectangle. I could check each pixel within the rectangle's bounding box, but in the worst case I would be checking O(n^2) pixels when only O(n) will be within the target rectangle. (This is when the target rectangle is at 45 degrees and is very long and narrow.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以计算 x 方向的范围(最小 x 坐标的下限,到最大 x 坐标的上限)。对于该范围内的每个 x,您可以计算 y 方向的范围。在一般情况下,您需要考虑几种不同的情况,具体取决于矩形的方向。
本质上,你有一个最左边的点,一个最右边的点,一个上面的点和一个下面的点。
y1
将从最左边开始,穿过下部,最后在最右边的点结束。y2
将穿过上点。为了包含所有接触的像素,我们需要在所有方向上查看半个像素。我选择使用每个像素的中心作为坐标。这样做是为了让最终图像看起来更自然。
以下是一些要演示的 F# 代码:
示例:
You can compute the range in the x-direction (floor of the minimum x-coordinate, to the ceiling of the maximum x-coordinate). For each x in that range, you can compute the range in the y-direction. You have a few different cases to consider in the generic case, depending on how the rectangle is oriented.
In essence, you have one leftmost point, one rightmost point, one upper point and one lower point.
y1
will start at the leftmost, go trough the lower, and end in the rightmost point.y2
will instead go trough the upper point.To include all touching pixels, we need to look half a pixel in all directions. I chose to use the center of each pixel as the coordinates. This was so that you get a more natural look of the final image.
Here are some F#-code to demonstrate:
Example:
你能使用格雷厄姆扫描之类的东西吗?
您可以使用 5 个点的集合(像素 + 4 个顶点),然后检查 4 个顶点是否定义了凸包的边界。这在最坏的情况下是 O(n log n),这对于大 n 来说是对 n^2 的显着改进。
或者,二维范围树可能就足够了,尽管我认为这仍然是 n log n
编辑:
实际上,您可以使用 4 个顶点之间的角度来创建像素可能位于的 4 个“范围”,然后只需取这 4 个范围的交集即可。这将是一个恒定时间操作,并且检查像素是否位于该范围内也是恒定时间 - 只需将它与每个顶点形成的角度与上述角度集进行比较即可。
作为另一种选择,使用 4 条边界线(相邻顶点之间的线)并在它们之间“行走”。一旦到达该线,任何进一步向下的点都不会位于该边界内,等等。矩形内的像素数量是 O(n),并且应该可以通过简单的广度优先搜索轻松解决
Could you use something like a Graham scan?
You could use the set of 5 points (the pixel + the 4 vertexes) and then check to see whether the 4 vertexes define the boundary of the convex hull. This would be at worst O(n log n), which is a marked improvement on your n^2 for large n.
Alternatively, a two-dimensional range tree might suffice, though I think this will still be n log n
EDIT:
Actually, you could use the angles between the 4 vertexes to create 4 "ranges" where pixels could potentially be located, then just take the intersection of these 4 ranges. That would be a constant time operation, and checking whether a pixel lies within this range is also constant time - just compare the angle it makes with each vertex against the above set of angles.
As another alternative, use the 4 boundary lines (the lines between adjacent vertexes) and just 'walk' between them. Once you hit the line, any further points downwards won't lie within this boundary, etc. That's O(n) on the amount of pixels that lie within the rectangle, and should easily be solved with a trivial breadth-first search
以下是一些基于 的 Python 代码MizardX 的答案 正是我想要的:
输出:
Here's some Python code based on MizardX's answer which does exactly what I was wanting:
Output: