2011-04-27 08:06:14 +00:00
|
|
|
// Copyright 2010 The draw2d Authors. All rights reserved.
|
|
|
|
// created: 13/12/2010 by Laurent Le Goff
|
2012-04-17 09:03:56 +00:00
|
|
|
|
2015-04-29 08:28:05 +00:00
|
|
|
package path
|
2011-04-27 08:06:14 +00:00
|
|
|
|
|
|
|
type DashVertexConverter struct {
|
2015-04-23 13:36:56 +00:00
|
|
|
next LineBuilder
|
2011-04-27 08:06:14 +00:00
|
|
|
x, y, distance float64
|
|
|
|
dash []float64
|
|
|
|
currentDash int
|
|
|
|
dashOffset float64
|
|
|
|
}
|
|
|
|
|
2015-04-23 13:36:56 +00:00
|
|
|
func NewDashConverter(dash []float64, dashOffset float64, converter LineBuilder) *DashVertexConverter {
|
2011-04-27 08:06:14 +00:00
|
|
|
var dasher DashVertexConverter
|
|
|
|
dasher.dash = dash
|
|
|
|
dasher.currentDash = 0
|
|
|
|
dasher.dashOffset = dashOffset
|
|
|
|
dasher.next = converter
|
|
|
|
return &dasher
|
|
|
|
}
|
|
|
|
|
2015-04-23 13:36:56 +00:00
|
|
|
func (dasher *DashVertexConverter) LineTo(x, y float64) {
|
|
|
|
dasher.lineTo(x, y)
|
2011-04-27 08:06:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 13:36:56 +00:00
|
|
|
func (dasher *DashVertexConverter) MoveTo(x, y float64) {
|
|
|
|
dasher.next.MoveTo(x, y)
|
2011-04-27 08:06:14 +00:00
|
|
|
dasher.x, dasher.y = x, y
|
|
|
|
dasher.distance = dasher.dashOffset
|
|
|
|
dasher.currentDash = 0
|
|
|
|
}
|
|
|
|
|
2015-04-23 15:43:26 +00:00
|
|
|
func (dasher *DashVertexConverter) LineJoin() {
|
|
|
|
dasher.next.LineJoin()
|
|
|
|
}
|
|
|
|
|
2015-04-23 15:08:20 +00:00
|
|
|
func (dasher *DashVertexConverter) Close() {
|
|
|
|
dasher.next.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dasher *DashVertexConverter) End() {
|
|
|
|
dasher.next.End()
|
|
|
|
}
|
|
|
|
|
2011-04-27 08:06:14 +00:00
|
|
|
func (dasher *DashVertexConverter) lineTo(x, y float64) {
|
|
|
|
rest := dasher.dash[dasher.currentDash] - dasher.distance
|
|
|
|
for rest < 0 {
|
|
|
|
dasher.distance = dasher.distance - dasher.dash[dasher.currentDash]
|
|
|
|
dasher.currentDash = (dasher.currentDash + 1) % len(dasher.dash)
|
|
|
|
rest = dasher.dash[dasher.currentDash] - dasher.distance
|
|
|
|
}
|
|
|
|
d := distance(dasher.x, dasher.y, x, y)
|
|
|
|
for d >= rest {
|
|
|
|
k := rest / d
|
|
|
|
lx := dasher.x + k*(x-dasher.x)
|
|
|
|
ly := dasher.y + k*(y-dasher.y)
|
|
|
|
if dasher.currentDash%2 == 0 {
|
|
|
|
// line
|
2015-04-23 13:36:56 +00:00
|
|
|
dasher.next.LineTo(lx, ly)
|
2011-04-27 08:06:14 +00:00
|
|
|
} else {
|
|
|
|
// gap
|
2015-04-23 14:18:21 +00:00
|
|
|
dasher.next.End()
|
2015-04-23 13:36:56 +00:00
|
|
|
dasher.next.MoveTo(lx, ly)
|
2011-04-27 08:06:14 +00:00
|
|
|
}
|
|
|
|
d = d - rest
|
|
|
|
dasher.x, dasher.y = lx, ly
|
|
|
|
dasher.currentDash = (dasher.currentDash + 1) % len(dasher.dash)
|
|
|
|
rest = dasher.dash[dasher.currentDash]
|
|
|
|
}
|
|
|
|
dasher.distance = d
|
|
|
|
if dasher.currentDash%2 == 0 {
|
|
|
|
// line
|
2015-04-23 13:36:56 +00:00
|
|
|
dasher.next.LineTo(x, y)
|
2011-04-27 08:06:14 +00:00
|
|
|
} else {
|
|
|
|
// gap
|
2015-04-23 14:18:21 +00:00
|
|
|
dasher.next.End()
|
2015-04-23 13:36:56 +00:00
|
|
|
dasher.next.MoveTo(x, y)
|
2011-04-27 08:06:14 +00:00
|
|
|
}
|
|
|
|
if dasher.distance >= dasher.dash[dasher.currentDash] {
|
|
|
|
dasher.distance = dasher.distance - dasher.dash[dasher.currentDash]
|
|
|
|
dasher.currentDash = (dasher.currentDash + 1) % len(dasher.dash)
|
|
|
|
}
|
|
|
|
dasher.x, dasher.y = x, y
|
|
|
|
}
|