This commit is contained in:
Hajime Hoshi 2019-04-14 02:34:42 +09:00
parent 6bd9ef6abd
commit 66cfe104ab

View File

@ -52,20 +52,27 @@ func (s *segment) atan2() float64 {
// Path represents a collection of paths. // Path represents a collection of paths.
type Path struct { type Path struct {
segs []segment segs [][]segment
cur point cur point
} }
// MoveTo skips the current position of the path to the given position (x, y) without adding any strokes. // MoveTo skips the current position of the path to the given position (x, y) without adding any strokes.
func (p *Path) MoveTo(x, y float32) { func (p *Path) MoveTo(x, y float32) {
p.cur = point{x, y} p.cur = point{x, y}
if len(p.segs) > 0 && len(p.segs[len(p.segs)-1]) == 0 {
return
}
p.segs = append(p.segs, []segment{})
} }
// LineTo adds a segment to the path, which starts from the current position and ends to the given position (x, y). // LineTo adds a segment to the path, which starts from the current position and ends to the given position (x, y).
// //
// LineTo updates the current position to (x, y). // LineTo updates the current position to (x, y).
func (p *Path) LineTo(x, y float32) { func (p *Path) LineTo(x, y float32) {
p.segs = append(p.segs, segment{p.cur, point{x, y}}) if len(p.segs) == 0 {
p.segs = append(p.segs, []segment{})
}
p.segs[len(p.segs)-1] = append(p.segs[len(p.segs)-1], segment{p.cur, point{x, y}})
p.cur = point{x, y} p.cur = point{x, y}
} }
@ -77,58 +84,61 @@ func (p *Path) strokeVertices(lineWidth float32, clr color.Color) (vertices []eb
sw, sh := emptyImage.Size() sw, sh := emptyImage.Size()
r, g, b, a := clr.RGBA() r, g, b, a := clr.RGBA()
rf, gf, bf, af := float32(r)/0xffff, float32(g)/0xffff, float32(b)/0xffff, float32(a)/0xffff rf, gf, bf, af := float32(r)/0xffff, float32(g)/0xffff, float32(b)/0xffff, float32(a)/0xffff
for i, s := range p.segs { idx := uint16(0)
si, co := math.Sincos(s.atan2() + math.Pi/2) for _, ss := range p.segs {
dx, dy := float32(co)*lineWidth/2, float32(si)*lineWidth/2 for _, s := range ss {
v0 := point{s.p0.x + dx, s.p0.y + dy} si, co := math.Sincos(s.atan2() + math.Pi/2)
v1 := point{s.p0.x - dx, s.p0.y - dy} dx, dy := float32(co)*lineWidth/2, float32(si)*lineWidth/2
v2 := point{s.p1.x + dx, s.p1.y + dy} v0 := point{s.p0.x + dx, s.p0.y + dy}
v3 := point{s.p1.x - dx, s.p1.y - dy} v1 := point{s.p0.x - dx, s.p0.y - dy}
v2 := point{s.p1.x + dx, s.p1.y + dy}
v3 := point{s.p1.x - dx, s.p1.y - dy}
vertices = append(vertices, vertices = append(vertices,
ebiten.Vertex{ ebiten.Vertex{
DstX: v0.x, DstX: v0.x,
DstY: v0.y, DstY: v0.y,
SrcX: 0, SrcX: 0,
SrcY: 0, SrcY: 0,
ColorR: rf, ColorR: rf,
ColorG: gf, ColorG: gf,
ColorB: bf, ColorB: bf,
ColorA: af, ColorA: af,
}, },
ebiten.Vertex{ ebiten.Vertex{
DstX: v1.x, DstX: v1.x,
DstY: v1.y, DstY: v1.y,
SrcX: float32(sw), SrcX: float32(sw),
SrcY: 0, SrcY: 0,
ColorR: rf, ColorR: rf,
ColorG: gf, ColorG: gf,
ColorB: bf, ColorB: bf,
ColorA: af, ColorA: af,
}, },
ebiten.Vertex{ ebiten.Vertex{
DstX: v2.x, DstX: v2.x,
DstY: v2.y, DstY: v2.y,
SrcX: 0, SrcX: 0,
SrcY: float32(sh), SrcY: float32(sh),
ColorR: rf, ColorR: rf,
ColorG: gf, ColorG: gf,
ColorB: bf, ColorB: bf,
ColorA: af, ColorA: af,
}, },
ebiten.Vertex{ ebiten.Vertex{
DstX: v3.x, DstX: v3.x,
DstY: v3.y, DstY: v3.y,
SrcX: float32(sw), SrcX: float32(sw),
SrcY: float32(sh), SrcY: float32(sh),
ColorR: rf, ColorR: rf,
ColorG: gf, ColorG: gf,
ColorB: bf, ColorB: bf,
ColorA: af, ColorA: af,
}, },
) )
idx := uint16(4 * i) indices = append(indices, idx, idx+1, idx+2, idx+1, idx+2, idx+3)
indices = append(indices, idx, idx+1, idx+2, idx+1, idx+2, idx+3) idx += 4
}
} }
return return