GEOM-P4

Any two tracks crossing?

Practice 4 of 4Computational geometry13 tests

The problem

A map has straight pieces of track, each given by its two ends. Two pieces meet if they share any point: they cross, touch, share an end or overlap. Return the positions (i, j) of any two different pieces that meet, or None if no two meet. When several pairs meet, any one of them is accepted; the tests check that the two pieces you name really share a point.

any_crossing([((0, 0), (4, 4)), ((0, 4), (4, 0))])   # (0, 1)
any_crossing([((0, 0), (1, 0)), ((2, 0), (3, 0))])   # None
any_crossing([((0, 0), (2, 2)), ((2, 2), (4, 0))])   # (0, 1)

Each piece has two different ends, given in either order. Pieces may be upright. Coordinates are whole numbers up to about 3 million.

Limits

  • Up to 20,002 pieces. The big tests have 4 seconds each.
  • Checking every pair is 200 million pair checks, far too slow.

Builds on GEOM-4 Sweep line.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

13 tests, not run yet
Test results
#TestResultReads · time
1An XNot run
2ApartNot run
3One pieceNot run
4Touching endsNot run
5Neighbours after a removalNot run
6An upright pieceNot run
7Upright pieces on one lineNot run
8Upright pieces that touchNot run
9A shared startNot run
10Stacked, no crossingNot run
11Overlapping on one lineNot run
1220,000 pieces, none meetNot run
13A crossing at the far endNot run