def main(): Main.graph = Graph([("A", Point(560, 50)), ("B", Point(240, 150)), ("C", Point(800, 240)), ("D", Point(500, 240)), ("E", Point(80, 240)), ("F", Point(480, 320)), ("G", Point(900, 600)), ("H", Point(100, 480)), ("I", Point(640, 560)), ("J", Point(160, 640))]) Main.graph.add_edge("A", "B") Main.graph.add_edge("A", "C") Main.graph.add_edge("A", "D") Main.graph.add_edge("B", "D") Main.graph.add_edge("C", "G") Main.graph.add_edge("D", "E") Main.graph.add_edge("D", "F") Main.graph.add_edge("D", "G") Main.graph.add_edge("G", "I") Main.graph.add_edge("B", "E") Main.graph.add_edge("E", "H") Main.graph.add_edge("H", "J") Main.graph.add_edge("D", "J") Main.graph.add_edge("F", "J") Main.graph.add_edge("F", "I") Main.graph.add_edge("I", "J") Main.stripes_method = StripesMethod(Main.graph) Main.win.setCoords(0, 0, Main.width, Main.height) Main.graph.draw(Main.win) Main.win.bind('<Button-1>', Main.on_right_click) Main.win.mainloop()
def __gt__(self, other): if self.source.point != other.source.point and self.to.point != other.to.point: line_level = max(self.source.point.y, other.source.point.y) line_p1 = Point(0, line_level) line_p2 = Point(1, line_level) x_self, _ = utils.intersect(self.source.point, self.to.point, line_p1, line_p2) x_other, _ = utils.intersect(other.source.point, other.to.point, line_p1, line_p2) if x_self is None: x_self = self.source.point.x if x_other is None: x_other = self.to.point.x return x_self > x_other if self.source.point == other.source.point: origin = self.source.point self_angle = utils.polar_angle(origin, self.to.point) other_angle = utils.polar_angle(origin, other.to.point) return self_angle < other_angle else: origin = self.to.point self_angle = utils.polar_angle(origin, self.source.point) other_angle = utils.polar_angle(origin, other.source.point) return self_angle > other_angle
def on_mouse_click(event): point = Point(event.x, event.y) if any(point.x == p.x and point.y == p.y for p in Main.points): return Main.points.append(point) point.draw(Main.win) if len(Main.points) > 2: if Main.hull is not None: Main.hull.undraw(Main.win) Main.hull = DivideAndConquerAlgorithm.execute(Main.points, Main.win) Main.hull.draw(Main.win, "red")
def mirror_segments(S: Sequence[Segment], mirror_s: Segment) -> List[Segment]: """Produces a set of segments mirrored along a given line.""" mirrored_S: Deque[Segment] a, b, c = abc_line(mirror_s) mirrored_S = deque() for s in S: Dp = a * s.p.x + b * s.p.y + c Dq = a * s.q.x + b * s.q.y + c mirrored_S.append(Segment( Point(s.p.x - 2 * a * Dp, s.p.y - 2 * b * Dp, name=s.p.name), Point(s.q.x - 2 * a * Dq, s.q.y - 2 * b * Dq, name=s.p.name))) return list(mirrored_S)
def on_mouse_click(event): point = Point(event.x, event.y) if any(point.x == p.x and point.y == p.y for p in Main.points): return Main.points.append(point) point.draw(Main.win) if len(Main.points) > 2: if utils.are_collinear(Main.points): return if Main.hull is None: Main.hull = PreparataHull(Main.points) else: Main.hull.undraw(Main.win) Main.hull.add_point(point) Main.hull.draw(Main.win, "red")
def centroid(points): x_average = 0 y_average = 0 for p in points: x_average += p.x y_average += p.y x_average /= len(points) y_average /= len(points) return Point(x_average, y_average)
def read_points(self): possible_divs = [':', ';', ',', ' '] text = self.text_input.toPlainText() if text.strip() == '': return lines = [line.strip() for line in text.splitlines()] for char in possible_divs: if char in lines[0]: self.div = char break if not hasattr(self, 'div'): self.error( 'Πρόβλημα', 'Δεν βρέθηκε κάποιο κατάλληλο διαχωριστικό σε κάθε γραμμή...') return False linetype = LINETYPE[ self.from_linetype_combo.currentIndex()]['form'].split(',') txtpoints = [] for line in lines: items = [item.strip() for item in line.split(self.div)] if len(items) != len(linetype): self.error( 'Πρόβλημα', 'Λάθος μορφή, επιλέξτε την σωστή και προσπαθήστε ξανά...') return False point = dict() for idx, t in enumerate(linetype): item = items[idx] if t in ['x', 'y', 'z']: try: point[t] = float(item.replace(',', '.')) except: self.error('Πρόβλημα', 'Κάποιο από τα x, y, z δεν είναι αριθμός') else: point[t] = item txtpoints.append(point) self.points = [] for p in txtpoints: tmp = Point(p['x'], p['y']) if 'z' in p: tmp.z = p['z'] if 'name' in p: tmp.name = p['name'] self.points.append(tmp) source_system = self.from_combo.currentText()
def mirror_points(P: Sequence[Point], mirror_s: Segment) -> List[Point]: """Produces a set of points mirrored along a given line.""" mirrored_P: Deque[Point] a, b, c = abc_line(mirror_s) mirrored_P = deque() for p in P: D = a * p.x + b * p.y + c mirrored_P.append(Point(p.x - 2 * a * D, p.y - 2 * b * D, name=p.name)) return list(mirrored_P)
def read_points(self): possible_divs = [':', ';', ',', ' '] text = self.text_input.toPlainText() if text.strip() == '': return lines = [line.strip() for line in text.splitlines()] for char in possible_divs: if char in lines[0]: self.div = char break if not hasattr(self, 'div'): self.error('Πρόβλημα', 'Δεν βρέθηκε κάποιο κατάλληλο διαχωριστικό σε κάθε γραμμή...') return False linetype = LINETYPE[self.from_linetype_combo.currentIndex()]['form'].split(',') txtpoints = [] for line in lines: items = [item.strip() for item in line.split(self.div)] if len(items) != len(linetype): self.error('Πρόβλημα', 'Λάθος μορφή, επιλέξτε την σωστή και προσπαθήστε ξανά...') return False point = dict() for idx, t in enumerate(linetype): item = items[idx] if t in ['x', 'y', 'z']: try: point[t] = float(item.replace(',', '.')) except: self.error('Πρόβλημα', 'Κάποιο από τα x, y, z δεν είναι αριθμός') else: point[t] = item txtpoints.append(point) self.points = [] for p in txtpoints: tmp = Point(p['x'], p['y']) if 'z' in p: tmp.z = p['z'] if 'name' in p: tmp.name = p['name'] self.points.append(tmp) source_system = self.from_combo.currentText()
def on_right_click(event): if Main.graph is None: return if Main.last_point is not None: Main.last_point.undraw() for edge in Main.last_edges: edge.undraw() edge.draw(Main.win, "black") for vertex in [edge.source, edge.to]: vertex.undraw() vertex.draw(Main.win, "black") for line in Main.last_lines: line.undraw() Main.last_point = Point(event.x, Main.height - event.y) Main.last_point.draw(Main.win) result = Main.stripes_method.execute(Main.last_point) Main.last_edges = [] Main.last_lines = [] if result[0] is not None: for edge in result[0]: if edge is not None: edge.undraw() edge.draw(Main.win, "red") for vertex in [edge.source, edge.to]: vertex.undraw() vertex.draw(Main.win, "red") Main.last_edges.append(edge) for y in result[1]: if y is not None: line = graphics.Line(graphics.Point(0, y), graphics.Point(Main.width, y)) line.setFill("blue") line.draw(Main.win) Main.last_lines.append(line)
def add_point(self, x, y): p = Point(x, y, 0) self.objects.append({'type': 'point', 'data': p}) self.redraw_canvas()
s = temp_seg # continue with new segment popped = True else: break if popped: # removed some previous points? lower_CH.append(q) # then also add lower_CH_segs.append(s) return list(lower_CH), list(lower_CH_segs) # Might fail to include upper left and lower left points for large point counts (>10000) POINT_CNT = 20000 points = [ Point(random.randint(0, 100), random.random()) for i in range(POINT_CNT) ] CH = graham_scan(points) print(CH) print(len(CH)) # draw result if not DEBUG: poly = Polygon(CH) plt.axes() plt.scatter([p.x for p in points], [p.y for p in points]) for s in poly.segments: plt.gca().add_line(s.line()) plt.show()
def from_list(cls, array: np.ndarray): return FaceLandmarks(right_eye=Point(x=array[0], y=array[1]), left_eye=Point(x=array[2], y=array[3]), nose=Point(x=array[4], y=array[5]), right_mouth_corner=Point(x=array[6], y=array[7]), left_mouth_corner=Point(x=array[8], y=array[9]))