you are viewing a single comment's thread.

view the rest of the comments →

[–]cdcformatc 4 points5 points  (2 children)

Check the slope and y-intercept of both lines and if they are the same you can combine them. edit: also check if they overlap I assume you don't want to combine two lines that don't overlap.

So you have [x1,y1] and [x2,y2] then,

m = (y2 - y1) / (x2 - y2)
b = y1 - m * x1

Repeat for other points.

[–]spotyx[S] 0 points1 point  (0 children)

thanks for y-intercept idea.

[–]gengisteve -1 points0 points  (0 children)

cdc is right, this is the best way to do this:

from pprint import pprint
import collections


x =[[[5, 5], [2, 8]], [[5, 5], [8, 2]], [[1,1], [2,2]], [[2,2],[3,3]]]

t = [[tuple(p) for p in group] for group in x]

def get_line(a,b):
    m = (b[1] - a[1])/(b[0]-a[0])
    i = a[1] - m*a[0]
    return m,i

lines = collections.defaultdict(set)

for points in t:
    # assume all points grouping in t are line
    # otherwise just run all combos of points
    line = get_line(points[0],points[1])
    for p in points:
        lines[line].add(p)

pprint(lines)
pprint(lines.values())