refactor: 修改绘制辅助函数以支持浮点半径,更新精灵结构体以使用浮点数表示半径,确保图形处理更精确。

This commit is contained in:
lixiangwuxian
2025-05-10 12:57:58 +08:00
parent 2c2f855198
commit 0959b16b9d
2 changed files with 15 additions and 15 deletions

View File

@@ -103,27 +103,27 @@ func DrawLineOnImage(img *image.RGBA, x0, y0, x1, y1 int, color color.Color) {
}
// 辅助函数:在图像上绘制圆
func DrawCircleOnImage(img *image.RGBA, x0, y0, radius int, color color.Color) {
func DrawCircleOnImage(img *image.RGBA, x0, y0 int, radius float64, color color.Color) {
r, g, b, a := color.RGBA()
r8, g8, b8, a8 := uint8(r>>8), uint8(g>>8), uint8(b>>8), uint8(a>>8)
// 使用Bresenham算法绘制圆
x := 0
y := radius
x := float64(0)
y := float64(radius)
d := 3 - 2*radius
bounds := img.Bounds()
for {
// 绘制8个对称点但只在图像范围内
DrawPointIfInBounds(img, bounds, x0+x, y0+y, r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0+x, y0-y, r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0-x, y0+y, r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0-x, y0-y, r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0+y, y0+x, r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0+y, y0-x, r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0-y, y0+x, r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0-y, y0-x, r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0+int(x), y0+int(y), r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0+int(x), y0-int(y), r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0-int(x), y0+int(y), r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0-int(x), y0-int(y), r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0+int(y), y0+int(x), r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0+int(y), y0-int(x), r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0-int(y), y0+int(x), r8, g8, b8, a8)
DrawPointIfInBounds(img, bounds, x0-int(y), y0-int(x), r8, g8, b8, a8)
if d < 0 {
d += 4*x + 6