def draw(self, support_convex: bool = False): path = QtGui.QPainterPath() if self.getDegree() == 1: point = list(self.getVertices())[0].pos() start_angle = -pi / 4. c1 = point + QtCore.QPointF(self.real_point_gap * cos(start_angle), self.real_point_gap * sin(start_angle)) start_angle -= pi / 2. c2 = point + QtCore.QPointF(self.real_point_gap * cos(start_angle), self.real_point_gap * sin(start_angle)) help_point = point + QtCore.QPointF(0, -self.real_point_gap) path.moveTo(point) path.quadTo(c1, help_point) path.quadTo(c2, point) elif self.getDegree() == 2: vertices = list(self.getVertices()) if self.multiplicity > 1: if vertices[0].pos().y() < vertices[1].pos().y(): p_start = vertices[0].pos() p_finish = vertices[1].pos() else: p_start = vertices[1].pos() p_finish = vertices[0].pos() distance = CanvasVertex.distance_pos(p_start, p_finish) base_angle = CanvasVertex.polar_angle_pos(p_start, p_finish) base_angle += pi / 2. * (-1)**self.multiplicity c = (p_start + p_finish) / 2. + QtCore.QPointF( distance / 2. * self.multiplicity / 2. * cos(base_angle), distance / 2. * self.multiplicity / 2. * sin(base_angle)) path.moveTo(p_start) path.quadTo(c, p_finish) else: path.moveTo(vertices[0].pos()) path.lineTo(vertices[1].pos()) else: if not self.support_convex: self.grahamConvex() path.moveTo(self.arc_points[0]) for center, arc_final, next in zip(self.arc_points[1::3], self.arc_points[2::3], self.arc_points[3::3]): path.quadTo(center, arc_final) path.lineTo(next) path.quadTo(self.arc_points[-2], self.arc_points[-1]) path.closeSubpath() if support_convex: path.moveTo(self.support_convex[0]) for vertex in self.support_convex[1:]: path.lineTo(vertex) path.closeSubpath() self.setPath(path) return path
def inflateConvex(self, convex: list, gap: float) -> list: convex = [(convex[i - 1], convex[i], convex[(i + 1) % len(convex)]) for i in range(len(convex))] result_convex = [] for prev, vertex, next in convex: angles = [] for v in (prev, next): a = CanvasVertex.polar_angle_pos(vertex, v) # a = vertex.polar_angle(v) angles.append(a) angle = sum(angles) / 2. # TODO redo # new_vertex = CanvasVertex(id = vertex.getId() + '\'') new_vertex = QtCore.QPointF( vertex + QtCore.QPointF(gap * cos(angle), gap * sin(angle))) if CanvasVertex.vector_product_pos(prev, vertex, new_vertex) < 0.: # prev.vector_product(vertex, new_vertex) < 0.: angle += pi new_vertex = QtCore.QPointF( vertex + QtCore.QPointF(gap * cos(angle), gap * sin(angle))) result_convex.append(new_vertex) return result_convex
def __init__(self, *vertices, **kwargs): # TODO redo QtWidgets.QGraphicsPathItem.__init__(self) if 'other' in kwargs: edge = kwargs.pop('other') if type(edge) == HyperEdge: vertices = {CanvasVertex(other=v) for v in edge.getVertices()} HyperEdge.__init__(self, *vertices) else: HyperEdge.__init__(self, other=edge) else: vertices = {CanvasVertex(other=x) for x in vertices} HyperEdge.__init__(self, *vertices) # HyperEdge.__init__(self, *[CanvasVertex(vertex, self) for vertex in vertices], **kwargs) self.real_point_gap = 50. self.default_pen = QtGui.QPen() self.setPen(self.default_pen) self.hovered_pen = QtGui.QPen() self.hovered_pen.setColor(QtCore.Qt.red) self.hovered_brush = QtGui.QBrush() self.hovered_brush.setColor(QtCore.Qt.red) self.default_brush = QtGui.QBrush() # self.score = 0 self.setBrush(self.hovered_brush) self.support_convex = [] self.setAcceptHoverEvents(True) self.multiplicity = 1
def drawVertex(self, vertex: CanvasVertex): if vertex in filter(lambda item: isinstance(item, CanvasVertex), self.items()): return while True: vertex.setX(random.randint(0, 100)) vertex.setY(random.randint(0, 100)) if not self.collidingItems(vertex): break self.addItem(vertex)
def __init__(self, hg: HyperGraph): self.hypergraph = hg super().__init__() self.vertices = [CanvasVertex(x) for x in hg.getVertices()] for vertex in self.vertices: self.addItem(vertex) # randomising vertices positions vertex.setPos( QtCore.QPointF(random.uniform(-100., 100.), random.uniform(-100., 100.))) self.edges = [CanvasHyperEdge(x, self.vertices) for x in hg.getEdges()] # checking crossing variants self.crossing_matrix = [[False] * len(self.edges) for _ in range(len(self.edges))] for i, edge in enumerate(self.edges[:-1]): for j, other in enumerate(self.edges[i + 1:], i + 1): self.crossing_matrix[i][j] = edge.hyperedge.getCrossingVariant( other.hyperedge) # matrix reflection for i in range(len(self.crossing_matrix)): for j in range(i): self.crossing_matrix[i][j] = self.crossing_matrix[j][i] # reflection test # for i in range(len(self.crossing_matrix)): # for j in range(len(self.crossing_matrix)): # assert self.crossing_matrix[i][j] == self.crossing_matrix[j][i], "Bad reflection" for edge in self.edges: if edge.hyperedge.isHyperedge(): edge.grahamConvex()
def addVertices(self, vertices: set): vertices = {CanvasVertex(x) for x in vertices} super().addVertices(vertices)
def addVertex(self, vertex: CanvasVertex): vertex = CanvasVertex(vertex) super().addVertex(vertex)