예제 #1
0
 def test_circle_intersection_none(self):
     """Tests circle line intersection none case."""
     intersections = ChaseWaypointGenerator._circle_intersection(
         (-5, 0),
         (5, 0),
         (0, -10),
         9
     )
     self.assertEqual(len(intersections), 0)
예제 #2
0
    def test_circle_intersection_dual(self):
        """Tests circle line intersection two points case."""

        def _almost_equal_set_of_points(list_1, list_2, offset):
            """Tests a set of points for approximate equality."""
            for point_1 in list_1:
                matched = False
                for point_2 in list_2:
                    point_2 = self._add(point_2, offset)
                    if (
                            abs(point_1[0] - point_2[0]) < 0.00001
                            and abs(point_1[1] - point_2[1]) < 0.00001
                    ):
                        matched = True
                        break
                if not matched:
                    return False
            return True

        for p_1, p_2, circle, radius, expected in (
                # Vertical
                ((0, -10), (0, 10), (0, 0), 1, ((0, -1), (0, 1))),
                # Horizontal
                ((-3, 0), (300, 0), (0, 0), 2, ((2, 0), (-2, 0))),
        ):
            for offset in self.OFFSETS:
                intersections = ChaseWaypointGenerator._circle_intersection(
                    self._add(p_1, offset),
                    self._add(p_2, offset),
                    self._add(circle, offset),
                    radius
                )
                self.assertEqual(len(intersections), 2)
                self.assertTrue(
                    _almost_equal_set_of_points(
                        intersections,
                        expected,
                        offset
                    ),
                    'Calculated {}, expected {}'.format(intersections, expected)
                )

                intersections = ChaseWaypointGenerator._circle_intersection(
                    self._add(p_1, offset),
                    self._add(p_2, offset),
                    self._add(circle, offset),
                    radius
                )
                self.assertEqual(len(intersections), 2)
                self.assertTrue(
                    _almost_equal_set_of_points(
                        intersections,
                        expected,
                        offset
                    ),
                    'Calculated {}, expected {}'.format(intersections, expected)
                )

        # Diagonal
        # This case is degenerate itself, because it relies on floating point
        p_1, p_2, circle, radius, expected = (
            (-2, 0), (0, 2), (0, 0), math.sqrt(2), (-1, 1)
        )
        intersections = ChaseWaypointGenerator._circle_intersection(
            p_1,
            p_2,
            circle,
            radius
        )
        self.assertGreaterEqual(len(intersections), 1)
        for intersection in intersections:
            for value, expected_value in zip(intersection, expected):
                self.assertAlmostEqual(value, expected_value)