示例#1
0
    def test_intersect_1(self):
        """
        Base case: no intersection.
        """
        line_0 = Line(0, 0, 10, 0)
        line_1 = Line(1, 1, 100, 100)

        self.assertFalse(do_lines_intersect(line_0, line_1))
示例#2
0
    def test_intersect_3(self):
        """
        Base case: intersect halfaway.
        """
        line_0 = Line(5, 5, 15, 15)
        line_1 = Line(5, 15, 15, 5)

        self.assertTrue(do_lines_intersect(line_0, line_1))
示例#3
0
    def test_intersect_2(self):
        """
        Corner case: just intersect at endpoint.
        """
        line_0 = Line(0, 0, 10, 0)
        line_1 = Line(0, 0, 100, 100)

        self.assertTrue(do_lines_intersect(line_0, line_1))
示例#4
0
    def test_intersect_5(self):
        """
        Base case: part same inf line, but no intersection.
        """
        line_0 = Line(0, 0, 3, 3)
        line_1 = Line(4.3, 4.3, 12, 12)

        self.assertFalse(do_lines_intersect(line_0, line_1))
示例#5
0
    def test_intersect_4(self):
        """
        Base case: collinear, do intersect.
        """
        line_0 = Line(5, 5, 15, 15)
        line_1 = Line(3, 3, 12, 12)

        self.assertTrue(do_lines_intersect(line_0, line_1))
示例#6
0
 def test_check_valid_lines_1(self):
     """
     Base case: Lines fit in size.
     """
     lines = set([Line(0, 3, 3, 0), Line(0, 9, 0, 9), Line(1, 2, 3, 4)])
     size = Size(10, 10)
     # Fails if error is raised.
     layout = MazeLayout(lines, START_DUMMY, END_DUMMY, size)
示例#7
0
    def test_intersect_6(self):
        """
        Base case: collinear, do not intersect.
        """
        line_0 = Line(5, 5, 15, 15)
        line_1 = Line(5, 4, 15, 14)

        self.assertFalse(do_lines_intersect(line_0, line_1))
示例#8
0
    def test_does_ball_hit_wall_5(self):
        """
        Base case: 2 walls, one hits.
        """
        lines = set([Line(6, 9, 9, 2), Line(2, 0, 9, 6)])
        ball = Ball(2, 1, 1)
        layout = MazeLayout(lines, START_DUMMY, END_DUMMY, SIZE_DUMMY)

        assert (layout.does_ball_hit_wall(ball) == True)
示例#9
0
    def test_does_ball_hit_wall_4(self):
        """
        Base case: 2 walls, no hit.
        """
        lines = set([Line(6, 9, 9, 2), Line(2, 0, 9, 6)])
        ball = Ball(11, 12, 1)
        layout = MazeLayout(lines, START_DUMMY, END_DUMMY, SIZE_DUMMY)

        assert (layout.does_ball_hit_wall(ball) == False)
示例#10
0
    def test_dist_lines_1(self):
        """
        Base case: intersecting lines.
        """
        line_0 = Line(1, 1, 2, 2)
        line_1 = Line(1, 2, 2, 1)
        output = dist_line_to_line(line_0, line_1)
        expected = 0.0

        self.assertAlmostEqual(expected, output)
示例#11
0
 def test_collision_wall_1(self):
     """
     Sanity check: No wall hit, minimal distance.
     """
     wall1 = Line(4, 0, 4, 99)
     wall2 = Line(0, 4, 99, 4)
     layout = MazeLayout(set([wall1, wall2]), np.array([2, 2]),
                         np.array([99, 99]), self.size)
     self.model.reset(layout)
     assert (self.model.does_ball_hit_wall() == False)
示例#12
0
    def test_dist_lines_3(self):
        """
        Base case: colinear lines on same infinite line.
        """
        line_0 = Line(0, 0, 2, 2)
        line_1 = Line(6, 6, 5, 5)
        output = dist_line_to_line(line_0, line_1)
        expected = math.sqrt(3**2 + 3**2)

        self.assertAlmostEqual(expected, output)
示例#13
0
    def test_dist_lines_2(self):
        """
        Base case: colinear lines.
        """
        line_0 = Line(0, 1, 1, 2)
        line_1 = Line(0, 0, 1, 1)
        output = dist_line_to_line(line_0, line_1)
        expected = 0.5 * math.sqrt(1**2 + 1**2)

        self.assertAlmostEqual(expected, output)
示例#14
0
    def test_dist_lines_4(self):
        """
        Base case: shortest dist is to endpoint of one of the two.
        """
        # Note: line_0 not noted left-to-right. It should handle this.
        line_0 = Line(5, 4, 2, 1)
        line_1 = Line(1, 6, 10, 6)
        output = dist_line_to_line(line_0, line_1)
        expected = 2

        self.assertAlmostEqual(expected, output)
示例#15
0
    def test_collision_wall_4(self):
        """
        Corner case: hit two walls at once.
        """
        wall1 = Line(4, 0, 4, 99)
        wall2 = Line(0, 4, 99, 4)
        layout = MazeLayout(set([wall1, wall2]), np.array([2, 2]),
                            np.array([99, 99]), self.size)
        self.model.reset(layout)
        self.model.set_acceleration(1, 1)
        # New pos ball becomes (3, 3), and with rad=1 it will touch the walls.
        self.model.make_timestep()

        assert (self.model.does_ball_hit_wall() == True)
示例#16
0
 def test_check_valid_lines_5(self):
     """
     Base case: Lines do not fit in size -- y too big.
     """
     lines = set([
         Line(0, 3, 3, 0),
         Line(0, 9, 0, 11),
         Line(5, 5, 3, 9),
         Line(1, 2, 3, 4)
     ])
     size = Size(10, 10)
     try:
         layout = MazeLayout(lines, START_DUMMY, END_DUMMY, size)
         self.fail()
     except ValueError:
         pass
示例#17
0
    def test_moving_wall_detection_5(self):
        """
        Base case: does not hit wall with center of ball.
        """
        start = np.array([3, 3])
        end = np.array([100, 100])
        walls = set([Line(0, 10, 1, 10)])
        layout = MazeLayout(walls, start, end, self.size)
        self.model.reset(layout)

        self.model.set_acceleration(0, 1)
        self.assertTrue(self.model.make_timestep())  # position becomes (3, 4)
        self.model.set_acceleration(0, 0)

        self.assertTrue(self.model.make_timestep())  # position becomes (3, 5)
        self.assertTrue(self.model.make_timestep())  # position becomes (3, 6)
        self.assertTrue(self.model.make_timestep())  # position becomes (3, 7)
        self.assertTrue(self.model.make_timestep())  # position becomes (3, 8)
        self.assertTrue(self.model.make_timestep())  # position becomes (3, 9)
        self.assertFalse(
            self.model.make_timestep())  # position becomes (3, 10)
        # Next one still hits at start of timestep!
        self.assertFalse(
            self.model.make_timestep())  # position becomes (3, 11)
        self.assertTrue(self.model.make_timestep())
示例#18
0
    def test_moving_wall_detection_2(self):
        """
        Base case: does not collide with a wall.
        """
        start = np.array([5, 5])
        end = np.array([100, 100])
        walls = set([Line(0, 0, 1, 1009), Line(100, 1000, 101, 1002)])
        layout = MazeLayout(walls, start, end, self.size)
        self.model.reset(layout)

        self.model.set_acceleration(1, 1)
        self.assertTrue(self.model.make_timestep())  # position becomes (6, 6)
        self.model.set_acceleration(0, 0)
        for _ in range(self.size.x - self.ball_rad - 1 - 6):
            self.assertTrue(self.model.make_timestep())
        # Should have reached end of rectangle now.
        self.assertFalse(self.model.make_timestep())
示例#19
0
    def test_does_ball_hit_wall_12(self):
        """
        Corner case: Hits, both points of line right of ball center.
        """
        lines = set([Line(100, 200, 100, 0)])
        ball = Ball(99, 100, 3)
        layout = MazeLayout(lines, START_DUMMY, END_DUMMY, SIZE_DUMMY)

        assert (layout.does_ball_hit_wall(ball) == True)
示例#20
0
    def test_does_ball_hit_wall_3(self):
        """
        Corner case: normal ball just hits a wall.
        """
        lines = set([Line(3, 3, 3, 0)])
        ball = Ball(0, 1.5, 3)
        layout = MazeLayout(lines, START_DUMMY, END_DUMMY, SIZE_DUMMY)

        assert (layout.does_ball_hit_wall(ball) == True)
示例#21
0
    def test_does_ball_hit_wall_2(self):
        """
        Base case: normal ball does not hit a wall.
        """
        lines = set([Line(0, 3, 3, 0)])
        ball = Ball(0, 0, 1)
        layout = MazeLayout(lines, START_DUMMY, END_DUMMY, SIZE_DUMMY)

        assert (layout.does_ball_hit_wall(ball) == False)
示例#22
0
    def test_orientation_1(self):
        """
        Base case: simple clockwise triangle.
        """
        line = Line(0, 0, 1, 1)
        p = np.array([2, 0])
        output = compute_orientation_points(line, p)

        self.assertEqual(Orientation.CLOCKWISE, output)
示例#23
0
    def test_orientation_2(self):
        """
        Base case: simple counterclockwise triangle.
        """
        line = Line(0, 0, 1, 0)
        p = np.array([3, 2])
        output = compute_orientation_points(line, p)

        self.assertEqual(Orientation.COUNTERCLOCKWISE, output)
示例#24
0
    def test_does_ball_hit_wall_9(self):
        """
        Corner case: Does not hit but would do so if the line were longer v3.
        """
        lines = set([Line(1, 5, 1, 4)])
        ball = Ball(1, 0, 1)
        layout = MazeLayout(lines, START_DUMMY, END_DUMMY, SIZE_DUMMY)

        assert (layout.does_ball_hit_wall(ball) == False)
示例#25
0
    def test_does_ball_hit_wall_6(self):
        """
        Corner case: endpoints of line are far away.
        """
        lines = set([Line(0, 0, 10, 10)])
        ball = Ball(5, 5, 1)
        layout = MazeLayout(lines, START_DUMMY, END_DUMMY, SIZE_DUMMY)

        assert (layout.does_ball_hit_wall(ball) == True)
示例#26
0
    def test_orientation_3(self):
        """
        Base case: simple collinear line.
        """
        line = Line(0, 0, 5, 5)
        p = np.array([2, 2])
        output = compute_orientation_points(line, p)

        self.assertEqual(Orientation.COLLINEAR, output)
示例#27
0
    def test_orientation_5(self):
        """
        Base case: wide-angle clockwise triangle.
        """
        line = Line(1, 1, 2, 1)
        p = np.array([3, 0])
        output = compute_orientation_points(line, p)

        self.assertEqual(Orientation.CLOCKWISE, output)
示例#28
0
    def test_orientation_6(self):
        """
        Base case: right-angle counterclockwise triangle.
        """
        line = Line(1, 1, 2, 1)
        p = np.array([2, 2])
        output = compute_orientation_points(line, p)

        self.assertEqual(Orientation.COUNTERCLOCKWISE, output)
示例#29
0
    def test_orientation_8(self):
        """
        Corner case: counterclockwise, 2 points same x-val.
        """
        line = Line(1, 1, 1, 2)
        p = np.array([0, 2])
        output = compute_orientation_points(line, p)

        self.assertEqual(Orientation.COUNTERCLOCKWISE, output)
示例#30
0
    def test_does_ball_hit_wall_1(self):
        """
        Base case: normal ball hits normal wall.
        """
        lines = set([Line(0, 3, 3, 0)])
        ball = Ball(0, 0, 2.5)
        layout = MazeLayout(lines, START_DUMMY, END_DUMMY, SIZE_DUMMY)

        assert (layout.does_ball_hit_wall(ball) == True)