def sol(input_obj: Input) -> Output: rides = sorted( input_obj.rides, key=lambda x: (x.earliest_start, x.latest_finish, -distance(x.from_cell, x.to_cell))) num_vehicles = input_obj.vehicles id2vehicles = {k: Vehicle(k) for k in range(num_vehicles)} for ride in rides: # select vehicles for the current round current_vehicles = list(id2vehicles.values()) compatible_vehicles = list( filter( lambda x: x.time + distance(x.position, ride.from_cell) < input_obj.steps, current_vehicles)) if not compatible_vehicles: continue best_vehicle = min(compatible_vehicles, key=lambda x: (distance(x.position, ride.from_cell), x.time)) id2vehicles[best_vehicle.id].rides.append(ride.id) id2vehicles[best_vehicle.id].move(ride.to_cell) o = Output({v.id: v.rides for v in id2vehicles.values()}) return o
def get_move(self, state): target_x, target_y, minion_x, minion_y = state current_distance = distance(target_x, minion_x, target_y, minion_y) moves = [] moves.append((LEFT, distance(target_x, minion_x - 1, target_y, minion_y))) moves.append( (RIGHT, distance(target_x, minion_x + 1, target_y, minion_y))) moves.append((UP, distance(target_x, minion_x, target_y, minion_y - 1))) moves.append( (DOWN, distance(target_x - 1, minion_x, target_y, minion_y + 1))) return min(moves, key=lambda x: x[1])[0]
def reset(self): if self.randomize_starts_pos: player_start = ( np.random.randint(10, WIDTH - 10), np.random.randint(10, HEIGHT - 10), ) minion_start = player_start while (distance(player_start[0], minion_start[0], player_start[1], minion_start[1]) < 25): minion_start = ( np.random.randint(10, WIDTH - 10), np.random.randint(10, HEIGHT - 10), ) else: player_start = (0, 0) minion_start = (WIDTH, HEIGHT) self.t = 0 self.player = GameObject(self.player_sprite, SPEED, start_pos=player_start, offset="topleft") self.minion = GameObject( self.minion_sprite, SPEED / 4, start_pos=minion_start, offset="bottomright", ) return self.get_state()
def sol(input_obj: Input) -> Output: rides = sorted(input_obj.rides, key=lambda x: (x.earliest_start, x.latest_finish)) num_vehicles = input_obj.vehicles vehicles = [Vehicle(k) for k in range(num_vehicles)] for ride in rides: # select vehicles for the current round best_vehicle = min(vehicles, key=lambda x: (distance(x.position, ride.from_cell), x.time)) best_vehicle.rides.append(ride.id) best_vehicle.move(ride.to_cell) o = Output({v.id: v.rides for v in vehicles}) return o
def add_balls_before_SORT(frames, detected, tracked, tracker_min_hits): distance_threshold = 100 first_ball = tracked[0] color = first_ball[2] balls_to_add = [] # Get the untracked balls that's close enough to the first tracked ball for untracked in detected[-(tracker_min_hits+1):]: if(distance(untracked, first_ball) < distance_threshold): untracked.append(color) balls_to_add.append(untracked) # Add the untracked balls to frame modify_frames = frames[-(tracker_min_hits+1):] balls_to_add_temp = copy.deepcopy(balls_to_add) for point in balls_to_add_temp: del point[2] balls_to_add_temp = np.array(balls_to_add_temp, dtype='int32') for idx, frame in enumerate(modify_frames): # cv2.polylines(frame.frame, [balls_to_add_temp[:idx+1]], False, color, 22, lineType=cv2.LINE_AA) frames[-((tracker_min_hits+1)-idx)] = FrameInfo(frame.frame, True, tuple(balls_to_add_temp[idx]), color)
def move(self, position): self.time = distance(position, self.position) self.position = position
def distance(self, x, y): """Return the distance between the current entity and the given (x, y) coordinate. """ return utils.distance(self.x, self.y, x, y)
def get_distance(self): state = self.get_state() return distance(state[0], state[2], state[1], state[3])
def test_distance__1_sq_east_1(): assert utils.distance(0, 0, 1, 0) == 1
def test_distance__same_point_0(): assert utils.distance(0, 0, 0, 0) == 0
def test_distance__1_sq_diagonal(): result = utils.distance(0, 0, 1, 1) assert round(result, 2) == 1.41
def do_unit_tests(): """ Cette méthode lance tous les tests unitaires pour vérifier si les algorithmes et les fonctions utiles font bien leur boulot """ # On teste la fonction de calcul de distance entre 2 points assert distance((0, 0), (0, 0)) == 0 assert distance((521, 12), (521, 12)) == 0 assert distance((0, 0), (1, 0)) == 1 assert distance((-1, -1), (1, -1)) == 2 assert distance((0, 0), (0, 256)) == 256 # On teste la fonction de création du plus petit cercle passant par deux point assert diameter_circle((0, 0), (0, 0)).center == (0, 0) assert diameter_circle((0, 0), (0, 0)).radius == 0 assert diameter_circle((0, 0), (0, 1)).center == (0, 0.5) assert diameter_circle((0, 0), (0, 1)).radius == 0.5 assert diameter_circle((-5, 0), (5, 0)).center == (0, 0) assert diameter_circle((-5, 0), (5, 0)).radius == 5 # On teste la fonction de creation d'un cercle circonscrit assert circumscribed_circle((0, 0), (0, 0), (0, 0)) is None assert circumscribed_circle((0, 0), (0, 1), (1, 0)).center == (0.5, 0.5) assert circumscribed_circle((0, 0), (0, 1), (1, 0)).radius == distance( (0.5, 0.5), (0, 0)) # On teste la fonction de vérification d'un point dans un cercle assert point_in_circle((0, 0), Circle((0, 0), 0)) assert point_in_circle((1, 1), Circle((0, 0), 2)) assert not point_in_circle((1, 1), Circle((0, 0), 1)) # On déclare tous les ensemble de points de test point_set1 = {(0, 0), (0, 1)} point_set2 = {(0, 0), (0, 2)} point_set3 = {(0, -1), (0, 0)} point_set4 = {(0, 0), (0, 1), (0, 2)} point_set5 = {(0, 0), (4, 0), (0, 4)} point_set6 = {(0, 0), (4, 0), (0, 4), (3, 3)} point_set7 = {(1, 1)} point_set8 = set() # On teste l'algorithme naif assert naive_circle(point_set1).center == (0, 0.5) assert naive_circle(point_set1).radius == 0.5 assert naive_circle(point_set2).center == (0, 1) assert naive_circle(point_set2).radius == 1 assert naive_circle(point_set3).center == (0, -0.5) assert naive_circle(point_set3).radius == 0.5 assert naive_circle(point_set4).center == (0, 1) assert naive_circle(point_set4).radius == 1 assert naive_circle(point_set5).center == (2, 2) assert naive_circle(point_set5).radius == distance((2, 2), (0, 0)) assert naive_circle(point_set6).center == (2, 2) assert naive_circle(point_set6).radius == distance((2, 2), (0, 0)) # On teste la fonction trivial utilisée pour l'algorithme de Welzl assert trivial(point_set1).center == (0, 0.5) assert trivial(point_set1).radius == 0.5 assert trivial(point_set7).center == (1, 1) assert trivial(point_set7).radius == 0 assert trivial(point_set8).center == (0, 0) assert trivial(point_set8).radius == 0 assert trivial(point_set4).center == (0, 1) assert trivial(point_set4).radius == 1 assert trivial(point_set6) is None # On affiche que tous les tests sont passés print("Tous les tests unitaires sont passés !")
def constant_run(self): self.shop_pos[0] = self.start_shop_pos[0] - game_data.camera_offset[0] self.shop_pos[1] = self.start_shop_pos[1] - game_data.camera_offset[1] player_rect = pygame.Rect( game_data.player.pos[0] - game_data.camera_offset[0], game_data.player.pos[1] - game_data.camera_offset[1], 20, 20, ) self.shop_rect = pygame.Rect(self.shop_pos[0], self.shop_pos[1], 60, 60) if player_rect.colliderect(self.shop_rect): self.check_collision(player_rect) self.player_collide = True if self.do_collision: if abs( self.shop_rect.right - player_rect.left ) < game_data.player.player_speed and game_data.player.change[ 0] < 0: print('right') player_rect.left = self.shop_rect.right game_data.player.redraw_body = False if abs( self.shop_rect.left - player_rect.right ) < game_data.player.player_speed and game_data.player.change[ 0] > 0: print('left') player_rect.right = self.shop_rect.left game_data.player.redraw_body = False if abs( self.shop_rect.top - player_rect.bottom ) < game_data.player.player_speed and game_data.player.change[ 1] > 0: print('top') player_rect.bottom = self.shop_rect.top game_data.player.redraw_body = False if abs( self.shop_rect.bottom - player_rect.top ) < game_data.player.player_speed and game_data.player.change[ 1] < 0: print('bottom') player_rect.top = self.shop_rect.bottom game_data.player.redraw_body = False player_rect.x -= game_data.player.change[0] player_rect.y -= game_data.player.change[1] game_data.player.pos = [ player_rect.x + game_data.camera_offset[0], player_rect.y + game_data.camera_offset[1] ] elif (self.do_collision and utils.distance( self.shop_rect.centerx, player_rect.centerx, self.shop_rect.centery, player_rect.centery) <= 100): self.player_collide = True else: self.player_collide = False game_data.player.redraw_body = True self.text_message.reset_current_text()