def insertRear(self, val): # Fix to self.rear starting at 1 if self.nItems == 0: self.rear = 0 self.front = self.rear # circular # create new cell and cell value display objects # Start drawing new one at rear cell = canvas.create_rectangle(ARRAY_X0+CELL_SIZE*self.rear, ARRAY_Y0, \ ARRAY_X0+CELL_SIZE*(self.rear+1), ARRAY_Y0 + CELL_SIZE, \ fill=Queue.colors[Queue.nextColor], outline='') cell_val = canvas.create_text(ARRAY_X0+CELL_SIZE*self.rear + (CELL_SIZE / 2), \ ARRAY_Y0 + (CELL_SIZE / 2), text=val, font=('Helvetica', '20')) #insert the item self.list[self.rear] = drawable(val, Queue.colors[Queue.nextColor], cell, cell_val) self.nItems += 1 # increment nextColor Queue.nextColor = (Queue.nextColor + 1) % len(Queue.colors) self.onOffButtons() # update window window.update() #if there's a space to insert into elif self.nItems != self.size: #deal with wraparound if self.rear == self.size - 1: self.rear = -1 #increment rear self.rear += 1 # create new cell and cell value display objects # Start drawing new one at rear cell = canvas.create_rectangle(ARRAY_X0+CELL_SIZE*self.rear, ARRAY_Y0, \ ARRAY_X0+CELL_SIZE*(self.rear+1), ARRAY_Y0 + CELL_SIZE, \ fill=Queue.colors[Queue.nextColor], outline='') cell_val = canvas.create_text(ARRAY_X0+CELL_SIZE*self.rear + (CELL_SIZE / 2), \ ARRAY_Y0 + (CELL_SIZE / 2), text=val, font=('Helvetica', '20')) #insert the item self.list[self.rear] = drawable(val, Queue.colors[Queue.nextColor], cell, cell_val) self.nItems += 1 # increment nextColor Queue.nextColor = (Queue.nextColor + 1) % len(Queue.colors) self.onOffButtons() # update window window.update()
def assignToTemp(self, index, varName="temp"): y0 = canvas.coords(self.list[index].display_shape)[1] posCell = canvas.coords(self.list[index].display_shape) posCellVal = canvas.coords(self.list[index].display_val) yspeed = -5 shape = canvas.create_rectangle(posCell[0], posCell[1], posCell[2], posCell[3], fill=self.list[index][1]) val = canvas.create_text(posCellVal[0], posCellVal[1], text=str(self.list[index][0]), font=('Helvetica', '20')) while canvas.coords(shape)[1] > (y0 - CELL_SIZE - 15): canvas.move(shape, 0, yspeed) canvas.move(val, 0, yspeed) window.update() time.sleep(self.speed(0.01)) text = canvas.create_text(posCell[0] + (CELL_SIZE / 2), y0 - CELL_SIZE - 30, text=varName, font=('Helvetica', '20')) temp = drawable(self.list[index][0], self.list[index][1], shape, val) window.update() return temp, text
def insert(self, val): self.cleanUp() # draw an index pointing to the last cell indexDisplay = self.createIndex(len(self.list)) self.cleanup |= set(indexDisplay) # create new cell and cell value display objects toPositions = (self.cellCoords(len(self.list)), self.cellCenter(len(self.list))) # Animate arrival of new value from operations panel area canvasDimensions = self.widgetDimensions(self.canvas) startPosition = [canvasDimensions[0] // 2, canvasDimensions[1]] * 2 startPosition = add_vector(startPosition, (0, 0, CELL_SIZE, CELL_SIZE)) cellPair = self.createCellValue(startPosition, val) self.moveItemsTo(cellPair, toPositions, steps=CELL_SIZE, sleepTime=0.01) # add a new Drawable with the new value, color, and display objects self.list.append( drawable(val, self.canvas.itemconfigure(cellPair[0], 'fill'), *cellPair)) # update window self.window.update() # advance index for next insert self.moveItemsBy(indexDisplay, (CELL_SIZE, 0))
def assignToTemp(self, index, varName="temp", existing=None): """Assign indexed cell to a temporary variable named varName. Animate value moving to the temporary variable above the array. Return a drawable for the new temporary value and a text item for its name. The existing name item can be passed to avoid creating a new one and for moving the value to that location """ fromDraw = self.list[index] fromCell = fromDraw.display_shape fromCellVal = fromDraw.display_val posCell = self.canvas.coords(fromCell) posCellVal = self.canvas.coords(fromCellVal) shape = self.copyCanvasItem(fromCell) val = self.copyCanvasItem(fromCellVal) if existing: tempPos = self.canvas.coords(existing) templabel = existing else: posLabel = subtract_vector(posCellVal, (0, self.CELL_SIZE * 2)) templabel = self.canvas.create_text( *posLabel, text=varName, font=self.VARIABLE_FONT, fill=self.VARIABLE_COLOR) delta = (tempPos[0] - posCellVal[0] if existing else 0, - self.CELL_SIZE * 4 // 3) self.moveItemsBy((shape, val), delta, sleepTime=0.02) return drawable(fromDraw.val, fromDraw.color, shape, val), templabel
def insert(self, val): callEnviron = self.createCallEnvironment() self.startAnimations() # draw an index pointing to the last cell indexDisplay = self.createIndex(len(self.list), "nItems") callEnviron |= set(indexDisplay) # create new cell and cell value display objects toPositions = (self.cellCoords(len(self.list)), self.cellCenter(len(self.list))) # Animate arrival of new value from operations panel area canvasDimensions = self.widgetDimensions(self.canvas) startPosition = add_vector( [canvasDimensions[0]//2 - self.CELL_SIZE, canvasDimensions[1]] * 2, (0, 0) + (self.CELL_SIZE - self.CELL_BORDER,) * 2) cellPair = self.createCellValue(startPosition, val) callEnviron |= set(cellPair) self.moveItemsTo(cellPair, toPositions, steps=self.CELL_SIZE, sleepTime=0.01) # add a new Drawable with the new value, color, and display objects self.list.append(drawable( val, self.canvas.itemconfigure(cellPair[0], 'fill'), *cellPair)) callEnviron ^= set(cellPair) # Remove new cell from temp call environ # advance index for next insert self.moveItemsBy(indexDisplay, (self.CELL_SIZE, 0)) self.cleanUp(callEnviron)
def __init__(self, size=10, title="Simple Sorting", **kwargs): super().__init__(title=title, **kwargs) self.size = size self.title = title self.list = [] # Internal array of drawable cell values self.buttons = self.makeButtons() for i in range(size): self.list.append(drawable(random.randrange(30))) self.display()
def __init__(self, size=10, title="Array", **kwargs): super().__init__(title=title, **kwargs) self.size = size self.title = title self.list = [] self.buttons = self.makeButtons() # Fill in initial array values with random integers # The display items representing these array cells are created later for i in range(size - 1): self.list.append(drawable(random.randrange(90))) self.display()
def insert(self, val): self.startAnimations() callEnviron = self.createCallEnvironment(self.insertCode.strip(), self.insertCodeSnippets) # If array needs to grow, add cells: while self.size < len(self.list) + 1: self.size += 1 self.createArrayCell(len(self.list)) # draw an index pointing to the last cell self.highlightCodeTags('item_assignment', callEnviron) indexDisplay = self.createIndex(len(self.list), 'nItems') callEnviron |= set(indexDisplay) # create new cell and cell value display objects toPositions = (self.cellCoords(len(self.list)), self.cellCenter(len(self.list))) # Animate arrival of new value from operations panel area canvasDimensions = self.widgetDimensions(self.canvas) startPosition = add_vector( [canvasDimensions[0] // 2 - self.CELL_SIZE, canvasDimensions[1]] * 2, (0, 0) + (self.CELL_SIZE - self.CELL_BORDER, ) * 2) cellPair = self.createCellValue(startPosition, val) callEnviron |= set(cellPair) self.moveItemsTo(cellPair, toPositions, steps=self.CELL_SIZE, sleepTime=0.01) # add a new Drawable with the new value, color, and display objects self.list.append( drawable(val, self.canvas.itemconfigure(cellPair[0], 'fill'), *cellPair)) callEnviron ^= set(cellPair) # Remove new cell from temp call environ # advance index for next insert self.highlightCodeTags('nitem_increment', callEnviron) self.moveItemsBy(indexDisplay, (self.CELL_SIZE, 0), sleepTime=0.01) self.highlightCodeTags([], callEnviron) self.cleanUp(callEnviron)
def display(self): canvas.delete("all") xpos = 10 ypos = BF_Y0 for n in range(len(self.__displayList)): # create display objects for the associated drawables cell = canvas.create_rectangle(xpos, ypos, xpos + CELL_SIZE, ypos + CELL_SIZE) cell_val = canvas.create_text(xpos + (CELL_SIZE / 2), ypos + (CELL_SIZE / 2), text=self.__bv[n], font=('Helvetica', '20')) self.__displayList[n] = drawable(self.__bv[n], None, cell, cell_val) # increment xpos xpos += CELL_SIZE window.update()
def append(self, val): # create new cell and cell value display objects cell = canvas.create_rectangle(ARRAY_X0 + CELL_SIZE * len(self.list), ARRAY_Y0, ARRAY_X0 + CELL_SIZE * (len(self.list) + 1), ARRAY_Y0 + CELL_SIZE, fill=Array.colors[Array.nextColor]) cell_val = canvas.create_text(ARRAY_X0 + CELL_SIZE * len(self.list) + (CELL_SIZE / 2), ARRAY_Y0 + (CELL_SIZE / 2), text=val, font=('Helvetica', '20')) # add a new Element to the list with the new value, color, and display objects self.list.append( drawable(val, Array.colors[Array.nextColor], cell, cell_val)) # increment nextColor Array.nextColor = (Array.nextColor + 1) % len(Array.colors) # update window window.update()