Debian Codegraph @

drawing/path.go

raw symbols SCIP
1 package drawing
2
3 import (
4 "fmt"
5 "math"
6 )
7
8 // PathBuilder describes the interface for path drawing.
9 type PathBuilder interface {
10 // LastPoint returns the current point of the current sub path
11 LastPoint() (x, y float64)
12 // MoveTo creates a new subpath that start at the specified point
13 MoveTo(x, y float64)
14 // LineTo adds a line to the current subpath
15 LineTo(x, y float64)
16 // QuadCurveTo adds a quadratic Bézier curve to the current subpath
17 QuadCurveTo(cx, cy, x, y float64)
18 // CubicCurveTo adds a cubic Bézier curve to the current subpath
19 CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64)
20 // ArcTo adds an arc to the current subpath
21 ArcTo(cx, cy, rx, ry, startAngle, angle float64)
22 // Close creates a line from the current point to the last MoveTo
23 // point (if not the same) and mark the path as closed so the
24 // first and last lines join nicely.
25 Close()
26 }
27
28 // PathComponent represents component of a path
29 type PathComponent int
30
31 const (
32 // MoveToComponent is a MoveTo component in a Path
33 MoveToComponent PathComponent = iota
34 // LineToComponent is a LineTo component in a Path
35 LineToComponent
36 // QuadCurveToComponent is a QuadCurveTo component in a Path
37 QuadCurveToComponent
38 // CubicCurveToComponent is a CubicCurveTo component in a Path
39 CubicCurveToComponent
40 // ArcToComponent is a ArcTo component in a Path
41 ArcToComponent
42 // CloseComponent is a ArcTo component in a Path
43 CloseComponent
44 )
45
46 // Path stores points
47 type Path struct {
48 // Components is a slice of PathComponent in a Path and mark the role of each points in the Path
49 Components []PathComponent
50 // Points are combined with Components to have a specific role in the path
51 Points []float64
52 // Last Point of the Path
53 x, y float64
54 }
55
56 func (p *Path) appendToPath(cmd PathComponent, points ...float64) {
57 p.Components = append(p.Components, cmd)
58 p.Points = append(p.Points, points...)
59 }
60
61 // LastPoint returns the current point of the current path
62 func (p *Path) LastPoint() (x, y float64) {
63 return p.x, p.y
64 }
65
66 // MoveTo starts a new path at (x, y) position
67 func (p *Path) MoveTo(x, y float64) {
68 p.appendToPath(MoveToComponent, x, y)
69 p.x = x
70 p.y = y
71 }
72
73 // LineTo adds a line to the current path
74 func (p *Path) LineTo(x, y float64) {
75 if len(p.Components) == 0 { //special case when no move has been done
76 p.MoveTo(0, 0)
77 }
78 p.appendToPath(LineToComponent, x, y)
79 p.x = x
80 p.y = y
81 }
82
83 // QuadCurveTo adds a quadratic bezier curve to the current path
84 func (p *Path) QuadCurveTo(cx, cy, x, y float64) {
85 if len(p.Components) == 0 { //special case when no move has been done
86 p.MoveTo(0, 0)
87 }
88 p.appendToPath(QuadCurveToComponent, cx, cy, x, y)
89 p.x = x
90 p.y = y
91 }
92
93 // CubicCurveTo adds a cubic bezier curve to the current path
94 func (p *Path) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) {
95 if len(p.Components) == 0 { //special case when no move has been done
96 p.MoveTo(0, 0)
97 }
98 p.appendToPath(CubicCurveToComponent, cx1, cy1, cx2, cy2, x, y)
99 p.x = x
100 p.y = y
101 }
102
103 // ArcTo adds an arc to the path
104 func (p *Path) ArcTo(cx, cy, rx, ry, startAngle, delta float64) {
105 endAngle := startAngle + delta
106 clockWise := true
107 if delta < 0 {
108 clockWise = false
109 }
110 // normalize
111 if clockWise {
112 for endAngle < startAngle {
113 endAngle += math.Pi * 2.0
114 }
115 } else {
116 for startAngle < endAngle {
117 startAngle += math.Pi * 2.0
118 }
119 }
120 startX := cx + math.Cos(startAngle)*rx
121 startY := cy + math.Sin(startAngle)*ry
122 if len(p.Components) > 0 {
123 p.LineTo(startX, startY)
124 } else {
125 p.MoveTo(startX, startY)
126 }
127 p.appendToPath(ArcToComponent, cx, cy, rx, ry, startAngle, delta)
128 p.x = cx + math.Cos(endAngle)*rx
129 p.y = cy + math.Sin(endAngle)*ry
130 }
131
132 // Close closes the current path
133 func (p *Path) Close() {
134 p.appendToPath(CloseComponent)
135 }
136
137 // Copy make a clone of the current path and return it
138 func (p *Path) Copy() (dest *Path) {
139 dest = new(Path)
140 dest.Components = make([]PathComponent, len(p.Components))
141 copy(dest.Components, p.Components)
142 dest.Points = make([]float64, len(p.Points))
143 copy(dest.Points, p.Points)
144 dest.x, dest.y = p.x, p.y
145 return dest
146 }
147
148 // Clear reset the path
149 func (p *Path) Clear() {
150 p.Components = p.Components[0:0]
151 p.Points = p.Points[0:0]
152 return
153 }
154
155 // IsEmpty returns true if the path is empty
156 func (p *Path) IsEmpty() bool {
157 return len(p.Components) == 0
158 }
159
160 // String returns a debug text view of the path
161 func (p *Path) String() string {
162 s := ""
163 j := 0
164 for _, cmd := range p.Components {
165 switch cmd {
166 case MoveToComponent:
167 s += fmt.Sprintf("MoveTo: %f, %f\n", p.Points[j], p.Points[j+1])
168 j = j + 2
169 case LineToComponent:
170 s += fmt.Sprintf("LineTo: %f, %f\n", p.Points[j], p.Points[j+1])
171 j = j + 2
172 case QuadCurveToComponent:
173 s += fmt.Sprintf("QuadCurveTo: %f, %f, %f, %f\n", p.Points[j], p.Points[j+1], p.Points[j+2], p.Points[j+3])
174 j = j + 4
175 case CubicCurveToComponent:
176 s += fmt.Sprintf("CubicCurveTo: %f, %f, %f, %f, %f, %f\n", p.Points[j], p.Points[j+1], p.Points[j+2], p.Points[j+3], p.Points[j+4], p.Points[j+5])
177 j = j + 6
178 case ArcToComponent:
179 s += fmt.Sprintf("ArcTo: %f, %f, %f, %f, %f, %f\n", p.Points[j], p.Points[j+1], p.Points[j+2], p.Points[j+3], p.Points[j+4], p.Points[j+5])
180 j = j + 6
181 case CloseComponent:
182 s += "Close\n"
183 }
184 }
185 return s
186 }