Debian Codegraph @

vendor/github.com/xyproto/gfx/polyline.go

raw
1 package gfx
2
3 import "math"
4
5 // Polyline is a slice of polygons forming a line.
6 type Polyline []Polygon
7
8 // NewPolyline constructs a slice of line polygons.
9 func NewPolyline(p Polygon, t float64) Polyline {
10 l := len(p)
11
12 if l < 2 {
13 return []Polygon{}
14 }
15
16 var pl Polyline
17
18 for i := range p[:l-1] {
19 pl = append(pl, newLinePolygon(p[i], p[i+1], t))
20 }
21
22 return pl
23 }
24
25 func polylineFromTo(from, to Vec, t float64) Polygon {
26 return NewPolyline(Polygon{from, to}, t)[0]
27 }
28
29 func newLinePolygon(from, to Vec, t float64) Polygon {
30 a := from.To(to).Angle()
31
32 return Polygon{
33 V(from.X+t*math.Cos(a+math.Pi/2), from.Y+t*math.Sin(a+math.Pi/2)),
34 V(from.X+t*math.Cos(a-math.Pi/2), from.Y+t*math.Sin(a-math.Pi/2)),
35
36 V(to.X+t*math.Cos(a-math.Pi/2), to.Y+t*math.Sin(a-math.Pi/2)),
37 V(to.X+t*math.Cos(a+math.Pi/2), to.Y+t*math.Sin(a+math.Pi/2)),
38 }
39 }