-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqrwproc.go
More file actions
66 lines (58 loc) · 1.81 KB
/
sqrwproc.go
File metadata and controls
66 lines (58 loc) · 1.81 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
package graphics2d
// SquareWaveProc applies a square wave along a path with a defined wave length and amplitude.
// The wave starts and ends on zero-crossing points and the last half wave is truncated to the
// path length remaining. The internal zero-crossing points can be optionally preserved.
type SquareWaveProc struct {
HalfLambda float64 // Half wave length
Scale float64 // Ratio of amplitude to lambda
KeepZero bool // Keeps internal zero-point crossings if set
Flip bool // Flips the wave phase by 180 (pi) if set
}
// NewSquareWaveProc creates a new SquareWaveProc with the supplied wave length and amplitude.
func NewSquareWaveProc(lambda, amplitude float64) SquareWaveProc {
return SquareWaveProc{lambda / 2, amplitude / lambda, false, false}
}
// Process implements the PathProcessor interface.
func (sp SquareWaveProc) Process(p *Path) []*Path {
// Chunk up path into pieces
pp1 := NewMunchProc(sp.HalfLambda).Process(p)
p1, _ := ConcatenatePaths(pp1...)
n := len(p1.steps)
last := p1.steps[0][0]
path := NewPath(last)
left := !sp.Flip
for i := 1; i < n; i++ {
// Each step is a half wave
cur := p1.steps[i][0]
dx, dy := cur[0]-last[0], cur[1]-last[1]
ndx, ndy := dy*sp.Scale, -dx*sp.Scale
if left {
ndx, ndy = -ndx, -ndy
}
path.AddStep([]float64{last[0] + ndx, last[1] + ndy})
path.AddStep([]float64{cur[0] + ndx, cur[1] + ndy})
path.AddStep([]float64{cur[0], cur[1]})
last = cur
left = !left
}
if sp.KeepZero {
if p.Closed() {
path.Close()
}
return []*Path{path}
}
// Filter out internal zero-crossing points
n = len(path.steps)
fpath := NewPath(path.steps[0][0])
for i := 1; i < n-1; i++ {
if i%3 == 0 {
continue
}
fpath.AddStep(path.steps[i][0])
}
fpath.AddStep(path.steps[n-1][0])
if p.Closed() {
fpath.Close()
}
return []*Path{fpath}
}