如何在给定的矩形列表中找到点所在位置?(在小于O(n)的复杂度下)

huangapple go评论64阅读模式
英文:

How to find where does Point lies in given list of rectangles ? (In less than O(n) complexity)

问题

这个问题相关的问题陈述如下:

有一个矩形列表,每个矩形有两个给定点。

每个矩形由x0,y0,x1,y1表示。

  1. x0,y0 - 表示左上角或起始点
  2. x1,y1 - 表示右下角或结束点

注意:我们可以按任何格式对列表进行排序。假设(可以更改)列表是基于起始点排序的。

需要找到给定点X、Y完全位于其中的最佳矩形。

如果有多个矩形重叠,可以选择面积最小的矩形。

稍后可以改进为距离任何矩形角落的最短距离。

输入:

点:(X,Y)=(1450,380)

矩形列表:

[
  {
    "x0": 1797,
    "x1": 1854,
    "y0": 333,
    "y1": 434
  },
  {
    "x0": 1671,
    "x1": 1688,
    "y0": 423,
    "y1": 434
  },
  {
    "x0": 1565,
    "x1": 1594,
    "y0": 366,
    "y1": 378
  },
  {
    "x0": 1547,
    "x1": 1552,
    "y0": 112,
    "y1": 146
  },
  {
    "x0": 1439,
    "x1": 1457,
    "y0": 373,
    "y1": 396
  }
]

输出:

{
    "x0": 1439,
    "x1": 1457,
    "y0": 373,
    "y1": 396
}

找到的一种简单方法是遍历所有矩形,找到包含点的最小矩形。这在时间复杂度上可以达到O(n)。

是否有比O(n)更低复杂度的解决重叠场景并获得最佳矩形的更好解决方案?

英文:

The problem statement bit related to this one.

There is a list of rectangles that has 2 points given.

Each Rectangle is represented by x0,y0,x1,y1.

  1. x0,y0 - Represents top left or starting point
  2. x1,y1 - Represents bottom right or ending point

Note: We can sort the list in any format. Assume (can be changed) the list is sorted based on starting point.

Need to find given best rectangle where point X,Y lies in completely.

If more than one rectangle is overlapping, one can choose the rectangle with the smallest area.

This can be improved later to the shortest distance from any rectangle corner.

Input:

Point: (X, Y) = (1450,380)

List of rectangles :

[
  {
    "x0": 1797,
    "x1": 1854,
    "y0": 333,
    "y1": 434
  },
  {
    "x0": 1671,
    "x1": 1688,
    "y0": 423,
    "y1": 434
  },
  {
    "x0": 1565,
    "x1": 1594,
    "y0": 366,
    "y1": 378
  },
  {
    "x0": 1547,
    "x1": 1552,
    "y0": 112,
    "y1": 146
  },
  {
    "x0": 1439,
    "x1": 1457,
    "y0": 373,
    "y1": 396
  }
]

Output:

  {
    "x0": 1439,
    "x1": 1457,
    "y0": 373,
    "y1": 396
  }

One simple way to find is to iterate through all rectangles and find the smallest rectangle where the point lies. This is possible O(n) in complexity.

Is there a better solution for getting the best rectangle resolving overlapping scenarios in less complexity than O(n)?

答案1

得分: 2

二分搜索不适用,因为您只能在一个轴上进行分区,但空间分区算法应该适用。例如,四叉树数据结构可以将搜索的复杂性在非退化成功案例中轻松降低到O(logn)

但可以轻松构建一个需要线性扫描的退化情况,其中数组的所有元素都是相同的矩形。这实际上取决于数据的输入方式以及您对标签的要求。

英文:

A binary search won't work because you can only partition on one axis, but a spatial partitioning algorithm should work. For example, a quadtree data structure can trivially reduce the complexity of your search to O(logn) in a non-degenerate success case.

One can easily build a degenerate case that requires a linear scan however, where you have all elements of your array as the same rectangle. It's really up to how your data is coming in and how pedantic you want to be with labels.

huangapple
  • 本文由 发表于 2023年2月16日 02:29:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定