예제 #1
0
파일: graph.py 프로젝트: Ace17/graphmaze
    def tick(self, userInput):
        viewModel = ViewModel()
        viewModel.edges = self.fullGraph.edges

        if userInput.click == True:
            self.k += 1
            self.currNode = self.fullGraph.nodes[self.k %
                                                 len(self.fullGraph.nodes)]

        # layout nodes
        i = 0
        N = len(self.afferent[self.currNode])
        radius = NODE_RADIUS * (2 + N)
        for name in self.afferent[self.currNode]:
            angleFraction = math.pi / (N + 2)
            angle = -((1 + i + 0.5) * angleFraction)
            viewModel.nodePos[name] = Vec2(math.cos(angle),
                                           math.sin(angle)) * radius
            i += 1

        i = 0
        N = len(self.efferent[self.currNode])
        radius = NODE_RADIUS * (2 + N)
        for name in self.efferent[self.currNode]:
            angleFraction = math.pi / (N + 2)
            angle = +((1 + i + 0.5) * angleFraction)
            viewModel.nodePos[name] = Vec2(math.cos(angle),
                                           math.sin(angle)) * radius
            i += 1

        viewModel.nodePos[self.currNode] = Vec2(0, 0)

        return viewModel
예제 #2
0
    def render(self, screen, camera):
        # Draw the main player
        pygame.draw.circle(
            screen, (0, 0, 0),
            camera.world_to_screen(self.pos).to_int().to_tuple(),
            int(self.radius * camera.zoom))

        # Find the middle of the deflector
        deflector_mid_point = self.pos + Vec2(
            self.deflector_distance_from_player * math.cos(
                self.deflector_angle), -self.deflector_distance_from_player *
            math.sin(self.deflector_angle))

        #Find the vector that takes you from the middle to the end of the deflector (to get to the start, just do the negative of this vector
        vec_to_end = Vec2(
            (self.deflector_width / 2) * math.sin(self.deflector_angle),
            (self.deflector_width / 2) * math.cos(self.deflector_angle))

        # Find the start and end of the deflector
        # These are stored as member variables because they are useful in the detection of collisions between the deflector and bullets
        self.deflector_start = deflector_mid_point - vec_to_end
        self.deflector_end = deflector_mid_point + vec_to_end

        # Draw the deflector
        pygame.draw.line(
            screen, (0, 0, 0),
            camera.world_to_screen(self.deflector_start).to_int().to_tuple(),
            camera.world_to_screen(self.deflector_end).to_int().to_tuple())
예제 #3
0
    def __init__(self):
        # Prepping pygame and the gui window
        pygame.init()
        pygame.font.init()
        self.resolution = (1920, 1050)
        self.window = pygame.display.set_mode(self.resolution, pygame.RESIZABLE)
        pygame.display.set_caption("AirSchedule")
        pygame.display.set_icon(pygame.image.load("icon.png"))
        self.bg_color = (100, 100, 100)

        # Loading up some commonly used fonts
        self.font_15 = pygame.font.SysFont("courier", 15)
        self.font_25 = pygame.font.SysFont("courier", 25)
        self.font_30 = pygame.font.SysFont("courier", 30)

        self.quit = False
        # The elements currently active
        self.elements = []
        # Pygame events
        self.events = []
        self.scroll = 0
        self.clock = pygame.time.Clock()
        # The reference time that everything is referenced to
        self.default_time = None

        # Mouse activity
        self.click = False
        self.click_time = time.time()
        self.double_click = False
        self.mouse_start = Vec2(0, 0)
        self.mouse_pos = Vec2(0, 0)
예제 #4
0
    def test_get_best_action_works_behind_right(self):
        board = PodBoard([Vec2(5000, 5000), Vec2(1000, 1000)])
        # Pod is directly right of check, but facing away (slightly to the right)
        pod = PodState(Vec2(7000, 5000))
        pod.angle = -0.000001

        self.__do_get_best_action_assert(board, pod, 0, -Constants.max_turn())
예제 #5
0
    def test_get_best_action_works_right(self):
        board = PodBoard([Vec2(5000, 5000), Vec2(1000, 1000)])
        # Pod is directly below the check, but the check is behind and to its right
        pod = PodState(Vec2(5000, 0))
        pod.angle = math.pi * 1.25

        self.__do_get_best_action_assert(board, pod, 0, -Constants.max_turn())
예제 #6
0
    def test_get_best_action_works_straight(self):
        board = PodBoard([Vec2(5000, 5000), Vec2(1000, 1000)])
        # Pod is directly below the check, but looking straight at it
        pod = PodState(Vec2(5000, 0))
        pod.angle = math.pi / 2

        self.__do_get_best_action_assert(board, pod, Constants.max_thrust(), 0)
예제 #7
0
def test_get_4_pixel_neighborhood():
    binimg = [[0, 0, 1, 1], [0, 0, 1, 1], [0, 1, 1, 1], [0, 1, 1, 0]]

    assert _get4PixelNeighborhood(binimg, Vec2(0, 0)) == ((0, 0), (0, 0))
    assert _get4PixelNeighborhood(binimg, Vec2(1, 2)) == ((0, 0), (0, 1))
    assert _get4PixelNeighborhood(binimg, Vec2(1, 4)) == ((0, 1), (0, 0))
    assert _get4PixelNeighborhood(binimg, Vec2(3, 3)) == ((1, 1), (1, 0))
    assert _get4PixelNeighborhood(binimg, Vec2(4, 0)) == ((0, 0), (1, 0))
예제 #8
0
    def __init__(self, pos, w, h):
        self.pos = pos
        self.zoom = 1
        self.screen_size = Vec2(w, h)
        self.size = Vec2(w, h)

        self.target_zoom = 1
        self.target_pos = pos
        self.target_size = Vec2(w, h)
예제 #9
0
파일: d03.py 프로젝트: Randdalf/aoc20
def check_slopes(terrain):
    results = [
        count_trees(terrain, slope=Vec2(1, 1)),
        count_trees(terrain, slope=Vec2(3, 1)),
        count_trees(terrain, slope=Vec2(5, 1)),
        count_trees(terrain, slope=Vec2(7, 1)),
        count_trees(terrain, slope=Vec2(1, 2))
    ]
    return reduce(lambda x, y: x*y, results)
예제 #10
0
    def __init__(self, pos):
        # Scalars
        self.density = 0

        # Forces
        self.position = pos
        self.velocity = Vec2(0, 0)
        self.pressure_force = Vec2(0, 0)
        self.viscosity_force = Vec2(0, 0)
예제 #11
0
 def test_PodState_equals_not_initial(self):
     p1 = PodState(pos=Vec2(1, 2),
                   angle=1.23,
                   vel=Vec2(3, 4),
                   next_check_id=5)
     p2 = PodState(pos=Vec2(1, 2),
                   angle=1.23,
                   vel=Vec2(3, 4),
                   next_check_id=5)
     self.assertEqual(p1, p2)
예제 #12
0
def createBoid(screen):
    boids = []

    for i in range(10):
        position = Vec2(random.randrange(700, SCREEN_W),
                        random.randrange(400, SCREEN_H))
        velocity = Vec2(0, 0)
        p = Boid(position, velocity, screen)
        boids.append(p)
    return boids
예제 #13
0
def checkRule2(boids, b, target):
    c = Vec2(0, 0)  #CONTROL VECTOR FOR POSITION
    targetPos = Vec2(target.x, target.y)
    for boid in boids:
        if (boid != b and abs(boid.position - b.position) < 20):
            c = c - (boid.position - b.position)
    if (abs(targetPos - b.position) < 20):
        c = c - (targetPos / b.position)
    if (c.mag() > 0):
        c = c.norm()
    return c
예제 #14
0
def moveBoids(boids, target):
    v1, v2, v3 = Vec2(0, 0), Vec2(0, 0), Vec2(0, 0)
    for b in boids:
        v1 = checkRule1(boids, b)  #CHECK COHESION
        v2 = checkRule2(boids, b, target)  #CHECK ALIGNMENT
        v3 = checkRule3(boids, b)  #CHECK SEPARATION
        trg = getTarget(b, target)

        b.velocity = b.velocity + v1 + v2 + v3 + trg
        limit_speed(b)
        b.position = b.position + b.velocity + boundPos(b)
예제 #15
0
    def __init__(self, x, y, c):
        self.pos = Vec2(x, y)
        self.vel = Vec2(0, 0)
        self.colour = c

        self.radius = 10

        #Amount of ticks between shooting
        self.max_shoot_timer = 20
        self.shoot_timer = self.max_shoot_timer

        self.alive = True
예제 #16
0
    def test_state_to_vector_works1(self):
        # A pod at (100, 100) pointing down -X, moving full speed +Y
        pod = PodState(Vec2(100, 100), Vec2(0, Constants.max_vel()), -math.pi)
        # The target checkpoint is directly behind it
        board = PodBoard([Vec2(100 + MAX_DIST, 100), ORIGIN])

        state = state_to_vector(pod, board)

        self.assertEqual(len(state), STATE_VECTOR_LEN)
        self.assertAlmostEqual(state[0], 0, msg="velocity x")
        self.assertAlmostEqual(state[1], -1, msg="velocity y")
        self.assertAlmostEqual(state[2], -1, msg="check1 x")
        self.assertAlmostEqual(state[3], 0, msg="check1 y")
예제 #17
0
    def tester() -> 'PodBoard':
        """
        Generate a board laid out to test as many situations as possible
        (start) -> 0 -> 1: straight line
        1 -> 2: 180° turn
        2 -> 3: 90° turn
        3 -> 4 -> 5 -> 6: curve around to the right
        6 -> 7 (start): curve to the left
        """
        checks = []
        start = Vec2(Constants.world_x() / 10, Constants.world_y() / 2)
        checks.append(start + Vec2(5000, 0))  # straight ahead
        checks.append(checks[-1] + Vec2(6000, 0))  # straight ahead

        checks.append(checks[-1] + Vec2(-3000, 0))  # straight back

        checks.append(checks[-1] + Vec2(0, 2500))  # turn 90°

        checks.append(checks[-1] + Vec2(-3000, 1500))  # curve around
        checks.append(checks[-1] + Vec2(-3000, -1500))  # curve around
        checks.append(checks[-1] + Vec2(0, -5500))  # curve around

        checks.append(start)  # turn other way

        return PodBoard(checks)
예제 #18
0
    def grid(rows: int = 3,
             cols: int = 3,
             x_spacing: int = 4000,
             y_spacing: int = 3000) -> 'PodBoard':
        """
        Generate a board with checks in grid form:
        1 2 3
        4 5 6
        7 8 9
        """
        checks = []

        x_center = Constants.world_x() / 2
        y_center = Constants.world_y() / 2

        # 5 rows: -2, -1, 0, 1, 2
        # 4 rows: -1.5, -0.5, 0.5, 1.5
        # 3 rows: -1, 0, 1
        # => start at -(r-1)/2
        row_start = (1 - rows) / 2
        col_start = (1 - cols) / 2

        for row in range(rows):
            y_off = (row_start + row) * y_spacing
            for col in range(cols):
                x_off = (col_start + col) * x_spacing
                checks.append(Vec2(x_center + x_off, y_center + y_off))

        return PodBoard(checks)
예제 #19
0
 def test_rmul_scalar_works(self):
     v1 = Vec2(1, 2)
     v2 = 3 * v1
     self.assertEqual(v1.x, 1)
     self.assertEqual(v1.y, 2)
     self.assertEqual(v2.x, 3)
     self.assertEqual(v2.y, 6)
예제 #20
0
 def test_radd_scalar_works(self):
     v1 = Vec2(1, 2)
     v2 = 5 + v1
     self.assertEqual(v1.x, 1)
     self.assertEqual(v1.y, 2)
     self.assertEqual(v2.x, 6)
     self.assertEqual(v2.y, 7)
예제 #21
0
def drag_handler(element, client, gui):
    # how far the mouse has moved
    drag = gui.mouse_pos - gui.mouse_start
    # ac_change keeps track of how many rows we have dragged the flight through
    # we also make sure that the user can't drag the flight to an empty row (No aircraft)
    flight = element.object
    ac_list = client.objects["aircraft"]
    ac_index = ac_list.index(element.object.aircraft)
    lower_limit = 0 - ac_index
    upper_limit = len(ac_list) - ac_index - 1
    ac_change = drag[1] // 35
    if ac_change < lower_limit:
        ac_change = lower_limit
    elif ac_change > upper_limit:
        ac_change = upper_limit
    # how much time we have passed
    time_change = drag[0] // (client.MINUTES_WIDTH*5)
    # modifies the loc_mod of the selected flight(s) so that we can see them being dragged around
    if element.selected:
        element.loc_mod += Vec2(time_change * client.MINUTES_WIDTH * 5, ac_change*35)

    # If the element is moved and deselected we create an event request to send to the server
    if element.deselected:
        if ac_change != 0:
            old_ac_name = flight.aircraft.name
            new_ac_name = ac_list[ac_index + ac_change].name
            client.new_events.append("flight,%s,aircraft,%s,%s" % (flight.name, old_ac_name, new_ac_name))
        if time_change != 0:
            old_dp = flight.dept_time.isoformat()
            old_ar = flight.arri_time.isoformat()
            new_dp = (flight.dept_time + timedelta(minutes=time_change*5)).isoformat()
            new_ar = (flight.arri_time + timedelta(minutes=time_change*5)).isoformat()
            client.new_events.append("flight,%s,dept_time,%s,%s" % (flight.name, old_dp, new_dp))
            client.new_events.append("flight,%s,arri_time,%s,%s" % (flight.name, old_ar, new_ar))
예제 #22
0
 def update(self, client):
     # self.clock.tick()
     # print(self.clock.get_time())
     self.window.fill(self.bg_color)
     # Refreshing pygame and getting mouse/key events
     self.events = pygame.event.get()
     self.mouse_pos = Vec2(pygame.mouse.get_pos())
     for event in self.events:
         if event.type == pygame.QUIT:
             self.quit = True
         elif event.type == pygame.MOUSEBUTTONDOWN:
             if event.button == 4:
                 self.scroll -= 25
             elif event.button == 5:
                 self.scroll += 25
             if self.scroll > 0:
                 self.scroll = 0
             if event.button == 1:
                 if time.time() - self.click_time < 0.25:
                     self.click = False
                     self.double_click = True
                     self.mouse_start = self.mouse_pos.copy()
                 else:
                     self.click = True
                     self.click_time = time.time()
                     self.mouse_start = self.mouse_pos.copy()
         elif event.type == pygame.MOUSEBUTTONUP:
             if event.button == 1:
                 self.click = self.double_click = False
     # Update and render elements
     for element in self.elements:
         element.update(client, self)
         element.render(self.window)
     # update display
     pygame.display.update()
예제 #23
0
def test_normalize_4_pixel_neighborhood():
    neighborhood1 = ((0, 0), (0, 1))

    assert _normalize4PixelNeighborhood(neighborhood1,
                                        Vec2(0, 1)) == ((0, 0), (0, 1))
    assert _normalize4PixelNeighborhood(neighborhood1,
                                        Vec2(1, 0)) == ((0, 1), (0, 0))
    assert _normalize4PixelNeighborhood(neighborhood1,
                                        Vec2(-1, 0)) == ((0, 0), (1, 0))
    assert _normalize4PixelNeighborhood(neighborhood1,
                                        Vec2(0, -1)) == ((1, 0), (0, 0))

    neighborhood2 = ((0, 0), (1, 1))

    assert _normalize4PixelNeighborhood(neighborhood2,
                                        Vec2(-1, 0)) == ((1, 0), (1, 0))
예제 #24
0
def checkRule1(boids, b):
    pc = Vec2(0, 0)  #PERCIVED CENTER OF THE BOID FOR B
    for boid in boids:
        if (boid != b):
            pc = pc + boid.position
    pc = pc * (1 / (len(boids) - 1))
    return (pc - b.position) * (1 / 100)
예제 #25
0
    def update(self, input_state):

        if not (self.alive):
            return
        #Use F=ma to get the acceleration
        if input_state.move_up:
            self.accel.y -= self.force_applied / self.mass * input_state.move_up

        if input_state.move_down:
            self.accel.y += self.force_applied / self.mass * input_state.move_down

        if input_state.move_left:
            self.accel.x -= self.force_applied / self.mass * input_state.move_left

        if input_state.move_right:
            self.accel.x += self.force_applied / self.mass * input_state.move_right

            # s = ut + 1/2 at^2
        self.pos += self.vel * constants.DT + self.accel * 0.5 * constants.DT**2
        # v = u+at
        self.vel = (self.vel + self.accel * constants.DT) * (1 - self.friction)
        self.accel = self.accel * (1 - self.friction)

        #Calculate the angle the deflector is facing by finding the vector between the mouse and the player and getting the angle of it
        self.deflector_angle = -(Vec2.from_tuple(input_state.mouse_pos) -
                                 self.pos).angle()
예제 #26
0
    def trainer(num_checks: int = 3) -> 'PodBoard':
        """
        Generate a board with the given number of checks.
        They are all in a row, but at varying distances.
        The goal is to use it with gen_pods to generate test data with varying distances to the next check.
        """
        checks = [
            Vec2(Constants.check_radius() * ((i + 1)**2),
                 Constants.world_y() / 2) for i in range(num_checks)
        ]

        # Shift the checks to center them
        width = checks[-1].x - checks[0].x
        x_start = (Constants.world_x() - width) / 2 - checks[0].x

        return PodBoard([check + Vec2(x_start, 0) for check in checks])
예제 #27
0
    def change_zoom(self, new_zoom):
        self.target_zoom = new_zoom

        self.target_size = self.screen_size / self.target_zoom

        self.target_pos = self.pos - (self.target_size -
                                      self.size).multiply_vec(Vec2(0.5, 0.5))
예제 #28
0
파일: d20.py 프로젝트: Randdalf/aoc20
def reconstruct(grid, size):
    image = {}
    for pos, tile in grid.items():
        offset = pos * (tile.size - 2)
        for y in range(1, tile.size - 1):
            for x in range(1, tile.size - 1):
                image[Vec2(x - 1, y - 1) + offset] = tile.get(x, y)
    return image, max(pos.x for pos in image) + 1
예제 #29
0
    def test_state_to_vector_works2(self):
        # A pod at (-100, -100) pointing up +Y, moving 45 degrees down-left
        pod = PodState(Vec2(-100, -100), Vec2(-3, -3), math.pi / 2)
        # The target checkpoint is directly in front
        board = PodBoard([Vec2(-100, 1000), ORIGIN])

        state = state_to_vector(pod, board)

        self.assertEqual(len(state), STATE_VECTOR_LEN)
        self.assertAlmostEqual(state[0],
                               -3 / Constants.max_vel(),
                               msg="velocity x")
        self.assertAlmostEqual(state[1],
                               3 / Constants.max_vel(),
                               msg="velocity y")
        self.assertAlmostEqual(state[2], 1100 / MAX_DIST, msg="check1 x")
        self.assertAlmostEqual(state[3], 0, msg="check1 y")
예제 #30
0
def handlePU(pos, strokestr):
    global polylines, pd_was_seen, lastpos, moves

    # Register non-cutting move
    if pos:
        parsedpos = parsePosition(pos)
        moves.append(lastpos - Vec2(int(parsedpos[0]), int(parsedpos[1])))

    if pd_was_seen:
        polyline = Polyline(currpower, currspeed, currfreq)
        for p in [
                coord.split(",") for coord in strokestr.split(" ")
                if len(coord) > 0
        ]:
            polyline.addVertex(Vec2(int(p[0]), int(p[1])))
        polylines.append(polyline)
        lastpos = polyline.vertices[len(polyline.vertices) - 1]