-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboxerproc.go
40 lines (35 loc) · 1.14 KB
/
boxerproc.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
package graphics2d
// BoxerProc is a path processor that converts a path into a set of boxes along the path with the
// specified width. If the offset is 0 then the box is centered on the path.
type BoxerProc struct {
Width float64 // Width of box
Offs float64 // Offset from path of box
Flat float64 // Flatten value
}
// NewBoxerProc returns a new BoxerProc path processor.
func NewBoxerProc(width, offs float64) *BoxerProc {
return &BoxerProc{width, offs, RenderFlatten}
}
// Process implements the PathProcessor interface.
func (bp *BoxerProc) Process(p *Path) []*Path {
paths := []*Path{}
hw := bp.Width / 2
for _, part := range p.Flatten(bp.Flat).Parts() {
paths = append(paths, Polygon(box(part[0], part[1], hw, bp.Offs)...))
}
return paths
}
// Convert start and end points into a set of box corners.
func box(p1, p2 []float64, hw, offs float64) [][]float64 {
dx, dy := p2[0]-p1[0], p2[1]-p1[1]
nx, ny := unit(-dy, dx)
l, r := hw-offs, hw+offs
lnx, lny := l*nx, l*ny
rnx, rny := r*nx, r*ny
return [][]float64{
{p1[0] + rnx, p1[1] + rny},
{p2[0] + rnx, p2[1] + rny},
{p2[0] - lnx, p2[1] - lny},
{p1[0] - lnx, p1[1] - lny},
}
}