Grid 求包含最大相互等距点的直线

Grid 求包含最大相互等距点的直线,grid,line,computational-geometry,Grid,Line,Computational Geometry,给定R(行),C(列),N个坐标(x,y),其中x简单的方法是通过选择起始点来尝试每一条可能的线,并且检查也是输入点中的下一个点。因此,在结构集中存储点(更快的检查是点入) 但这个解的顺序是N^3。我已经有一个正在工作的N^3。正如我在问题本身中提到的,一般情况下是N^2。在最坏的情况下,当所有点都在一条线上时,它是N^3。这可以通过只测试一个行的方向来处理(但是,这里存在一个问题,我们不需要考虑包含非相互等距点的行)。例如:(1,1),(2,2),(3,3),(4,4),(6,6)。根据你的伪

给定R(行),C(列),N个坐标(x,y),其中x简单的方法是通过选择起始点来尝试每一条可能的线,并且检查也是输入点中的下一个点。因此,在结构集中存储点(更快的检查是点入)


但这个解的顺序是N^3。我已经有一个正在工作的N^3。正如我在问题本身中提到的,一般情况下是N^2。在最坏的情况下,当所有点都在一条线上时,它是N^3。这可以通过只测试一个行的方向来处理(但是,这里存在一个问题,我们不需要考虑包含非相互等距点的行)。例如:(1,1),(2,2),(3,3),(4,4),(6,6)。根据你的伪代码,这些将被考虑为ANS=4(最大4点在同一行)。对于这种情况,答案应该是0。
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
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)