vector: Add FillOptions

This commit is contained in:
Hajime Hoshi 2020-03-16 23:49:55 +09:00
parent 4b63be0156
commit 1fdab58ff5
3 changed files with 32 additions and 11 deletions

View File

@ -98,7 +98,10 @@ func drawEbitenText(screen *ebiten.Image) {
path.LineTo(320, 55)
path.LineTo(290, 20)
path.Fill(screen, color.RGBA{0xdb, 0x56, 0x20, 0xff})
op := &vector.FillOptions{
Color: color.RGBA{0xdb, 0x56, 0x20, 0xff},
}
path.Fill(screen, op)
}
func drawEbitenLogo(screen *ebiten.Image, x, y int) {
@ -127,7 +130,10 @@ func drawEbitenLogo(screen *ebiten.Image, x, y int) {
path.LineTo(xf+unit, yf+3*unit)
path.LineTo(xf+unit, yf+4*unit)
path.Fill(screen, color.RGBA{0xdb, 0x56, 0x20, 0xff})
op := &vector.FillOptions{
Color: color.RGBA{0xdb, 0x56, 0x20, 0xff},
}
path.Fill(screen, op)
}
func maxCounter(index int) int {
@ -159,7 +165,10 @@ func drawWave(screen *ebiten.Image, counter int) {
path.LineTo(screenWidth, screenHeight)
path.LineTo(0, screenHeight)
path.Fill(screen, color.RGBA{0x33, 0x66, 0xff, 0xff})
op := &vector.FillOptions{
Color: color.RGBA{0x33, 0x66, 0xff, 0xff},
}
path.Fill(screen, op)
}
var counter = 0

View File

@ -49,6 +49,7 @@ func InTriangle(pt, pt0, pt1, pt2 Point) bool {
return (c0 <= 0 && c1 <= 0 && c2 <= 0) || (c0 >= 0 && c1 >= 0 && c2 >= 0)
}
// Triangulate triangulates the region surrounded by the points pts and returnes the point indices.
func Triangulate(pts []Point) []uint16 {
if len(pts) < 3 {
return nil
@ -78,6 +79,7 @@ func Triangulate(pts []Point) []uint16 {
// Triangulation by Ear Clipping.
// https://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf
// TODO: Adopt a more efficient algorithm.
for len(currentIndices) >= 3 {
// Calculate cross-products and remove unneeded vertices.
cs := make([]float32, len(currentIndices))

View File

@ -92,19 +92,29 @@ func (p *Path) CubicTo(cp0x, cp0y, cp1x, cp1y, x, y float32) {
}
}
func (p *Path) Fill(dst *ebiten.Image, clr color.Color) {
type FillOptions struct {
Color color.Color
}
// Fill fills the region of the path with the given options op.
func (p *Path) Fill(dst *ebiten.Image, op *FillOptions) {
var vertices []ebiten.Vertex
var indices []uint16
r, g, b, a := clr.RGBA()
var rf, gf, bf, af float32
if a > 0 {
rf = float32(r) / float32(a)
gf = float32(g) / float32(a)
bf = float32(b) / float32(a)
af = float32(a) / 0xffff
if op.Color == nil {
return
}
r, g, b, a := op.Color.RGBA()
var rf, gf, bf, af float32
if a == 0 {
return
}
rf = float32(r) / float32(a)
gf = float32(g) / float32(a)
bf = float32(b) / float32(a)
af = float32(a) / 0xffff
var base uint16
for _, seg := range p.segs {
for _, pt := range seg {