Skip to content
Advertisement

Removing the 2d point that are close to each others

I would like to remove the coordinates that lie close to each other or if it is just a duplicate.

For example,

JavaScript

In the above list, the first and second element lies close to each other. How do I remove the second element while preserving the first one?

Following is what I have tried,

JavaScript

The problem is that it doesn’t recursively check if there are any other duplicates since it only checks the adjacent coordinates. What is an efficient way of filtering the coordinate?

Sample Input with thresh = (10,10):

JavaScript

Sample output:

JavaScript

Advertisement

Answer

Your question is a bit vague, but I’m taking it to mean:

  1. You want to compare all combinations of points
  2. If a combination contains points closer than a threshold
  3. Then remove the point further from the start of the input list

Try this:

JavaScript

The way this works is to generate all combinations of points using itertools (which leaves the points in the original order), and then create a list of points to remove using the threshold. Then it returns a list of points not in that list of points to remove.

You’ll notice that I have an extra point than you. I simply copied what seemed to be your intended functionality (i.e. both dy AND dx <= thresh for point removal). However, if I change the line with the AND statement to remove point if dy OR dx <= thresh, I get the same output as your sample.

So I’m going to ask you to recheck your sample output.

BTW, it might be useful for you to confirm if checking for x and y proximity separately is what you really want. So as a bonus, I’ve included a version using the Euclidean distance as well:

JavaScript

This version fits your original sample when I used a threshold radius of 100.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement