Exemplo n.º 1
0
def test_reversed_coordinates(segments: List[Segment]) -> None:
    result = segments_intersections(segments)

    reversed_result = segments_intersections(
        reverse_segments_coordinates(segments))
    assert result == {
        ids_pair: tuple(
            sorted(
                reverse_point_coordinates(endpoint) for endpoint in endpoints))
        for ids_pair, endpoints in reversed_result.items()
    }
def get_intersections(segments):
    """
    list of segments -> dictionary of {intersection point:segment indexes}
    :param segments: list of segments of form of form list<((x1, y1), (x2, y2))>
    :return: dictionary of points to the segments that cross it:
    dictionary<(point<Fraction, Fraction>:list<int>)>
    """
    return segments_intersections(segments)
def test_step(context: Context, segments: List[Segment]) -> None:
    *rest_segments, last_segment = segments

    result = segments_intersections(rest_segments)
    next_result = segments_intersections(segments)

    assert (next_result.keys() == (result.keys() | set(
        chain.from_iterable(
            segments_pair_intersections(last_segment.start, last_segment.end,
                                        segment.start, segment.end)
            for segment in rest_segments))))
    assert all(segment_id < next_segment_id == len(segments) - 1
               for point, intersections in next_result.items()
               for segment_id, next_segment_id in (intersections -
                                                   result.get(point, set())))
    assert all(point in segments_pair_intersections(
        segments[segment_id].start, segments[segment_id].end,
        segments[next_segment_id].start, segments[next_segment_id].end)
               for point, intersections in next_result.items()
               for segment_id, next_segment_id in (intersections -
                                                   result.get(point, set())))
def test_basic(segments: List[Segment]) -> None:
    result = segments_intersections(segments)

    assert isinstance(result, dict)
    assert all(is_point(key) for key in result.keys())
    assert all(isinstance(value, set) for value in result.values())
    assert all(
        isinstance(element, tuple) for value in result.values()
        for element in value)
    assert all(
        len(element) == 2 and all(
            isinstance(coordinate, int) for coordinate in element)
        for value in result.values() for element in value)
Exemplo n.º 5
0
def test_step(context: Context, segments: List[Segment]) -> None:
    *rest_segments, last_segment = segments

    result = segments_intersections(rest_segments)
    next_result = segments_intersections(segments)

    assert (next_result.keys() == (
        result.keys()
        | {(index, len(segments) - 1)
           for index, segment in enumerate(rest_segments) if context.
           segments_relation(segment, last_segment) is not Relation.DISJOINT}))
    assert result.items() <= next_result.items()
    assert all(segment_id < next_segment_id == len(segments) - 1
               for segment_id, next_segment_id in (next_result.keys() -
                                                   result.keys()))
    assert all(
        context.segments_intersection(
            segments[segment_id], segments[next_segment_id]) == next_result[(
                segment_id, next_segment_id)][0] if len(next_result[(
                    segment_id,
                    next_segment_id)]) == 1 else context.segments_intersection(
                        segments[segment_id], segments[next_segment_id]) not in
        (Relation.DISJOINT, Relation.TOUCH, Relation.CROSS) and (
            to_sorted_pair(*next_result[(segment_id, next_segment_id)]) ==
            next_result[(segment_id, next_segment_id)]) and all(
                context.segment_contains_point(segments[segment_id], point)
                for point in next_result[(segment_id, next_segment_id)])
        and all(
            context.segment_contains_point(segments[next_segment_id], point)
            for point in next_result[(segment_id, next_segment_id)])
        for segment_id, next_segment_id in (next_result.keys() -
                                            result.keys()))
    assert all(
        context.segments_relation(segments[segment_id],
                                  segments[next_segment_id])
        is not Relation.DISJOINT
        for segment_id, next_segment_id in (next_result.keys() -
                                            result.keys()))
Exemplo n.º 6
0
def test_basic(segments: List[Segment]) -> None:
    result = segments_intersections(segments)

    assert isinstance(result, dict)
    assert all(
        isinstance(key, tuple) and all(
            isinstance(coordinate, int) for coordinate in key)
        for key in result.keys())
    assert all(
        isinstance(value, tuple) and all(
            is_point(coordinate) for coordinate in value)
        for value in result.values())
    assert all(len(key) == 2 for key in result.keys())
    assert all(1 <= len(value) <= 2 for value in result.values())
def test_degenerate_segments(segments: List[Segment]) -> None:
    with pytest.raises(ValueError):
        segments_intersections(segments)
def test_base_case(segments: List[Segment]) -> None:
    result = segments_intersections(segments)

    assert not result
Exemplo n.º 9
0
from bentley_ottmann.planar import segments_intersections
file = open("segments.txt","r")
text = file.read() # good luck with that
segments = []
for line in text.split("\n"):
	try:
		split_str = line.split("    ")
		x1 = float(split_str[0].split(" ")[0])
		y1 = float(split_str[0].split(" ")[1])
		x2 = float(split_str[1].split(" ")[0])
		y2 = float(split_str[1].split(" ")[1])
		segments.append(((x1, y1), (x2, y2)))
	except:
		pass
intersections = segments_intersections(segments)
file = open("output.txt","w")
file.write(str(intersections))