def enterFc(self, ctx): operator = "GOSUB" left_operand = functionId right_operand = "" result = "" global qCount qCount += 1 quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad) funcReturnType = functionDirectory.dictionary[ functionId].funcReturnType if funcReturnType != "void": operator = "=" left_operand = functionId result = "" if funcReturnType == 'int': global tempVarIntAddr right_operand = "ti" + str(tempVarIntAddr + 1) tempVariableTable.insertTempVariable(right_operand, 'int', tempVarIntAddr) tempVarIntAddr += 1 else: #bool global tempVarBoolAddr right_operand = "tb" + str(tempVarBoolAddr + 1) tempVariableTable.insertTempVariable(right_operand, 'bool', tempVarBoolAddr) tempVarBoolAddr += 1 qCount += 1 quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad) stackOperands.append(right_operand)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.set_minimum_size(400, 300) glClearColor(0.2, 0.3, 0.2, 1.0) glEnable(GL_DEPTH_TEST) self.quad = Quad()
class Simulation: def __init__(self, width=640, height=480): pygame.init() self.screen = pygame.display.set_mode((width, height)) self.clock = pygame.time.Clock() self.vertices = np.array([ [-1, 1, -1], [1, 1, -1], [1, -1, -1], [-1, -1, -1], [-1, 1, 1], [1, 1, 1], [1, -1, 1], [-1, -1, 1] ], dtype='float32') self.faces = [(0, 1, 2, 3), (1, 5, 6, 2), (5, 4, 7, 6), (4, 0, 3, 7), (0, 4, 5, 1), (3, 2, 6, 7)] self.colors = [(255, 0, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 255, 255), (255, 255, 0)] self.angle = 0 self.quad = Quad() def run(self): t = threading.Thread(target=runQuad, args=(self.quad,)) t.start() while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: self.quad.quit() pygame.quit() sys.exit() self.clock.tick(50) self.screen.fill((0, 32, 0)) position, orientation = self.quad.params rotation = quaternion.as_rotation_matrix(orientation) t = self.vertices.dot(rotation) + position factors = 256 / (4 + t[:,2]) # fov / viewer_distance + z t[:, 0] = t[:, 0] * factors + self.screen.get_width() / 2 t[:, 1] = -t[:, 1] * factors + self.screen.get_height() / 2 avg_z = [] for i, f in enumerate(self.faces): z = (t[f[0],2] + t[f[1],2] + t[f[2],2] + t[f[3],2]) / 4.0 avg_z.append([i, z]) for tmp in sorted(avg_z, key=itemgetter(1), reverse=True): face_index = tmp[0] f = self.faces[face_index] pointlist = [(t[f[0],0], t[f[0],1]), (t[f[1],0], t[f[1],1]), (t[f[1],0], t[f[1],1]), (t[f[2],0], t[f[2],1]), (t[f[2],0], t[f[2],1]), (t[f[3],0], t[f[3],1]), (t[f[3],0], t[f[3],1]), (t[f[0],0], t[f[0],1])] pygame.draw.polygon(self.screen, self.colors[face_index], pointlist) self.angle += 1 pygame.display.flip()
def main(): intA = eval(input("Enter integer: ")) intB = eval(input("Enter integer: ")) intC = eval(input("Enter integer: ")) quad1 = Quad(intA, intB, intC) print("Discriminant is: ", quad1.getDiscriminant()) print("Root 1 is: ", quad1.getRoot1()) print("Root 2 is: ", quad1.getRoot2())
def exitFunction(self, ctx): global qCount global tempVarIntAddr global tempVarBoolAddr global tempVarCharAddr global parameterIntAddr global parameterBoolAddr global parameterCharAddr if functionName == 'main': operator = "END" else: operator = "ENDPROC" left_operand = "" right_operand = "" result = "" qCount += 1 quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad) tempVarIntAddr = 0 tempVarBoolAddr = 0 tempVarCharAddr = 0 parameterIntAddr = 0 parameterBoolAddr = 0 parameterCharAddr = 0 functionDirectory.dictionary[ functionName].parameterTable = parameterTable functionDirectory.dictionary[ functionName].tempVariableTable = tempVariableTable
def exitE(self, ctx): if len(stackOperators) > 0: if stackOperators[-1] == '<' or stackOperators[ -1] == '>' or stackOperators[-1] == '==' or stackOperators[ -1] == '!=': right_operand = stackOperands.pop() left_operand = stackOperands.pop() right_type = self.checkType(right_operand) left_type = self.checkType(left_operand) operator = stackOperators.pop() result_type = relationalOperators(operator, right_type, left_type) if result_type == "bool": global tempVarBoolAddr result = "tb" + str(tempVarBoolAddr + 1) tempVariableTable.insertTempVariable( result, 'bool', tempVarBoolAddr) tempVarBoolAddr += 1 global qCount qCount += 1 quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad) stackOperands.append(result) else: raise Exception("Type mismatch")
def exitFactor(self, ctx): operator = str(ctx.RPARENTHESES()) if operator != "None": stackOperators.pop() if len(stackOperators) > 0: if stackOperators[-1] == '*' or stackOperators[-1] == '/': right_operand = stackOperands.pop() left_operand = stackOperands.pop() right_type = self.checkType(right_operand) left_type = self.checkType(left_operand) operator = stackOperators.pop() result_type = arithmeticOperators(operator, right_type, left_type) if result_type == "int": global tempVarIntAddr result = "ti" + str(tempVarIntAddr + 1) tempVariableTable.insertTempVariable( result, 'int', tempVarIntAddr) tempVarIntAddr += 1 global qCount qCount += 1 quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad) stackOperands.append(result) else: raise Exception("Type mismatch")
def init(self): # Initialize the renderer, set up the Window. glfw.init() glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.DECORATED, gl.GL_TRUE) glfw.window_hint(glfw.DECORATED, gl.GL_TRUE) glfw.window_hint(glfw.RESIZABLE, gl.GL_FALSE) width = Config.windowWidth height = Config.windowHeight self.aspect = width/height # Initialize the window self.window = glfw.create_window(width, height, "Magic", None, None) glfw.make_context_current(self.window) self.renderer = Renderer() self.quad = [] self.text = [] # Set up camera self.viewMatrix = Matrix.identity() # Set up view transform. View is always 16 units high, and 16 * aspect units # wide. The extra space outside of a 4:3 ratio is unused, to make sure that # all the cards always fit in a 4:3 aspect monitor. vHeight = 2 vWidth = vHeight * self.aspect self.projectionMatrix = Matrix.ortho(-vWidth/2, vWidth/2, vHeight/2, -vHeight/2, -1, 1) self.viewProjectionMatrix = self.projectionMatrix * self.viewMatrix # Set up viewport fbWidth, fbHeight = glfw.get_framebuffer_size(self.window) gl.glViewport(0, 0, fbWidth, fbHeight) q = Quad(Texture('Images/Sen Triplets.jpg')) # Natural resolution of cards is 480x680 cardAspect = 480/680 q.x = 0 q.y = 0 q.height = .8 q.width = q.height * cardAspect self.quad.append(q)
def exitW1(self, ctx): left_operand = stackOperands.pop() right_operand = "" operator = "PRINT" result = "" global qCount qCount += 1 quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad)
def enterProgram(self, ctx): operator = "goto" left_operand = "" right_operand = "" result = "" global qCount qCount += 1 quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad)
class MyWindow(pyglet.window.Window): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.set_minimum_size(400, 300) glClearColor(0.2, 0.3, 0.2, 1.0) glEnable(GL_DEPTH_TEST) self.quad = Quad() def on_draw(self): self.clear() glDrawElements(GL_TRIANGLES, len(self.quad.indices), GL_UNSIGNED_INT, None) def on_resize(self, width, height): glViewport(0, 0, width, height) def update(self, dt): self.quad.rotate()
def exitLoop(self, ctx): end = stackJumps.pop() operator = "goto" left_operand = "" right_operand = "" result = stackJumps.pop() global qCount qCount += 1 quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad) queueQuads[end - 1].result = qCount + 1
def exitFunction_return(self, ctx): global qCount global executionSourceReturn operator = "RETURN" left_operand = stackOperands.pop() right_operand = "" result = "" qCount += 1 executionSourceReturn = False quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad)
def exitRead(self, ctx): if len(stackOperators) > 0: if stackOperators[-1] == 'read': left_operand = stackOperands.pop() right_operand = "" operator = stackOperators.pop().upper() result = "" global qCount qCount += 1 quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad)
def enterC(self, ctx): operator = str(ctx.ELSE()) if operator == "else": global qCount qCount += 1 operator = "goto" left_operand = "" right_operand = "" result = "" quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad) false = stackJumps.pop() stackJumps.append(qCount) queueQuads[false - 1].result = qCount + 1
def enterFunction_call(self, ctx): global functionId global functionCallParameterTable functionId = str(ctx.ID()) functionCallParameterTable = functionDirectory.dictionary[ functionId].parameterTable if functionId in functionDirectory.dictionary: operator = "ERA" left_operand = functionId right_operand = "" result = "" global qCount qCount += 1 quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad)
def __init__(self, width=640, height=480): pygame.init() self.screen = pygame.display.set_mode((width, height)) self.clock = pygame.time.Clock() self.vertices = np.array([ [-1, 1, -1], [1, 1, -1], [1, -1, -1], [-1, -1, -1], [-1, 1, 1], [1, 1, 1], [1, -1, 1], [-1, -1, 1] ], dtype='float32') self.faces = [(0, 1, 2, 3), (1, 5, 6, 2), (5, 4, 7, 6), (4, 0, 3, 7), (0, 4, 5, 1), (3, 2, 6, 7)] self.colors = [(255, 0, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 255, 255), (255, 255, 0)] self.angle = 0 self.quad = Quad()
def exitAssignment(self, ctx): if len(stackOperators) > 0: if stackOperators[-1] == '=': left_operand = stackOperands.pop() right_operand = stackOperands.pop() left_type = self.checkType(left_operand) right_type = self.checkType(right_operand) operator = stackOperators.pop() result_type = assignmentOperator(operator, right_type, left_type) if result_type == "true": result = "" global qCount qCount += 1 quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad) else: raise Exception("Type mismatch")
def enterMore_args(self, ctx): global pCount argument = stackOperands.pop() argumentType = self.checkType(argument) funcCallParamTable = functionDirectory.dictionary[ functionId].parameterTable funcCallParamsList = list(funcCallParamTable.parameters) key = funcCallParamsList[pCount] if argumentType == funcCallParamTable.parameters[key].parameterType: operator = "PARAM" left_operand = argument right_operand = "" result = "parameter" + str(pCount + 1) global qCount qCount += 1 quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad) else: raise Exception("Type mismatch") pCount += 1
def enterSection(self, ctx): global executionSource if executionSource == "loop" or executionSource == "condition": tempVariable = tempVariableTable.tempVariables[stackOperands[-1]] tempVariableType = tempVariable.tempVariableType if tempVariableType == "bool": operator = "gotoF" left_operand = stackOperands.pop() right_operand = "" result = "" global qCount qCount += 1 quad = Quad(operator, left_operand, right_operand, result) queueQuads.append(quad) stackJumps.append(qCount) executionSource = "" else: raise Exception("Type mismatch") if executionSource == "function": functionDirectory.dictionary[functionName].startPosition = qCount executionSource = ""
def quad_vert_generator(_verts, _quads, _fine_verts, _parameters, refinement_level): assert type(_verts) is np.ndarray assert type(_quads) is np.ndarray assert type(_fine_verts) is np.ndarray # Create vertices vertex_list = [] for i in range(_verts.shape[0]): vertex_list.append( Vertex(id=i, x=_verts[i, 0], y=_verts[i, 1], z=_verts[i, 2])) # Creating edges edge_dict = {} edge_id = 0 for i in range(_quads.shape[0]): edges = [ (_quads[i, 0], _quads[i, 1]), # edges of this quad (_quads[i, 1], _quads[i, 2]), (_quads[i, 2], _quads[i, 3]), (_quads[i, 3], _quads[i, 0]) ] for e in edges: key = tuple(sorted(e)) if key not in edge_dict: edge_dict[key] = Edge(edge_id, vertex_list[int(key[0])], vertex_list[int(key[1])]) edge_id += 1 # Creating quads quad_list = [] for i in range(_quads.shape[0]): a = int(_quads[i, 0]) b = int(_quads[i, 1]) c = int(_quads[i, 2]) d = int(_quads[i, 3]) e1 = tuple(sorted([_quads[i, 0], _quads[i, 1]])) # edges of this quad e2 = tuple(sorted((_quads[i, 1], _quads[i, 2]))) e3 = tuple(sorted((_quads[i, 2], _quads[i, 3]))) e4 = tuple(sorted((_quads[i, 3], _quads[i, 0]))) quad_list.append( Quad(i, vertex_list[a], vertex_list[b], vertex_list[c], vertex_list[d], edge_dict[e1], edge_dict[e2], edge_dict[e3], edge_dict[e4])) # Erasing hanging nodes for i in range(vertex_list.__len__()): if not vertex_list[i].get_quads(): if vertex_list[i].get_edges(): print "ERROR, hanging vertex with edges" vertex_list[i] = None # New Id's of vertices new_id = 0 new_vertex_list = [] for i in range(vertex_list.__len__()): if vertex_list[i] is not None: vertex_list[i]._id = new_id new_vertex_list.append(vertex_list[i]) new_id += 1 # Check if every edge has only two quads! for e in edge_dict: if edge_dict[e].get_quads().__len__() > 2: print e # Create fine vertices fine_vertex_list = [] for i in range(_fine_verts.shape[0]): fine_vertex_list.append( FineVertex(id=i, x=_fine_verts[i, 0], y=_fine_verts[i, 1], z=_fine_verts[i, 2], u=_parameters[i, 1], v=_parameters[i, 2], quad=quad_list[int(_parameters[i, 0])])) verts = [] quads = [] verts = [ new_vertex_list[i].get_coordinates() / (2**refinement_level) for i in range(new_vertex_list.__len__()) ] fine_verts = [ fine_vertex_list[i].get_coordinates() / (2**refinement_level) for i in range(fine_vertex_list.__len__()) ] for i in range(quad_list.__len__()): quads.append( [quad_list[i].get_vertices()[j].get_id() for j in range(4)]) #verts = np.array(verts) #quads = np.array(quads) #fine_verts = np.array(fine_verts) return np.array(verts), np.array(quads), np.array( fine_verts), new_vertex_list, edge_dict, quad_list