Last active
December 21, 2018 09:19
-
-
Save fortable1999/d628708bef997247e5ef8eae5b9768c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point: | |
def __init__(self, a=0, b=0): | |
self.x = a | |
self.y = b | |
def point_equals(p1, p2): | |
return abs(p1[0] - p2[0]) < 0.0000001 and abs(p1[1] - p2[1]) < 0.0000001 | |
class Line(): | |
def __init__(self, **kwargs): | |
for k, v in kwargs.items(): | |
setattr(self, k, v) | |
self.points = [] | |
self.points.append([float(kwargs['x1']), float(kwargs['y1'])]) | |
self.points.append([float(kwargs['x2']), float(kwargs['y2'])]) | |
def on_line(self, x, y): | |
if [x, y] in self.points: | |
return True | |
if abs(self.x2 - self.x1) < 0.0000001: | |
# vertical | |
return abs(x - self.x1) < 0.0000001 | |
else: | |
if abs(x - self.x2) < 0.0000001: | |
return False | |
# print('here', (y - self.y2) / (x - self.x2), (self.y2 - self.y1) / (self.x2 - self.x1)) | |
return abs((y - self.y2) / (x - self.x2) - (self.y2 - self.y1) / (self.x2 - self.x1)) < 0.0000001 | |
def add_to_line(self, x, y): | |
self.points.append([float(x), float(y)]) | |
def __repr__(self): | |
return "(%d, %d) (%d, %d) points %s" % (self.x1, self.y1, self.x2, self.y2, str(self.points)) | |
class Solution(object): | |
def maxPoints(self, points): | |
""" | |
:type points: List[Point] | |
:rtype: int | |
""" | |
if len(points) < 2: | |
return len(points) | |
lines = [] | |
for i1 in range(1, len(points)): | |
if not abs(points[0].x - points[i1].x) < 0.0000001 or not abs(points[0].y - points[i1].y) < 0.0000001: | |
lines = [Line(x1=points[0].x, y1=points[0].y, | |
x2=points[i1].x, y2=points[i1].y,)] | |
for idx in range(1, i1): | |
lines[0].add_to_line(points[idx].x, points[idx].y) | |
break | |
else: | |
return len(points) | |
for idx in range(i1 + 1, len(points)): | |
point = [points[idx].x, points[idx].y] | |
new_lines = [] | |
for line in lines: | |
if line.on_line(*point): | |
# print("point %s in line %s" % (str(point), str(line))) | |
# one a line | |
line.add_to_line(*point) | |
else: | |
# add new lines to lines collection | |
for p in line.points: | |
new_lines.append(Line(x1=point[0], y1=point[1], x2=p[0], y2=p[1])) | |
lines = lines + new_lines | |
print(len(lines)) | |
return max([len(l.points) for l in lines]) | |
def list2p(points): | |
return [Point(p[0], p[1]) for p in points] | |
# def next_in_line(lines, point): | |
# """docstring for max_in_line""" | |
# pass | |
# line1 = Line(x1=1, y1=1, x2=3, y2=2) | |
# | |
# print(line1.on_line(4,1)) | |
# print(line1.on_line(300,301)) | |
sol = Solution() | |
# print(sol.maxPoints([[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]])) | |
# print(sol.maxPoints(list(list2p([[1,1],[2,2],[3,3]])))) | |
# print('=====') | |
print(sol.maxPoints(list(list2p([[0,-12],[5,2],[2,5],[0,-5]])))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment