-
Notifications
You must be signed in to change notification settings - Fork 19
/
pair.go
52 lines (49 loc) · 916 Bytes
/
pair.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package terrarium
type pair struct {
A, B Point
}
func pairsToPaths(pairs []pair) []Path {
paths := make([]Path, len(pairs))
for i, p := range pairs {
paths[i] = Path{p.A, p.B}
}
return paths
}
func joinPairs(pairs []pair) []Path {
// return pairsToPaths(pairs)
lookup := make(map[Point][]Point, len(pairs))
for _, pair := range pairs {
lookup[pair.A] = append(lookup[pair.A], pair.B)
}
var result []Path
for len(lookup) > 0 {
var p Point
for p = range lookup {
break
}
var path Path
for {
path = append(path, p)
if qs, ok := lookup[p]; ok {
q, a := qs[0], qs[1:]
if len(a) == 0 {
delete(lookup, p)
} else {
lookup[p] = a
}
p = q
} else {
break
}
}
// if len(path) < 10 {
// a := path[0]
// b := path[len(path)-1]
// if a.Distance(b) < 1e-6 {
// continue
// }
// }
result = append(result, path)
}
return result
}