-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorpath.go
70 lines (60 loc) · 1.68 KB
/
colorpath.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package colorpath
import (
sm "github.com/flopp/go-staticmaps"
"github.com/fogleman/gg"
"github.com/golang/geo/s2"
"github.com/lucasb-eyer/go-colorful"
)
// implements the map object interface for go-staticmaps for a path object that can
// vary the color of the path along the way
// Point is a coordinate and a color
type Point struct {
s2.LatLng
Color colorful.Color
}
// ColorPath satisfies the map object interface for go-staticmap
type ColorPath struct {
sm.MapObject
Positions []Point
Weight float64
}
// NewColorPath builds a new path with colors
func NewColorPath(weight float64) *ColorPath {
cp := new(ColorPath)
cp.Positions = []Point{}
cp.Weight = weight
return cp
}
// ExtraMarginPixels - to help go-staticmap find render bounds
// its just a line so no padding
func (cp *ColorPath) ExtraMarginPixels() (float64, float64, float64, float64) {
return cp.Weight, cp.Weight, cp.Weight, cp.Weight
}
// Bounds returns the geographical boundary rect (excluding the actual pixel dimensions).
func (cp *ColorPath) Bounds() s2.Rect {
r := s2.EmptyRect()
for _, ll := range cp.Positions {
r = r.AddPoint(ll.LatLng)
}
return r
}
// Draw draws the colorpath in the given graphical context.
func (cp *ColorPath) Draw(gc *gg.Context, trans *sm.Transformer) {
if len(cp.Positions) <= 1 {
return
}
gc.ClearPath()
gc.SetLineWidth(cp.Weight)
gc.SetLineCap(gg.LineCapRound)
gc.SetLineJoin(gg.LineJoinRound)
for i := 1; i < len(cp.Positions); i++ {
gc.SetColor(cp.Positions[i-1].Color)
spx, spy := trans.LatLngToXY(cp.Positions[i-1].LatLng)
epx, epy := trans.LatLngToXY(cp.Positions[i].LatLng)
gc.DrawLine(spx, spy, epx, epy)
if i%2 == 0 {
gc.Stroke()
}
}
gc.Stroke()
}