-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpath.go
134 lines (110 loc) · 2.59 KB
/
path.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2012 The geom Authors. All rights reserved.
// Use of this source code is governed by a license that
// can be found in the LICENSE file.
package geom
import (
"math"
)
type Path struct {
vertices []Coord
bounds Rect
}
func (p *Path) Translate(offset Coord) {
p.bounds.Translate(offset)
for i := range p.vertices {
p.vertices[i].Translate(offset)
}
}
func (p *Path) Rotate(rad float64) {
for i := range p.vertices {
p.vertices[i].Rotate(rad)
}
p.bounds = Rect{p.vertices[0], p.vertices[0]}
p.bounds.ExpandToContain(CoordChan(p.vertices[1:]))
}
func (p *Path) Scale(xf, yf float64) {
for i := range p.vertices {
p.vertices[i].Scale(xf, yf)
}
p.bounds.Scale(xf, yf)
}
func (p *Path) Clone() (op *Path) {
op = &Path{}
op.bounds = p.bounds
op.vertices = append([]Coord{}, p.vertices...)
return
}
//uncomment to check interface fulfillment
//var _ Bounded = &Path{}
func (p *Path) Equals(oi interface{}) bool {
o, ok := oi.(*Path)
if !ok {
return false
}
if len(p.vertices) != len(o.vertices) {
return false
}
for i := range p.vertices {
if !p.vertices[i].EqualsCoord(o.vertices[i]) {
return false
}
}
return true
}
func (p *Path) Register(op *Path) (offset Coord, match bool) {
offset = p.bounds.Min.Minus(op.bounds.Min)
if len(p.vertices) != len(op.vertices) {
dbg("registure failure: wrong counts")
return // with match = false
}
for i := range p.vertices {
if !p.vertices[i].EqualsCoord(op.vertices[i].Plus(offset)) {
dbg("register failure: v1=%v v2=%v offset=%v", p.vertices[i], op.vertices[i], offset)
return // with match = false
}
}
match = true
return
}
func (p *Path) Length() int {
return len(p.vertices)
}
func (p *Path) AddVertex(v Coord) {
if len(p.vertices) == 0 {
p.bounds = Rect{
Min: v,
Max: v,
}
} else {
p.bounds.ExpandToContainCoord(v)
}
p.vertices = append(p.vertices, v)
}
func (p *Path) InsertVertexAfter(v Coord, index int) {
p.vertices = append(p.vertices, v)
copy(p.vertices[index+1:], p.vertices[index:len(p.vertices)-1])
p.vertices[index] = v
}
func (p *Path) Bounds() (bounds *Rect) {
return &p.bounds
}
func (p *Path) Vertices() (v []Coord) {
v = p.vertices
return
}
func (me *Path) Error(other *Path) (offset Coord, error float64) {
meCenter := me.bounds.Center()
oCenter := other.bounds.Center()
offset = meCenter.Minus(oCenter)
if len(me.vertices) != len(other.vertices) {
error = math.Inf(1)
return
}
for i, mv := range me.vertices {
ov := other.vertices[i]
offsetMe := mv.Minus(meCenter)
offsetOther := ov.Minus(oCenter)
error += offsetMe.DistanceFrom(offsetOther)
}
return
}