def collisionCercleLigne(self, cercle, ligne): listeLigne = [] if len(cercle.lineList) == 0: nbrect = 8 x = cercle.x + cercle.rayon * 1. * math.sin(0) y = cercle.y + cercle.rayon * 1. * math.cos(0) for i in numpy.arange(0, math.pi * 2, math.pi / (nbrect + 1) * 2): x1 = cercle.x + cercle.rayon * 1. * math.sin(i) y1 = cercle.y + cercle.rayon * 1. * math.cos(i) ligne1 = Ligne("", x, y, x1, y1, "red") listeLigne.append(ligne1) x = x1 y = y1 x1 = cercle.x + cercle.rayon * 1. * math.sin(0) y1 = cercle.y + cercle.rayon * 1. * math.cos(0) listeLigne.append(Ligne("", x, y, x1, y1, "red")) cercle.lineList = listeLigne """if self.fenetre != None: for ligne1 in listeLigne: ligne1.setCouleur("blue") ligne1.dessiner(self.fenetre)""" else: listeLigne = cercle.lineList for ligne1 in listeLigne: if self.collisionEntre(ligne1, ligne): return True return False
def collisionCercleRectangle(self, cercle, rectangle): listCote = [] listCote.append( Ligne("haut", rectangle.x1, rectangle.y1, rectangle.x2, rectangle.y1, "black")) listCote.append( Ligne("bas", rectangle.x1, rectangle.y2, rectangle.x2, rectangle.y2, "black")) listCote.append( Ligne("gauche", rectangle.x1, rectangle.y1, rectangle.x1, rectangle.y2, "black")) listCote.append( Ligne("droite", rectangle.x2, rectangle.y1, rectangle.x2, rectangle.y2, "black")) listeRect = [] nbrect = 7 for i in numpy.arange(0, math.pi, math.pi / (nbrect + 1)): x = cercle.rayon * 1. * math.sin(i) y = cercle.rayon * 1. * math.cos(i) rect = Rectangle("", cercle.x - x, cercle.y - y, cercle.x + x, cercle.y + y, "red") listeRect.append(rect) for cote in listCote: for rect in listeRect: if self.collisionEntre(rect, cote): return True return False
def contenuDans(self, x, y, forme): if isinstance(forme, Ligne): return False if isinstance(forme, Cercle): return forme.distanceAvec(Ligne("", x, y, x, y)) <= forme.rayon if isinstance(forme, Rectangle): return forme.x1 <= x and forme.x2 >= x and forme.y1 <= y and forme.y2 >= y ligne = Ligne("", x, y, forme.x, forme.y) return not self.collisionEntre(ligne, forme)
def toJson(self): line = Ligne("", self.x, self.y, self.x + 100, self.y, "blue") line.rotate(self.angle) str = u''.join([ u'{', u'"type":"line",', u'"name":"{}",'.format(""), u'"x1":{},'.format(self.x), u'"y1":{},'.format(self.y), u'"x2":{},'.format(line.x2), u'"y2":{},'.format(line.y2), u'"color":"{}"'.format("blue"), u'}' ]) return str
def main(): set_controls() origin = XYZ() # world = vp.box(size=vp.vector(20000, 20000, 1)) # print('hello?') # vp.scene.caption = """Right button drag to rotate. Scroll wheel to zoom.""" # side = 4.0 # thk = 0.3 # s2 = 2*side - thk # s3 = 2*side + thk # wallR = vp.box (pos=vp.vector( side, 0, 0), size=vp.vector(thk, s2, s3), color = vp.color.red) # wallL = vp.box (pos=vp.vector(-side, 0, 0), size=vp.vector(thk, s2, s3), color = vp.color.red) # wallB = vp.box (pos=vp.vector(0, -side, 0), size=vp.vector(s3, thk, s3), color = vp.color.blue) # wallT = vp.box (pos=vp.vector(0, side, 0), size=vp.vector(s3, thk, s3), color = vp.color.blue) # wallBK = vp.box(pos=vp.vector(0, 0, -side), size=vp.vector(s2, s2, thk), color = vp.color.gray(0.7)) # ball = vp.sphere (color = vp.color.green, radius = 0.4, make_trail=True, retain=200) # ball.mass = 1.0 # ball.p = vp.vector (-0.15, -0.23, +0.27) # side = side - thk*0.5 - ball.radius # dt = 0.3 # while True: # vp.rate(200) # ball.pos = ball.pos + (ball.p/ball.mass)*dt # if not (side > ball.pos.x > -side): # ball.p.x = -ball.p.x # if not (side > ball.pos.y > -side): # ball.p.y = -ball.p.y # if not (side > ball.pos.z > -side): # ball.p.z = -ball.p.z ligne_verte = Ligne('verte.json') # wagon = Wagon(pos=vp.vector(10, 10, 0)) # evolving = [wagon] vp.scene.camera.axis = vp.vector(0, 0, -1) vp.scene.fullscreen = True exit = False dt = 0.01 sym_time = 0 while not exit: # vp.sleep(dt) vp.rate(1 / dt) sym_time += dt ligne_verte.update(sym_time) # poll() print(vp.scene.camera.pos) print(vp.scene.camera.axis)
def collisionRectangleLigne(self, rectangle, ligne): haut = Ligne("haut", rectangle.x1, rectangle.y1, rectangle.x2, rectangle.y1, "black") bas = Ligne("bas", rectangle.x1, rectangle.y2, rectangle.x2, rectangle.y2, "black") gauche = Ligne("gauche", rectangle.x1, rectangle.y1, rectangle.x1, rectangle.y2, "black") droite = Ligne("droite", rectangle.x2, rectangle.y1, rectangle.x2, rectangle.y2, "black") if self.collisionEntre(ligne, haut) or self.collisionEntre( ligne, bas) or self.collisionEntre( ligne, gauche) or self.collisionEntre(ligne, droite): return True return False
def collisionPolygoneCercle(self, polygone, cercle): count = 0 firstPoint = polygone.pointList[0] lastPoint = polygone.pointList[0] for point in polygone.pointList: count += 1 if count == 1: continue currentLine = Ligne("", float(point["x"]), float(point["y"]), float(lastPoint["x"]), float(lastPoint["y"])) if self.collisionLigneCercle(currentLine, cercle): return True lastPoint = point currentLine = Ligne("", float(firstPoint["x"]), float(firstPoint["y"]), float(lastPoint["x"]), float(lastPoint["y"])) if self.collisionLigneCercle(currentLine, cercle): return True return False
def dessiner(self, fenetre): self.forme.dessiner(fenetre) line = Ligne("", self.x, self.y, self.x + 100, self.y, "blue") line.rotate(self.angle) line.dessiner(fenetre)
def groupe_jouer_coup(): tests = [] ligne_jouee = Ligne() ligne_jouee.jouee = True lignes_attendues = { (0, 3, 'V'): Ligne(), (1, 3, 'V'): Ligne(), (2, 3, 'V'): Ligne(), (3, 0, 'H'): Ligne(), (3, 1, 'H'): Ligne(), (3, 2, 'H'): Ligne(), (0, 0, 'H'): Ligne(), (0, 0, 'V'): Ligne(), (1, 0, 'H'): Ligne(), (1, 0, 'V'): Ligne(), (2, 0, 'H'): Ligne(), (2, 0, 'V'): Ligne(), (0, 1, 'H'): Ligne(), (0, 1, 'V'): Ligne(), (1, 1, 'H'): Ligne(), (1, 1, 'V'): Ligne(), (2, 1, 'H'): Ligne(), (2, 1, 'V'): Ligne(), (0, 2, 'H'): Ligne(), (0, 2, 'V'): ligne_jouee, (1, 2, 'H'): Ligne(), (1, 2, 'V'): Ligne(), (2, 2, 'H'): Ligne(), (2, 2, 'V'): Ligne() } tests.append( TestAttributsObjet(planche_vide, 'test_jouer_coup_rouge', { 'index_ligne': (0, 2, 'V'), 'couleur': 'rouge' }, { 'lignes': lignes_attendues, 'couleur_dernier_coup': 'rouge', 'position_dernier_coup': (0, 2, 'V') })) tests.append( TestAttributsObjet(planche_vide, 'test_jouer_coup_bleu', { 'index_ligne': (0, 2, 'V'), 'couleur': 'bleu' }, { 'lignes': lignes_attendues, 'couleur_dernier_coup': 'bleu', 'position_dernier_coup': (0, 2, 'V') })) return GroupeTest('Planche.jouer_coup', tests, 'jouer_coup')
def groupe_init_lignes(): tests = [] lignes_attendues = { (0, 3, 'V'): Ligne(), (1, 3, 'V'): Ligne(), (2, 3, 'V'): Ligne(), (3, 0, 'H'): Ligne(), (3, 1, 'H'): Ligne(), (3, 2, 'H'): Ligne(), (0, 0, 'H'): Ligne(), (0, 0, 'V'): Ligne(), (1, 0, 'H'): Ligne(), (1, 0, 'V'): Ligne(), (2, 0, 'H'): Ligne(), (2, 0, 'V'): Ligne(), (0, 1, 'H'): Ligne(), (0, 1, 'V'): Ligne(), (1, 1, 'H'): Ligne(), (1, 1, 'V'): Ligne(), (2, 1, 'H'): Ligne(), (2, 1, 'V'): Ligne(), (0, 2, 'H'): Ligne(), (0, 2, 'V'): Ligne(), (1, 2, 'H'): Ligne(), (1, 2, 'V'): Ligne(), (2, 2, 'H'): Ligne(), (2, 2, 'V'): Ligne() } ma_planche_pleine = planche_vide() for ligne in ma_planche_pleine.lignes.values(): ligne.jouee = True tests.append( TestAttributsObjet(planche_vide, 'test_initilisation_lignes', {}, {'lignes': lignes_attendues})) tests.append( TestAttributsObjet(obj_to_func(ma_planche_pleine), 'test_reinitilisation_lignes', {}, {'lignes': lignes_attendues})) return GroupeTest('Planche.init_lignes', tests, 'initialiser_lignes')