def events(self): for event in pg.event.get(): if event.type == pg.QUIT: self.halt() if event.type == pg.MOUSEBUTTONDOWN: mx, my = pg.mouse.get_pos() for b in self.buttons: if b._rect.collidepoint((mx, my)): b.onClick() self.startVertexSelectingMode(Pos(mx, my)) if event.type == pg.KEYDOWN: if event.key == pg.K_ESCAPE: self.halt() elif (event.key == pg.K_LCTRL) or \ (event.type == pg.MOUSEBUTTONUP): self.stopVertexSelectingMode() elif event.key == pg.K_SPACE: self.startCrazySpanningMode() elif event.key == pg.K_EQUALS: self.scaleFactorUp() elif event.key == pg.K_MINUS: self.scaleFactorDown() elif event.key == pg.K_v: self.toggleVerticesSetupMode() elif event.key == pg.K_e: self.toggleEdgesSetupMode()
def calculateBezierPoints(t: float, controlPoints: List[Pos]) -> Pos: if not 0 <= t <= 1: raise Exception("t: float ∈ [0,1]") n: int = len(controlPoints) - 1 y: float = sum((pow((1 - t), n - i) * pow(t, i) * comb(n, i) * p.y) for i, p in enumerate(controlPoints)) x: float = sum((pow((1 - t), n - i) * pow(t, i) * comb(n, i) * p.x) for i, p in enumerate(controlPoints)) return Pos(int(x), int(y))
def generateVerticesCanFitIn(self, width, height, dimentsManger) -> GraphHolder: c, r = ((width // (dimentsManger.VerticesDiments.radius * 2)) // 2), ((height // (dimentsManger.VerticesDiments.radius * 2)) // 2) self.vertices.extend([ Vertex(-i, Pos(((i % c) * (width // c)), ((i // c) * (height // r)))) for i in range(c * r) ]) return self.exportGraphHolder()
def midPoint(self) -> Pos: return Pos(*getMidPointInLine(self.start.pos, self.end.pos))
def createVertex(VertexName: Any, pos: Pos = None) -> Vertex: if pos is None: pos = Pos(*random.choice( list(itertools.permutations( [-3, 0, 3, 1], 2)))) return Vertex(VertexName, pos)
def showEdgeAngle(edges: List[Edge]): posY = 0 for edge in edges: start = v.graphManger.verticesManger.byName(edge.start) end = v.graphManger.verticesManger.byName(edge.end) angle = getAngeBetweenVertices(start, end) text = f"{edge.start.name:^3} {angle* (180 / math.pi)} {edge.end.name:^3}" keyImage = font.render(text, True, Colors.MainColors.onSurfaceColor) posX = v.height - font.size(text)[0] v.screen.blit(keyImage, [posX - 20, posY]) posY += font.size(text)[1] circlePos = Pos(0, 0) v.graphManger.edgesManger.byId("(V0, V1)").isCarve = True while True: v.events() # showIntersectionCircle(v.graphManger.verticesManger.vertices) T = 0.0 pointsC0 = [] pointsC1 = [] while T <= 1.0: posC0: Pos = calculateBezierPoints( T, list(map(lambda x: x.pos, verticesC0))) posC1: Pos = calculateBezierPoints( T, list(map(lambda x: x.pos, verticesC1))) pointsC0.append(tuple(posC0)) pointsC1.append(tuple(posC1)) T += .01
graphGenerator = GraphGenerator(w, h) graphGenerator.verticesManger.addVertices(vertices) v.graphManger.setupFromGraphHolder(graphGenerator.exportGraphHolder()) v.startMouseThread() animationTime = 100 animationEnd = 100 def RUN_BUTTON_ACTION(): global animationTime animationTime = 0 bW, bH = 100, 60 RUN_BUTTON = Button("run", Pos(((w // 2) - (bW // 2)), (h // 4) * 3)) RUN_BUTTON.setOnClick(RUN_BUTTON_ACTION) v.buttons.append(RUN_BUTTON) circlePos = Pos(0, 0) while True: v.events() # showIntersectionCircle(v.graphManger.verticesManger.vertices) T = 0.0 points = [] while T <= 1.0: pos: Pos = calculateBezierPoints(T, [v.graphManger.verticesManger.byName(0).pos, v.graphManger.verticesManger.byName(1).pos, v.graphManger.verticesManger.byName(2).pos, v.graphManger.verticesManger.byName(3).pos,
def __init__(self, name: Any, pos: tuple = None): self.name = name self.pos = pos if pos is None else Pos(*pos)