-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpointsOnPath.go
More file actions
executable file
·94 lines (83 loc) · 2.23 KB
/
pointsOnPath.go
File metadata and controls
executable file
·94 lines (83 loc) · 2.23 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package gorough
import (
"github.com/NovikovRoman/gorough/data_parser"
)
func PointsOnPath(path string, tolerance float64, distance float64) (out [][]Point, err error) {
var segments []data_parser.Segment
if segments, err = data_parser.ParsePath(path); err != nil {
return
}
var (
sets [][]Point
currentPoints []Point
pendingCurve []Point
)
start := Point{}
normalize := data_parser.Normalize(data_parser.Absolutize(segments))
for _, s := range normalize {
switch s.Key {
case "M":
appendPendingPoints(&sets, ¤tPoints, &pendingCurve, tolerance)
start = Point{
X: s.Data[0],
Y: s.Data[1],
}
currentPoints = append(currentPoints, start)
case "L":
appendPendingCurve(¤tPoints, &pendingCurve, tolerance)
currentPoints = append(currentPoints, Point{
X: s.Data[0],
Y: s.Data[1],
})
case "C":
if len(pendingCurve) == 0 {
lastPoint := start
if len(currentPoints) > 0 {
lastPoint = currentPoints[len(currentPoints)-1]
}
pendingCurve = append(pendingCurve, lastPoint)
}
pendingCurve = append(pendingCurve, Point{
X: s.Data[0],
Y: s.Data[1],
})
pendingCurve = append(pendingCurve, Point{
X: s.Data[2],
Y: s.Data[3],
})
pendingCurve = append(pendingCurve, Point{
X: s.Data[4],
Y: s.Data[5],
})
case "Z":
appendPendingCurve(¤tPoints, &pendingCurve, tolerance)
currentPoints = append(currentPoints, start)
}
}
appendPendingPoints(&sets, ¤tPoints, &pendingCurve, tolerance)
if distance == 0 {
return sets, nil
}
for _, set := range sets {
simplifiedSet := Simplify(set, distance)
if len(simplifiedSet) > 0 {
out = append(out, simplifiedSet)
}
}
return
}
func appendPendingCurve(currentPoints *[]Point, pendingCurve *[]Point, tolerance float64) {
if len(*pendingCurve) >= 4 {
*currentPoints = append(*currentPoints, PointsOnBezierCurves(*pendingCurve, tolerance, 0)...)
}
*pendingCurve = []Point{}
return
}
func appendPendingPoints(sets *[][]Point, currentPoints *[]Point, pendingCurve *[]Point, tolerance float64) {
appendPendingCurve(currentPoints, pendingCurve, tolerance)
if len(*currentPoints) > 0 {
*sets = append(*sets, *currentPoints)
*currentPoints = []Point{}
}
return
}