forked from andybalholm/stroke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstroke.go
184 lines (166 loc) · 5.06 KB
/
stroke.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package stroke
import (
"math"
)
// strokeContour strokes a single contour (connected series of segments). If c
// is closed, it returns both the outer contour of the stroke and the inner
// one (the outline of the hole in the middle). Otherwise inner is nil.
func strokeContour(c []Segment, opt Options) (outer, inner []Segment) {
halfWidth := opt.Width / 2
if !isCCW(c) {
c = reversePath(c)
}
for _, s := range c {
// Skip segments that don't do anything.
if s.CP1 == s.Start && s.CP2 == s.Start && s.End == s.Start {
continue
}
right, left := offsetCurves(s, halfWidth)
if len(outer) > 0 && outer[len(outer)-1].End != right[0].Start {
j := join(outer[len(outer)-1].End, right[0].Start, s.Start, opt)
outer = append(outer, j...)
}
outer = append(outer, right...)
if len(inner) > 0 && inner[len(inner)-1].End != left[0].Start {
j := join(left[0].Start, inner[len(inner)-1].End, s.Start, opt)
inner = append(inner, reversePath(j)...)
}
inner = append(inner, left...)
}
if c[0].Start == c[len(c)-1].End {
// The path was closed, so we'll return two separate contours.
// But we need to draw a join first.
j := join(outer[len(outer)-1].End, outer[0].Start, c[0].Start, opt)
outer = append(outer, j...)
j = join(inner[0].Start, inner[len(inner)-1].End, c[0].Start, opt)
inner = append(inner, reversePath(j)...)
return outer, reversePath(inner)
} else {
// Cap the ends and combine into one contour.
switch opt.Cap {
default:
// FlatCap or invalid value
outer = append(outer, LinearSegment(outer[len(outer)-1].End, inner[len(inner)-1].End))
outer = append(outer, reversePath(inner)...)
outer = append(outer, LinearSegment(inner[0].Start, outer[0].Start))
case RoundCap:
cp := roundCap(outer[len(outer)-1].End, inner[len(inner)-1].End)
outer = append(outer, cp[:]...)
outer = append(outer, reversePath(inner)...)
cp = roundCap(inner[0].Start, outer[0].Start)
outer = append(outer, cp[:]...)
case SquareCap:
cp := squareCap(outer[len(outer)-1].End, inner[len(inner)-1].End)
outer = append(outer, cp[:]...)
outer = append(outer, reversePath(inner)...)
cp = squareCap(inner[0].Start, outer[0].Start)
outer = append(outer, cp[:]...)
}
return outer, nil
}
}
func roundCap(p1, p2 Point) [2]Segment {
const k = 0.551784777779014
half := p2.Sub(p1).Mul(0.5)
tip := p1.Add(half).Add(rot90CW(half))
return [2]Segment{
{p1, p1.Add(rot90CW(half).Mul(k)), tip.Sub(half.Mul(k)), tip},
{tip, tip.Add(half.Mul(k)), p2.Add(rot90CW(half).Mul(k)), p2},
}
}
func squareCap(p1, p2 Point) [3]Segment {
half := p2.Sub(p1).Mul(0.5)
offset := rot90CW(half)
return [3]Segment{
LinearSegment(p1, p1.Add(offset)),
LinearSegment(p1.Add(offset), p2.Add(offset)),
LinearSegment(p2.Add(offset), p2),
}
}
// Stroke returns outlines for the contours in path. Both in the parameter and
// in the return value, each element of the slice is a contour (a connected
// series of segments).
func Stroke(path [][]Segment, opt Options) [][]Segment {
var result [][]Segment
for _, c := range path {
outer, inner := strokeContour(c, opt)
result = append(result, outer)
if inner != nil {
result = append(result, inner)
}
}
return result
}
type CapStyle int
const (
FlatCap CapStyle = 0
RoundCap = 1
SquareCap = 2
)
type JoinStyle int
const (
MiterJoin JoinStyle = 0
RoundJoin = 1
BevelJoin = 2
)
type Options struct {
Width float32
Cap CapStyle
Join JoinStyle
MiterLimit float32
}
// isCCW returns whether c is counter-clockwise.
func isCCW(c []Segment) bool {
// Use the shoelace formula:
// https://en.wikipedia.org/wiki/Shoelace_formula
var area float32
for _, s := range c {
area += (s.End.X - s.Start.X) * (s.End.Y + s.Start.Y)
}
return area < 0
}
// join draws a corner join from start to end, with the style specified by opt.
// (center is the center of the corner; i.e. the corner of the path being
// stroked.)
func join(start, end, center Point, opt Options) []Segment {
style := opt.Join
angle := math.Atan2(float64(start.X-center.X), float64(start.Y-center.Y)) -
math.Atan2(float64(end.X-center.X), float64(end.Y-center.Y))
switch {
case angle > math.Pi:
angle -= 2 * math.Pi
case angle < -math.Pi:
angle += 2 * math.Pi
}
// If it's an inside corner, always do a bevel join, since it's the simplest,
// and it won't show anyway.
if angle < 0 {
style = BevelJoin
}
if style == MiterJoin {
phi := math.Pi - angle
miterRatio := float32(1 / math.Sin(phi/2))
if miterRatio > opt.MiterLimit {
style = BevelJoin
} else {
direction := rot90CW(end.Sub(start).Div(distance(end, start)))
dist := distance(start, center) * miterRatio
tip := center.Add(direction.Mul(dist))
return []Segment{
LinearSegment(start, tip),
LinearSegment(tip, end),
}
}
}
if style == RoundJoin {
k := float32(math.Tan(angle/4)) * 4 / 3
cp1 := start.Add(rot90CW(center.Sub(start)).Mul(k))
cp2 := end.Add(rot90CW(end.Sub(center)).Mul(k))
return []Segment{
{start, cp1, cp2, end},
}
}
return []Segment{
LinearSegment(start, end),
}
}