Skip to content

Commit

Permalink
Complete 'bentley_ottmann.sweep' function
Browse files Browse the repository at this point in the history
  • Loading branch information
lycantropos committed Mar 26, 2021
1 parent bf810c7 commit d77508d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 10 additions & 6 deletions bentley_ottmann/core/bentley_ottmann.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from itertools import combinations
from itertools import (chain,
product)
from typing import (Iterable,
List,
Optional,
Expand All @@ -11,6 +12,7 @@
from .event import Event
from .events_queue import EventsQueue
from .sweep_line import SweepLine
from .utils import to_pairs_combinations


def sweep(segments: Sequence[Segment],
Expand All @@ -25,13 +27,15 @@ def sweep(segments: Sequence[Segment],
while events_queue:
event = events_queue.pop()
start = event.start
same_start_events = (prev_same_start_events + [event]
if start == prev_start
else [event])
same_start_events = [event]
while events_queue and events_queue.peek().start == start:
same_start_events.append(events_queue.pop())
for event, other_event in combinations(same_start_events,
r=2):
for event, other_event in (
chain(to_pairs_combinations(same_start_events),
product(same_start_events,
prev_same_start_events))
if start == prev_start
else to_pairs_combinations(same_start_events)):
if event.segments_ids is not other_event.segments_ids:
relation = segments_relater(
event.original_start, event.original_end,
Expand Down
6 changes: 6 additions & 0 deletions bentley_ottmann/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from functools import partial
from itertools import combinations
from typing import (Hashable,
Iterable,
Tuple,
Expand Down Expand Up @@ -25,5 +27,9 @@ def pairwise(ids: Iterable[_T]) -> Iterable[Tuple[_T, _T]]:
value = next_value


to_pairs_combinations = partial(combinations,
r=2)


def to_sorted_pair(first: _T, second: _T) -> Tuple[_T, _T]:
return (first, second) if first < second else (second, first)

0 comments on commit d77508d

Please sign in to comment.