-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshapeprocess.go
More file actions
74 lines (63 loc) · 1.43 KB
/
shapeprocess.go
File metadata and controls
74 lines (63 loc) · 1.43 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
package graphics2d
import "math/rand"
// General purpose interface that takes a shape and turns it into
// a slice of shapes.
// ShapeProcessor defines the interface required for function passed to the Process function in Shape.
type ShapeProcessor interface {
Process(s *Shape) []*Shape
}
// PathsProc converts each path in a shape into its own shape.
type PathsProc struct{}
// Process implements the ShapeProcessor interface.
func (pp PathsProc) Process(s *Shape) []*Shape {
paths := s.Paths()
shapes := make([]*Shape, len(paths))
for i, path := range paths {
shapes[i] = NewShape(path)
}
return shapes
}
// BucketProc aggregates paths into N shapes using the specificed style.
type BucketProc struct {
N int
Style BucketStyle
}
type BucketStyle int
const (
Chunk BucketStyle = iota
RoundRobin
Random
)
// Process implements the ShapeProcessor interface.
func (bp BucketProc) Process(s *Shape) []*Shape {
shapes := make([]*Shape, bp.N)
for i, _ := range shapes {
shapes[i] = &Shape{}
}
paths := s.Paths()
np := len(paths)
b := 0
switch bp.Style {
case Chunk:
npb := (np + bp.N - 1) / bp.N
for i, path := range paths {
shapes[b].AddPaths(path)
if i > 0 && i%npb == 0 {
b++
}
}
case RoundRobin:
for _, path := range paths {
shapes[b].AddPaths(path)
b++
if b == bp.N {
b = 0
}
}
case Random:
for _, path := range paths {
shapes[rand.Intn(bp.N)].AddPaths(path)
}
}
return shapes
}