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 12:33:32 +00:00
|
|
|
package draw2dbase
|
2015-04-29 08:28:05 +00:00
|
|
|
|
|
|
|
import (
|
2015-04-29 12:33:32 +00:00
|
|
|
"math"
|
2015-04-29 08:28:05 +00:00
|
|
|
|
2015-08-14 20:22:01 +00:00
|
|
|
"github.com/llgcode/draw2d"
|
|
|
|
)
|
2015-04-29 08:28:05 +00:00
|
|
|
|
2011-04-27 08:06:14 +00:00
|
|
|
type LineStroker struct {
|
2015-04-29 15:16:15 +00:00
|
|
|
Flattener Flattener
|
2011-04-27 08:06:14 +00:00
|
|
|
HalfLineWidth float64
|
2015-04-29 12:33:32 +00:00
|
|
|
Cap draw2d.LineCap
|
|
|
|
Join draw2d.LineJoin
|
2011-04-27 08:06:14 +00:00
|
|
|
vertices []float64
|
|
|
|
rewind []float64
|
|
|
|
x, y, nx, ny float64
|
|
|
|
}
|
|
|
|
|
2015-04-29 15:16:15 +00:00
|
|
|
func NewLineStroker(c draw2d.LineCap, j draw2d.LineJoin, flattener Flattener) *LineStroker {
|
2011-04-27 08:06:14 +00:00
|
|
|
l := new(LineStroker)
|
2015-04-29 15:16:15 +00:00
|
|
|
l.Flattener = flattener
|
2011-04-27 08:06:14 +00:00
|
|
|
l.HalfLineWidth = 0.5
|
|
|
|
l.Cap = c
|
|
|
|
l.Join = j
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2015-04-23 15:43:26 +00:00
|
|
|
func (l *LineStroker) MoveTo(x, y float64) {
|
|
|
|
l.x, l.y = x, y
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LineStroker) LineTo(x, y float64) {
|
|
|
|
l.line(l.x, l.y, x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LineStroker) LineJoin() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LineStroker) line(x1, y1, x2, y2 float64) {
|
|
|
|
dx := (x2 - x1)
|
|
|
|
dy := (y2 - y1)
|
|
|
|
d := vectorDistance(dx, dy)
|
|
|
|
if d != 0 {
|
|
|
|
nx := dy * l.HalfLineWidth / d
|
|
|
|
ny := -(dx * l.HalfLineWidth / d)
|
|
|
|
l.appendVertex(x1+nx, y1+ny, x2+nx, y2+ny, x1-nx, y1-ny, x2-nx, y2-ny)
|
|
|
|
l.x, l.y, l.nx, l.ny = x2, y2, nx, ny
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LineStroker) Close() {
|
|
|
|
if len(l.vertices) > 1 {
|
|
|
|
l.appendVertex(l.vertices[0], l.vertices[1], l.rewind[0], l.rewind[1])
|
|
|
|
}
|
2015-04-23 14:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LineStroker) End() {
|
|
|
|
if len(l.vertices) > 1 {
|
2015-04-29 15:16:15 +00:00
|
|
|
l.Flattener.MoveTo(l.vertices[0], l.vertices[1])
|
2015-04-23 14:18:21 +00:00
|
|
|
for i, j := 2, 3; j < len(l.vertices); i, j = i+2, j+2 {
|
2015-04-29 15:16:15 +00:00
|
|
|
l.Flattener.LineTo(l.vertices[i], l.vertices[j])
|
2011-04-27 08:06:14 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-23 14:18:21 +00:00
|
|
|
for i, j := len(l.rewind)-2, len(l.rewind)-1; j > 0; i, j = i-2, j-2 {
|
2015-04-29 15:16:15 +00:00
|
|
|
l.Flattener.LineTo(l.rewind[i], l.rewind[j])
|
2015-04-23 14:18:21 +00:00
|
|
|
}
|
|
|
|
if len(l.vertices) > 1 {
|
2015-04-29 15:16:15 +00:00
|
|
|
l.Flattener.LineTo(l.vertices[0], l.vertices[1])
|
2015-04-23 14:18:21 +00:00
|
|
|
}
|
2015-04-29 15:16:15 +00:00
|
|
|
l.Flattener.End()
|
2015-04-23 14:18:21 +00:00
|
|
|
// reinit vertices
|
|
|
|
l.vertices = l.vertices[0:0]
|
|
|
|
l.rewind = l.rewind[0:0]
|
|
|
|
l.x, l.y, l.nx, l.ny = 0, 0, 0, 0
|
|
|
|
|
2011-04-27 08:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LineStroker) appendVertex(vertices ...float64) {
|
|
|
|
s := len(vertices) / 2
|
2015-04-23 15:24:41 +00:00
|
|
|
l.vertices = append(l.vertices, vertices[:s]...)
|
|
|
|
l.rewind = append(l.rewind, vertices[s:]...)
|
2011-04-27 08:06:14 +00:00
|
|
|
}
|
2015-04-29 12:33:32 +00:00
|
|
|
|
|
|
|
func vectorDistance(dx, dy float64) float64 {
|
|
|
|
return float64(math.Sqrt(dx*dx + dy*dy))
|
|
|
|
}
|