Exemplo n.º 1
0
 def test_interval_intersection(self):
     for args, answer in [
             ((0, 0, 0, 1, 0, 0, 1, 0), True),
             ((0, 0.5, 0, 1, 0.5, 0, 1, 0), False),
             ((0, 1, 1, 0, 0, 0, 1, 1), True),
             ((0, 1, 0.25, 0.75, 0, 0, 1, 1), False),
             ((0, 0, 1, 1, 0, 1, 1, 1.0004), False),
             ((0, 0, 1, 1, 0, 1, 1, 0.999), True),
             ((0, 0, 1, 1, 0, 1, 0.99, 1), False),
             ((0, 0, 0, 1, 1, 0, 1, 1), False),
     ]:
         self.assertEqual(
             geometry.are_intervals_intersect(*args),
             answer
         )
Exemplo n.º 2
0
def cross_boundaries(tank, world):
    tank_borders = utils.get_borders(tank)

    for world_border in utils.get_world_borders():
        for tank_border in tank_borders:
            x1, y1, x2, y2 = tank_border
            if geometry.are_intervals_intersect(x1, y1, x2, y2, *world_border):
                return True

    def can_cross(unit):
        unit_size = math.hypot(unit.width / 2., unit.height / 2.)
        tank_size = math.hypot(tank.width / 2., tank.height / 2.)
        return unit_size + tank_size > math.hypot(unit.x - tank.x, unit.y - tank.y)

    # for unit in world.obstacles:
    # for unit in world.obstacles + utils.other_tanks(world, tank):
    for unit in filter(lambda u: can_cross(u), world.obstacles + utils.other_tanks(world, tank)):
        for border in utils.get_borders(unit):
            for tank_border in tank_borders:
                x1, y1, x2, y2 = tank_border
                if geometry.are_intervals_intersect(x1, y1, x2, y2, *border):
                    return True

    return False