ebitenutil: add DrawCircle (#2175)

Closes #2163
This commit is contained in:
Filip Piwowarczyk 2022-07-03 11:31:57 +02:00 committed by GitHub
parent c31cc4ecff
commit a249296723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,7 @@ import (
"math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
var (
@ -59,3 +60,24 @@ func DrawRect(dst *ebiten.Image, x, y, width, height float64, clr color.Color) {
// Linear filtering would make edges blurred.
dst.DrawImage(emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image), op)
}
// DrawCircle draws a circle on given destination dst.
//
// DrawCircle is intended to be used mainly for debugging or prototyping puropose.
func DrawCircle(dst *ebiten.Image, cx, cy, r float64, clr color.Color) {
var path vector.Path
rd, g, b, a := clr.RGBA()
path.Arc(float32(cx), float32(cy), float32(r), 0, 2*math.Pi, vector.Clockwise)
verticles, indices := path.AppendVerticesAndIndicesForFilling(nil, nil)
for i := range verticles {
verticles[i].SrcX = 1
verticles[i].SrcY = 1
verticles[i].ColorR = float32(rd) / 0xffff
verticles[i].ColorG = float32(g) / 0xffff
verticles[i].ColorB = float32(b) / 0xffff
verticles[i].ColorA = float32(a) / 0xffff
}
dst.DrawTriangles(verticles, indices, emptySubImage, nil)
}