Rankinstudio
Rankinstudio

Reputation: 612

Python List of Paired Points, How to Find Points Along Line

I have a set of functions that generate a list of paired points like this:

my_list=[((57.5, 3048.0), (62.0, 3050.0)), ((82.0, 1006.0), (93.0, 1021.5)), ((95.0, 1354.0), (86.0, 1336.0)), ((95.0, 1354.0), (89.0, 1327.0))...]

Such that each pair is:

((57.5, 3048.0), (62.0, 3050.0)) Pair 0
((82.0, 1006.0), (93.0, 1021.5)) Pair 1
((95.0, 1354.0), (86.0, 1336.0)) Pair 2
((95.0, 1354.0), (89.0, 1327.0)) Pair 3
((104.0, 2366.0), (109.0, 2350.0)) Pair 4
((104.0, 2370.0), (109.0, 2350.0)) Pair 5
((122.5, 2375.0), (109.0, 2350.0)) Pair 6

etc.

This is a long list of X,Y coordinates generated from analyzing images. I need to find a way to reject most of the points.

Anything less than 3 points that don't fall on a similar line (not exact, need some wiggle room) needs to be rejected.

Most of the points are clusters not on a line, or only two points on a line. Is there a way to run through this list and only save the set of 3 or more points on roughly the same line?

Here is an image to demonstrate what I'm after. The image shows the faint moving asteroid found by the functions and all of the noise also found. You can see the asteroid has 3 + points roughly on the same line while the noise is rather random. I am trying to discard the points that are only two points on the same line.

enter image description here

A lot to ask, but essential!

After implementing answer 1

enter image description here

Upvotes: 2

Views: 2310

Answers (1)

Mark Perryman
Mark Perryman

Reputation: 779

You can find an equation for the distance of one point from the line between two other points. (https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line).

def distance(point, segment):
    """Distance from a point to a line formed by a segment"""
    ...

Then loop through all the segments and calculate the distance from each point. You will need a threshold for "close enough". You could then make a list of all the points close to a line. Something like this.

ON_LINE_THRESHOLD = 1.5

for segment in my_list:
    close_points = []
    for point1, point2 in my_list:
        if distance(point1, segment) < ON_LINE_THRESHOLD:
            close_points.append(point1)
        if distance(point2, segment) < ON_LINE_THRESHOLD:
            close_points.append(point2)
    if len(close_points) >=3:
        # Close points will always be at least two as it includes the end points.
        do something!

What you then do with the list of points on a line is up to you.

Upvotes: 2

Related Questions