Reputation: 681
I have code that filters points from a list where the distance between them is below 3:
import numpy
lst_x = [0,1,2,3,4,5,6,7,8,9,10]
lst_y = [9,1,3,2,7,6,2,7,2,3,8]
lst = numpy.column_stack((lst_x,lst_y))
diff = 3
new = []
for n in lst:
if not new or ((n[0] - new[-1][0]) ** 2 + (n[1] - new[-1][1]) ** 2) ** .5 >= diff:
new.append(n)
The problem is that the output is:
[array([0, 9]), array([1, 1]), array([4, 7]), array([6, 2]), array([7, 7]), array([8, 2]), array([10, 8])]
point [6,2]
and [8,2]
are closer than 3 to each other yet they are in the results, I believe this is because the for loop is only checking one point then jumping to the next.
How can I make it check all numbers at every point.
Upvotes: 2
Views: 213
Reputation: 77837
Your algorithm very carefully checks against only the point most recently added to the list, new[-1]
. This is insufficient. You need another loop to make sure that you check against each element of new
. Something like this:
for n in lst:
too_close = False
for seen_point in new:
# Is any previous point too close to this one (n)?
if not new or ((n[0] - seen_point[0]) ** 2 + \
(n[1] - seen_point[1]) ** 2) ** .5 < diff:
too_close = True
break
# If no point was too close, add this one to the "new" list.
if not too_close:
new.append(n)
Upvotes: 1