| 1 |
package drawing |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
type PathBuilder interface { |
| 10 |
|
| 11 |
LastPoint() (x, y float64) |
| 12 |
|
| 13 |
MoveTo(x, y float64) |
| 14 |
|
| 15 |
LineTo(x, y float64) |
| 16 |
h |
| 17 |
QuadCurveTo(cx, cy, x, y float64) |
| 18 |
h |
| 19 |
CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) |
| 20 |
|
| 21 |
ArcTo(cx, cy, rx, ry, startAngle, angle float64) |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
Close() |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
type PathComponent int |
| 30 |
|
| 31 |
const ( |
| 32 |
|
| 33 |
MoveToComponent PathComponent = iota |
| 34 |
|
| 35 |
LineToComponent |
| 36 |
|
| 37 |
QuadCurveToComponent |
| 38 |
|
| 39 |
CubicCurveToComponent |
| 40 |
|
| 41 |
ArcToComponent |
| 42 |
|
| 43 |
CloseComponent |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
type Path struct { |
| 48 |
|
| 49 |
Components []PathComponent |
| 50 |
|
| 51 |
Points []float64 |
| 52 |
|
| 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 |
|
| 62 |
func (p *Path) LastPoint() (x, y float64) { |
| 63 |
return p.x, p.y |
| 64 |
} |
| 65 |
|
| 66 |
|
| 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 |
|
| 74 |
func (p *Path) LineTo(x, y float64) { |
| 75 |
if len(p.Components) == 0 { |
| 76 |
p.MoveTo(0, 0) |
| 77 |
} |
| 78 |
p.appendToPath(LineToComponent, x, y) |
| 79 |
p.x = x |
| 80 |
p.y = y |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
func (p *Path) QuadCurveTo(cx, cy, x, y float64) { |
| 85 |
if len(p.Components) == 0 { |
| 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 |
|
| 94 |
func (p *Path) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) { |
| 95 |
if len(p.Components) == 0 { |
| 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 |
|
| 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 |
|
| 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 |
|
| 133 |
func (p *Path) Close() { |
| 134 |
p.appendToPath(CloseComponent) |
| 135 |
} |
| 136 |
|
| 137 |
|
| 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 |
|
| 149 |
func (p *Path) Clear() { |
| 150 |
p.Components = p.Components[0:0] |
| 151 |
p.Points = p.Points[0:0] |
| 152 |
return |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
func (p *Path) IsEmpty() bool { |
| 157 |
return len(p.Components) == 0 |
| 158 |
} |
| 159 |
|
| 160 |
|
| 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 |
} |