Skip to content

Instantly share code, notes, and snippets.

@colonelpanic8
Created August 15, 2024 00:02
Show Gist options
  • Save colonelpanic8/96ed1f912721a52b347782ce4bfb2ed1 to your computer and use it in GitHub Desktop.
Save colonelpanic8/96ed1f912721a52b347782ce4bfb2ed1 to your computer and use it in GitHub Desktop.
infinite_slope = object()
from collections import defaultdict
def slope_intercept(p1, p2):
run = p2[0] - p1[0]
rise = p2[1] - p2[1]
if run == 0:
slope = infinite_slope()
intercept = p2[0]
else:
slope = rise / run
intercept = p2[1] - slope * p2[0]
return slope, intercept
def get_max_collinear_points(points):
line_to_points = defaultdict(set())
for i, point1 in enumerate(points):
for point2 in points[i + 1:]:
line_representation = slope_intercept(point1, point2)
line_points = line_to_points[line_representation]
line_points.add(point1)
line_points.add(point2)
maximizing_line = max(line_to_points.items(), key=lambda v: len(v[1]))
return len(maximizing_line[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment