查找包含最大相互等距点的直线

时间:2017-04-29 07:05:22

标签: grid line computational-geometry

给定R(行),C(coloumns),N坐标(x,y),其中x <= R&amp;&amp; Y'LT; = C, 我们需要找到包含最大相互等距点的直线。最终目标是找到该线上的那些点数。

通过相互等距,意味着该线上的所有点应与其相邻点的距离相等。 例如:(1,1),(3,3),(5,5),(7,7),(9,9)。

我们不需要考虑包含非相互等距点的线。 例如:(1,1),(2,2),(4,4)。

我尝试使用近N ^ 3方法解决它,但它无法通过所有测试用例。

我的方法:

For every pair of two points i and j:
 Consider another point k:
     if(slope(i,j) == slope(i,k))
       They are at same line, map these points with the calculated slope.

Set ans = 0.
Then for every slope in the map,
sort all the mapped points and 
check whether they are
mutually equidistant.
If they are mutually equidistant,
and their count is greater than ans,
Set ans=count.
Output = ans

有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

这样做的简单方法是通过选择开始两个点来尝试每个可能的行,并且在输入点中检查下一个点。对于那个商店中的结构集点(更快的检查是指向的)。

max_line = []
For every (un-ordered) pair of two points i and j:
  line = [i, j]  # Line starts with i, second point is j
  slope = j - i
  k = j + slope  # Third line point
  while k in points:  # Checking is next line point in
    line.append(k)
    k += slope   # Proceed to next point
  if len(line) > len(max_line):
    max_line = line
print(max_line)