Join joins path q to p and returns the extended path p (or q if p is empty). It's like executing the commands in q to p in sequence, where if the first MoveTo of q doesn't coincide with p, or if p ends in Close, it will fallback to appending the paths.
(q *Path)
| 311 | |
| 312 | // Join joins path q to p and returns the extended path p (or q if p is empty). It's like executing the commands in q to p in sequence, where if the first MoveTo of q doesn't coincide with p, or if p ends in Close, it will fallback to appending the paths. |
| 313 | func (p *Path) Join(q *Path) *Path { |
| 314 | if q.Empty() { |
| 315 | return p |
| 316 | } else if p.Empty() { |
| 317 | return q |
| 318 | } |
| 319 | |
| 320 | if p.d[len(p.d)-1] == CloseCmd || !Equal(p.d[len(p.d)-3], q.d[1]) || !Equal(p.d[len(p.d)-2], q.d[2]) { |
| 321 | return &Path{append(p.d, q.d...)} |
| 322 | } |
| 323 | |
| 324 | d := q.d[cmdLen(MoveToCmd):] |
| 325 | |
| 326 | // add the first command through the command functions to use the optimization features |
| 327 | // q is not empty, so starts with a MoveTo followed by other commands |
| 328 | cmd := d[0] |
| 329 | switch cmd { |
| 330 | case MoveToCmd: |
| 331 | p.MoveTo(d[1], d[2]) |
| 332 | case LineToCmd: |
| 333 | p.LineTo(d[1], d[2]) |
| 334 | case QuadToCmd: |
| 335 | p.QuadTo(d[1], d[2], d[3], d[4]) |
| 336 | case CubeToCmd: |
| 337 | p.CubeTo(d[1], d[2], d[3], d[4], d[5], d[6]) |
| 338 | case ArcToCmd: |
| 339 | large, sweep := toArcFlags(d[4]) |
| 340 | p.ArcTo(d[1], d[2], d[3]*180.0/math.Pi, large, sweep, d[5], d[6]) |
| 341 | case CloseCmd: |
| 342 | p.Close() |
| 343 | } |
| 344 | |
| 345 | i := len(p.d) |
| 346 | end := p.StartPos() |
| 347 | p = &Path{append(p.d, d[cmdLen(cmd):]...)} |
| 348 | |
| 349 | // repair close commands |
| 350 | for i < len(p.d) { |
| 351 | cmd := p.d[i] |
| 352 | if cmd == MoveToCmd { |
| 353 | break |
| 354 | } else if cmd == CloseCmd { |
| 355 | p.d[i+1] = end.X |
| 356 | p.d[i+2] = end.Y |
| 357 | break |
| 358 | } |
| 359 | i += cmdLen(cmd) |
| 360 | } |
| 361 | return p |
| 362 | |
| 363 | } |
| 364 | |
| 365 | // Pos returns the current position of the path, which is the end point of the last command. |
| 366 | func (p *Path) Pos() Point { |