GEOM-P2

Do two tracks meet?

Practice 2 of 4Computational geometry17 tests

The problem

Two straight pieces of track run from a to b and from c to d. Return 0 if they have no point in common, 1 if they have exactly one, and 2 if they share more than one point (they lie on one line and overlap along a stretch).

meet((0, 0), (4, 4), (0, 4), (4, 0))   # 1: they cross at (2, 2)
meet((0, 0), (2, 2), (2, 2), (4, 0))   # 1: a shared end
meet((0, 0), (3, 3), (1, 1), (5, 5))   # 2: they overlap
meet((0, 0), (1, 1), (2, 2), (3, 3))   # 0: one line, apart

A piece may be a single point (both its ends equal). Pieces may be upright. Coordinates can be as large as 10¹⁷, where floats are not exact.

Builds on GEOM-2 Orientation and intersection.

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.

17 tests, not run yet
Test results
#TestResultReads · time
1A crossingNot run
2ApartNot run
3The lines cross, the pieces do notNot run
4ParallelNot run
5Touching endsNot run
6An end on the middleNot run
7Almost touchingNot run
8On one line, apartNot run
9On one line, end to endNot run
10On one line, overlappingNot run
11One inside the otherNot run
12Upright and overlappingNot run
13Upright and apartNot run
14A single point on a pieceNot run
15Two single pointsNot run
16A point off the lineNot run
17Far from floatsNot run