def SetupQuickSort(origin, size, numberList, BarsList): for x in range(size): n = numberList[x] bar = Bars(gl.Point(origin.x, origin.y), MAX_WIDTH, -n / MAX_HEIGHT * 100, RandRBG(), n) BarsList.append(bar) origin.x += MAX_WIDTH + MAX_DISTANCE pivotState.append(0) # not pivot print("unsorted-->", numberList) Quicksort(numberList, 0, len(numberList) - 1) print("sorted-->", numberList)
def DrawRect(canvas, pos, w, h, color, state=0): rect = gl.Rectangle(pos, gl.Point(pos.x + w, pos.y + h)) rect.setFill(color) rect.setWidth(3) if state == 0: rect.setOutline('white') elif state == 1: #pivot color rect.setOutline('red') else: #borders color rect.setOutline('green') rect.draw(canvas) return rect
def FinishMenu(canvas, num, bars): txt = PrintMessage(canvas, gl.Point(200, SCREEN_HEIGTH - 100), "Desea Repetir?") ans = DrawRect(canvas, gl.Point(450, SCREEN_HEIGTH - 100), 50, 30, "red") ans2 = DrawRect(canvas, gl.Point(550, SCREEN_HEIGTH - 100), 50, 30, "green") op = True while op: mouse = canvas.getMouse() if (ans.getP1().x < mouse.getX() < ans.getP2().x) and ( mouse.getY() > ans.getP1().y and mouse.getY() < ans.getP2().y): print("saliendo...") op = False if (ans2.getP1().x < mouse.getX() < ans2.getP2().x) and (mouse.getY() > ans2.getP1().y and mouse.getY() < ans2.getP2().y): print("repitiendo...") txt.undraw() ans.undraw() ans2.undraw() ClearLists(num, bars) canvas.update() return True return False
def DrawMainMenu(canvas, list, mode): canvas.setBackground(DARK_BLUE) PrintMessage(canvas, gl.Point(canvas.width / 2, 50), "Algoritmo Quicksort") menuOp = PrintMessage(canvas, gl.Point(200, 100), "Seleccione cantidad de elementos") menuOp2 = PrintMessage(canvas, gl.Point(700, 100), "O inserte los digitos: ") in_SizeBox = gl.Entry(gl.Point(400, 100), 4) in_SizeBox.draw(canvas) in_DigitBox = gl.Entry(gl.Point(900, 100), 10) in_DigitBox.draw(canvas) txt = PrintMessage(canvas, gl.Point(200, 120), "", "red") ans = DrawRect(canvas, gl.Point(450, 90), 50, 30, "red") ans2 = DrawRect(canvas, gl.Point(SCREEN_WIDTH - 100, 90), 50, 30, "blue") op = True while op: mouse = canvas.getMouse() if (ans.getP1().x < mouse.getX() < ans.getP2().x) and ( mouse.getY() > ans.getP1().y and mouse.getY() < ans.getP2().y): SIZE = int(in_SizeBox.getText()) print("size: " + str(SIZE)) mode.state = 1 op = False if (ans2.getP1().x < mouse.getX() < ans2.getP2().x) and (mouse.getY() > ans2.getP1().y and mouse.getY() < ans2.getP2().y): numText = in_DigitBox.getText() getNumbers(numText, list) SIZE = len(list) print("size: " + str(SIZE)) print("array: " + str(list)) mode.state = 2 op = False menuOp.undraw() menuOp2.undraw() in_SizeBox.undraw() in_DigitBox.undraw() ans.undraw() ans2.undraw() txt.undraw() return SIZE # user selected
def main(): #crea una ventana win = gl.GraphWin("Quicksort Visualizer", SCREEN_WIDTH, SCREEN_HEIGTH, autoflush=True) # App code starts here op = True option = STATES(0) while op: #loop principal origin = gl.Point(50, win.height / 2 + 100) numberList = [] # guarda los numeros para el sorteo BarsList = [] # guarda las barras SIZE = DrawMainMenu(win, numberList, option) #obtiene el tamaño del arreglo # Prepara los rectangulos a dibujar if option.state == 1: #si es aleatorio, se llama la funcion RandQuicksort SetupRandQuickSort(origin, SIZE, numberList, BarsList) elif option.state == 2: # si el usuario interviene, se llama la funcion SetQuicksort SetupQuickSort(origin, SIZE, numberList, BarsList) # Crea la animacion DrawQuickSortAnimation(win, BarsList, SIZE, 0.015) print("value (x, y)") #DEBUG # Dibuja el resultado final en pantalla for i in range(SIZE): BarsList[i].Draw(win) #dibuja barra a barra #DEBUG print(str(BarsList[i].value), "(", str(BarsList[i].origin.x), ", ", str(BarsList[i].origin.y), ")") op = FinishMenu(win, numberList, BarsList) #llama al menu final option.state = 0 #vuelve estado por defecto antes de iterar win.getMouse() # Pausa la ventana para ver el resultado win.close() #Cierra el programa
def Draw(self, canvas, state=0): self.obj = DrawRect(canvas, self.origin, self.w, self.h, self.color, state) self.TextVal = PrintMessage( canvas, gl.Point(self.origin.x + 10, self.origin.y + 10), self.value, 'white', state)