def brute_sweep_intersections(segments):
    start = time()
    counter = 0
    status = {
        "upper": 0,
        "lower": 1,
    }
    points = []
    event_queue = Tree(event_lt)
    status_queue = {}
    for segment in segments:
        event_queue.insert(upper_end(*segment), (status["upper"], segment))
        event_queue.insert(lower_end(*segment), (status["lower"], segment))
    while not event_queue.empty():
        (_, (label, segment)) = event_queue.pop()
        if label == status["upper"]:
            for other in list(status_queue.keys()):
                counter += 1
                point = point_of_intersection(segment, other)
                if point is not None:
                    points.append(point)
            status_queue[segment] = None
        else:
            del status_queue[segment]
    print(results(True, counter, not len(points) == len(set(points)), start))
    return (segments, points)
Example #2
0
 def test_empty(self):
     tree = Tree(lt)
     assert tree.empty()
     assert not self.seed_none(self.xs).empty()
def sweep_intersections(segments):
    start = time()
    counter = 0
    status = {
        "upper": 0,
        "intersection": 1,
        "lower": 2,
    }
    points = []
    memo = {}
    event_queue = Tree(event_lt)
    status_queue = Tree(status_lt)
    for segment in segments:
        event_queue.insert(upper_end(*segment), (status["upper"], segment))
        event_queue.insert(lower_end(*segment), (status["lower"], segment))
    while not event_queue.empty():
        (event, (label, payload)) = event_queue.pop()
        (_, y) = event
        if label == status["upper"]:
            memo[payload] = event
            status_queue.insert((event, payload), None)
            (left, right) = status_queue.neighbors((event, payload))
            if left is not None:
                ((_, left_segment), _) = left
                counter += 1
                update_points(
                    event_queue,
                    y,
                    left_segment,
                    payload,
                    status["intersection"],
                )
            if right is not None:
                ((_, right_segment), _) = right
                counter += 1
                update_points(
                    event_queue,
                    y,
                    payload,
                    right_segment,
                    status["intersection"],
                )
        elif label == status["lower"]:
            (left, right) = status_queue.neighbors((memo[payload], payload))
            status_queue.delete((memo[payload], payload))
            if (left is not None) and (right is not None):
                ((_, left_segment), _) = left
                ((_, right_segment), _) = right
                counter += 1
                update_points(
                    event_queue,
                    y,
                    left_segment,
                    right_segment,
                    status["intersection"],
                )
        else:
            points.append(event)
            (right, left) = payload
            status_queue.delete((memo[left], left))
            status_queue.delete((memo[right], right))
            memo[left] = event
            memo[right] = event
            status_queue.insert((memo[left], left), None)
            status_queue.insert((memo[right], right), None)
            (far_left, _) = status_queue.neighbors((memo[left], left))
            (_, far_right) = status_queue.neighbors((memo[right], right))
            if far_left is not None:
                ((_, far_left_segment), _) = far_left
                counter += 1
                update_points(
                    event_queue,
                    y,
                    far_left_segment,
                    left,
                    status["intersection"],
                )
            if far_right is not None:
                ((_, far_right_segment), _) = far_right
                counter += 1
                update_points(
                    event_queue,
                    y,
                    right,
                    far_right_segment,
                    status["intersection"],
                )
    print(results(False, counter, not len(points) == len(set(points)), start))
    return (segments, points)