12345678910111213141516171819202122232425262728293031323334353637383940 |
- func isSelfCrossing(x []int) bool {
- // 3 situations for self crossing:
- // 1. _____ 0 intersects 3, x[2] <= x[0] && x[1] <= x[3]
- // | 1 |
- // 2| |0
- // `-----+-
- // 3 |
- //
- // 2. _____ 0 intersects (and coincides) 4,
- // | 1 | x[1] == x[3] && x[2] <= x[0] + x[4]
- // 2| |0
- // | |
- // |_____^4
- // 3
- //
- // 3. ___ 0 intersects 5,
- // | 1 | 5 x[4] <= x[2] && x[1] <= x[3] && x[2] <= x[0] + x[4] && x[3] <= x[1] + x[5]
- // | <-+---.
- // 2| |0 |4
- // |_______|
- // 3
- //
- n := len(x)
- if n < 4 {
- return false
- }
- for i := 3; i < n; i++ {
- if x[i-1] <= x[i-3] && x[i-2] <= x[i] {
- return true
- }
- if 4 <= i && x[i-3] == x[i-1] && x[i-2] <= x[i-4]+x[i] {
- return true
- }
- if 5 <= i && x[i-1] <= x[i-3] && x[i-4] <= x[i-2] && x[i-3] <= x[i-5]+x[i-1] && x[i-2] <= x[i-4]+x[i] {
- return true
- }
- }
- return false
- }
|