コード例 #1
0
    def test_is_line_segment_intersected_horizontal_line(self):
        """Verifies that the _is_line_segment_intersected function behaves
        correctly for horizontal lines.
        """
        # The ray overlaps the the line segment, no intersection occurs here.
        #
        #          X--------->A------B
        #
        a1 = (10.0, 10.0)
        b1 = (20.0, 10.0)
        x1 = (5.0, 10.0)
        self.assertFalse(
            is_line_segment_intersected(a1, b1, x1),
            "Expected is_line_segment_intersected to return False. Line  "
            "segment starts at %s and ends at %s, and the ray in the direction "
            "<1, 0> starts at %s" % (a1, b1, x1))

        # Non-special case for horizontal line segment non-intersection.
        #
        #          X--------->
        #
        #                     A-------B
        a2 = (10.0, 10.0)
        b2 = (20.0, 10.0)
        x2 = (3.0, 12.0)
        self.assertFalse(
            is_line_segment_intersected(a2, b2, x2),
            "Expected is_line_segment_intersected to return False. Line  "
            "segment starts at %s and ends at %s, and the ray in the direction "
            "<1, 0> starts at %s" % (a2, b2, x2))
コード例 #2
0
    def test_is_line_segment_intersected_non_horizontal_or_vertical_line(self):
        """Verifies that the _is_line_segment_intersected function behaves
        correctly for non-horizontal and non-vertical lines.
        """
        # Special case: Vertex is intersected, this is expected to count
        # as an intersection because the non-intersected vertex is below
        # the intersected vertex.
        #
        #       X---------> A
        #                    \
        #                     \
        #                      \
        #                       B
        a1 = (10.0, 10.0)
        b1 = (20.0, 2.0)
        x1 = (5.0, 10.0)
        self.assertTrue(
            is_line_segment_intersected(a1, b1, x1),
            "Expected is_line_segment_intersected to return True. Line  "
            "segment starts at %s and ends at %s, and the ray in the direction "
            "<1, 0> starts at %s" % (a1, b1, x1))

        # Non-special case for non-horizontal/vertical line segment intersection.
        #
        #                   A
        #                    \
        #          X--------->\
        #                      \
        #                       B
        a2 = (10.0, 10.0)
        b2 = (20.0, 2.0)
        x2 = (5.0, 5.0)
        self.assertTrue(
            is_line_segment_intersected(a2, b2, x2),
            "Expected is_line_segment_intersected to return True. Line  "
            "segment starts at %s and ends at %s, and the ray in the direction "
            "<1, 0> starts at %s" % (a2, b2, x2))

        # Special case for non-horizontal/vertical line segment intersection.
        #
        #                   A
        #                    \
        #          X--------->\
        #                      \
        #                       B
        a2 = (10.0, 10.0)
        b2 = (20.0, 2.0)
        x2 = (5.0, 5.0)
        self.assertTrue(
            is_line_segment_intersected(a2, b2, x2),
            "Expected is_line_segment_intersected to return True. Line  "
            "segment starts at %s and ends at %s, and the ray in the direction "
            "<1, 0> starts at %s" % (a2, b2, x2))

        # Special case for non-horizontal/vertical line segment intersection
        # (ray/vertex intersection).
        #
        #                   A
        #                    \
        #                     \
        #                      \
        #            X--------->B
        a3 = (10.0, 10.0)
        b3 = (20.0, 2.0)
        x3 = (5.0, 2.0)
        self.assertFalse(
            is_line_segment_intersected(a3, b3, x3),
            "Expected is_line_segment_intersected to return False. Line  "
            "segment starts at %s and ends at %s, and the ray in the direction "
            "<1, 0> starts at %s" % (a3, b3, x3))
コード例 #3
0
    def test_is_line_segment_intersected_vertical_line(self):
        """Verifies that the _is_line_segment_intersected function behaves
        correctly for vertical lines.
        """
        # Special case for crossing number algorithm: Ray intersects a vertex
        # of the polygon. To resolve this special case, we only consider a
        # ray/vertex intersection to occur if the other vertex in the line
        # segment has the same vertical position or it's below the intersected
        # vertex. In the case below, we expect this to count as a line segment
        # intersection.
        #
        #         X---------->A
        #                     |
        #                     |
        #                     |
        #                     B
        a1 = (10.0, 10.0)
        b1 = (10.0, 5.0)
        x1 = (3.0, 10.0)
        self.assertTrue(
            is_line_segment_intersected(a1, b1, x1),
            "Expected is_line_segment_intersected to return True for "
            "the intersection with a vertex on a vertical line (where the"
            "non-intersected vertex lies below the intersected vertex)."
            "Line segment starts at %s and ends at %s, and the ray in the "
            "direction <1, 0> starts at %s" % (a1, b1, x1))

        # Opposite case as described above, here we do not expect this case
        # to count as an intersection.
        #
        #                     A
        #                     |
        #                     |
        #                     |
        #          X--------->B
        a2 = (10.0, 10.0)
        b2 = (10.0, 5.0)
        x2 = (3.0, 5.0)
        self.assertFalse(
            is_line_segment_intersected(a2, b2, x2),
            "Expected is_line_segment_intersected to return False for "
            "the intersection with a vertex on a vertical line (where the"
            "non-intersected vertex lies above the intersected vertex)."
            "Line segment starts at %s and ends at %s, and the ray in the "
            "direction <1, 0> starts at %s" % (a2, b2, x2))

        # Non-special case for vertical line segment intersection.
        #
        #                     A
        #                     |
        #          X--------->|
        #                     |
        #                     B
        a3 = (10.0, 10.0)
        b3 = (10.0, 5.0)
        x3 = (3.0, 7.0)
        self.assertTrue(
            is_line_segment_intersected(a3, b3, x3),
            "Expected is_line_segment_intersected to return True. Line segment "
            "starts at %s and ends at %s, and the ray in the direction <1, 0> "
            "starts at %s" % (a3, b3, x3))

        # Non-special case for vertical line segment non-intersection.
        #
        #          X--------->
        #
        #                     A
        #                     |
        #                     |
        #                     |
        #                     B
        a4 = (10.0, 10.0)
        b4 = (10.0, 5.0)
        x4 = (3.0, 12.0)
        self.assertFalse(
            is_line_segment_intersected(a4, b4, x4),
            "Expected is_line_segment_intersected to return False. Line  "
            "segment starts at %s and ends at %s, and the ray in the direction "
            "<1, 0> starts at %s" % (a4, b4, x4))