I would consider this an optimization problem and look into respective algorithms to solve it.
The most simple, but really ineffective variant would be brute force. Basically iterating over all lines and seeing which I can take with the line together without creating an overlap. Then you simply need to remember the max combination.
One improvement of this would be to store which lines overlap each other whilst iterating so that you dont need to try them out anymore.
If you want to be more fancy, I would look for optimization algorithms; potentially the knapsack problem is somewhat similar and gives a good starting point.
Here is some python code to get you started.
list_of_lines = [
[4, 5, 9, 5],
[7, 2, 7, 12],
[9, 4, 9, 5]
]
# see https://stackoverflow.com/a/9997374/5240684
def ccw(A,B,C):
return (C.y-A.y) * (B.x-A.x) > (B.y-A.y) * (C.x-A.x)
# Return true if line segments AB and CD intersect
def intersect(A,B,C,D):
return ccw(A,C,D) != ccw(B,C,D) and ccw(A,B,C) != ccw(A,B,D)
class Point(object):
def __init__(self, x,y):
self.x = x
self.y = y
class Line(object):
def __init__(self, a: Point, b: Point):
self.a = a
self.b = b
def intersects(self, line):
return intersect(self.a, self.b, line.a, line.b)
n = 0
for i, line in enumerate(list_of_lines):
a = Point(line[0],line[1])
line1 = Line(a, Point(line[2],line[3]))
for j in range(i+1, len(list_of_lines)):
_l = list_of_lines[j]
line2 = Line(Point(_l[0],_l[1]), Point(_l[2],_l[3]))
if not line1.intersects(line2):
n += 1
n
# returns 2
Note, that this code might have an issue, when the first line is not part of the solution. I have not checked this in detail, but I guess its a starting point anyways.