forked from rubenv/topojson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquantize.go
More file actions
70 lines (56 loc) · 1.2 KB
/
quantize.go
File metadata and controls
70 lines (56 loc) · 1.2 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package topojson
import "math"
type quantize struct {
Transform *Transform
dx, dy, kx, ky float64
}
func newQuantize(dx, dy, kx, ky float64) *quantize {
return &quantize{
dx: dx,
dy: dy,
kx: kx,
ky: ky,
Transform: &Transform{
Scale: [2]float64{1 / kx, 1 / ky},
Translate: [2]float64{-dx, -dy},
},
}
}
func (q *quantize) quantizePoint(p []float64) []float64 {
x := round((p[0] + q.dx) * q.kx)
y := round((p[1] + q.dy) * q.ky)
return []float64{x, y}
}
func (q *quantize) quantizeLine(in [][]float64, skipEqual bool) [][]float64 {
out := make([][]float64, 0, len(in))
var last []float64
for _, p := range in {
pt := q.quantizePoint(p)
if !pointEquals(pt, last) || !skipEqual {
out = append(out, pt)
last = pt
}
}
if len(out) < 2 {
out = append(out, out[0])
}
return out
}
func (q *quantize) quantizeMultiLine(in [][][]float64, skipEqual bool) [][][]float64 {
out := make([][][]float64, len(in))
for i, line := range in {
line = q.quantizeLine(line, skipEqual)
for len(line) < 4 {
line = append(line, line[0])
}
out[i] = line
}
return out
}
func round(v float64) float64 {
if v < 0 {
return math.Ceil(v - 0.5)
} else {
return math.Floor(v + 0.5)
}
}