import sys class Point(object): def __init__(self, x, y): (self.x, self.y) = x, y class Edge(object): def __init__(self, p1, p2): (self.p1, self.p2) = (p1, p2) class Line(object): def __init___(self, m, x, n): (self.m, self.n) = (m, x, n) class Polygon(object): def __init__(self, points): ''' Assuming order of vertices as the order of edges. edges are: (points[0], points[1]), (points[1], points[2]),..., (points[-1], points[0) ''' self.points = points self.edges = zip(points, points[1:]) + [(points[-1], points[0])] def calculate_normals(self): for edge in self.edges: middle_point = Point((edge[0].x + edge[1].x)/ 2, (edge[0].y + edge[1].y) / 2) def find_farthest_point_in_direction(self, d): pass get_orthogonal_slope = lambda y: (-1) / y def calculate_normal(edge): middle_point = Point((edge[0].x + edge[1].x)/ 2, (edge[0].y + edge[1].y) / 2) slope = (edge[1].y - edge[0].y) / (edge[1].x - edge[0].x) m = get_orthogonal_slope(slope) n = middle_point.x * get_orthogonal_slope(slope) + middle_point.y return Line(m, n) def calculate_lines_intersection(line1, line2): x = (line2.n - line1.n) / (line1.m - line2.m) y = line1.m * x + line1.n return Point(x, y) def calculate_free_confgurations(robot, room): normals = room.calculate_normals() for edge in robot.edges: normal = calculate_normal(edge) p = robot.find_farthest_point(normal) intersection_line = if __name__ == "__main__": triangle_p1 = Point(4, 2) triangle_p2 = Point(10, 2) triangle_p3 = Point(7, 6) rectangle_p1 = Point(0, 1) rectangle_p2 = Point(12, 1) rectangle_p3 = Point(12, 8) rectangle_p4 = Point(0, 8)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question