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.
type Path struct {
segs []segment
segs [][]segment
cur point
}
// 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) {
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 updates the current position to (x, y).
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}
}
@ -77,58 +84,61 @@ func (p *Path) strokeVertices(lineWidth float32, clr color.Color) (vertices []eb
sw, sh := emptyImage.Size()
r, g, b, a := clr.RGBA()
rf, gf, bf, af := float32(r)/0xffff, float32(g)/0xffff, float32(b)/0xffff, float32(a)/0xffff
for i, s := range p.segs {
si, co := math.Sincos(s.atan2() + math.Pi/2)
dx, dy := float32(co)*lineWidth/2, float32(si)*lineWidth/2
v0 := point{s.p0.x + dx, s.p0.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}
idx := uint16(0)
for _, ss := range p.segs {
for _, s := range ss {
si, co := math.Sincos(s.atan2() + math.Pi/2)
dx, dy := float32(co)*lineWidth/2, float32(si)*lineWidth/2
v0 := point{s.p0.x + dx, s.p0.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,
ebiten.Vertex{
DstX: v0.x,
DstY: v0.y,
SrcX: 0,
SrcY: 0,
ColorR: rf,
ColorG: gf,
ColorB: bf,
ColorA: af,
},
ebiten.Vertex{
DstX: v1.x,
DstY: v1.y,
SrcX: float32(sw),
SrcY: 0,
ColorR: rf,
ColorG: gf,
ColorB: bf,
ColorA: af,
},
ebiten.Vertex{
DstX: v2.x,
DstY: v2.y,
SrcX: 0,
SrcY: float32(sh),
ColorR: rf,
ColorG: gf,
ColorB: bf,
ColorA: af,
},
ebiten.Vertex{
DstX: v3.x,
DstY: v3.y,
SrcX: float32(sw),
SrcY: float32(sh),
ColorR: rf,
ColorG: gf,
ColorB: bf,
ColorA: af,
},
)
idx := uint16(4 * i)
indices = append(indices, idx, idx+1, idx+2, idx+1, idx+2, idx+3)
vertices = append(vertices,
ebiten.Vertex{
DstX: v0.x,
DstY: v0.y,
SrcX: 0,
SrcY: 0,
ColorR: rf,
ColorG: gf,
ColorB: bf,
ColorA: af,
},
ebiten.Vertex{
DstX: v1.x,
DstY: v1.y,
SrcX: float32(sw),
SrcY: 0,
ColorR: rf,
ColorG: gf,
ColorB: bf,
ColorA: af,
},
ebiten.Vertex{
DstX: v2.x,
DstY: v2.y,
SrcX: 0,
SrcY: float32(sh),
ColorR: rf,
ColorG: gf,
ColorB: bf,
ColorA: af,
},
ebiten.Vertex{
DstX: v3.x,
DstY: v3.y,
SrcX: float32(sw),
SrcY: float32(sh),
ColorR: rf,
ColorG: gf,
ColorB: bf,
ColorA: af,
},
)
indices = append(indices, idx, idx+1, idx+2, idx+1, idx+2, idx+3)
idx += 4
}
}
return