-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpolyline.go
39 lines (28 loc) · 846 Bytes
/
polyline.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
package gfx
import "math"
// Polyline is a slice of polygons forming a line.
type Polyline []Polygon
// NewPolyline constructs a slice of line polygons.
func NewPolyline(p Polygon, t float64) Polyline {
l := len(p)
if l < 2 {
return []Polygon{}
}
var pl Polyline
for i := range p[:l-1] {
pl = append(pl, newLinePolygon(p[i], p[i+1], t))
}
return pl
}
func polylineFromTo(from, to Vec, t float64) Polygon {
return NewPolyline(Polygon{from, to}, t)[0]
}
func newLinePolygon(from, to Vec, t float64) Polygon {
a := from.To(to).Angle()
return Polygon{
V(from.X+t*math.Cos(a+math.Pi/2), from.Y+t*math.Sin(a+math.Pi/2)),
V(from.X+t*math.Cos(a-math.Pi/2), from.Y+t*math.Sin(a-math.Pi/2)),
V(to.X+t*math.Cos(a-math.Pi/2), to.Y+t*math.Sin(a-math.Pi/2)),
V(to.X+t*math.Cos(a+math.Pi/2), to.Y+t*math.Sin(a+math.Pi/2)),
}
}