refactor: 修改绘制辅助函数以支持浮点半径,更新精灵结构体以使用浮点数表示半径,确保图形处理更精确。
This commit is contained in:
parent
2c2f855198
commit
0959b16b9d
@ -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()
|
r, g, b, a := color.RGBA()
|
||||||
r8, g8, b8, a8 := uint8(r>>8), uint8(g>>8), uint8(b>>8), uint8(a>>8)
|
r8, g8, b8, a8 := uint8(r>>8), uint8(g>>8), uint8(b>>8), uint8(a>>8)
|
||||||
|
|
||||||
// 使用Bresenham算法绘制圆
|
// 使用Bresenham算法绘制圆
|
||||||
x := 0
|
x := float64(0)
|
||||||
y := radius
|
y := float64(radius)
|
||||||
d := 3 - 2*radius
|
d := 3 - 2*radius
|
||||||
|
|
||||||
bounds := img.Bounds()
|
bounds := img.Bounds()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// 绘制8个对称点,但只在图像范围内
|
// 绘制8个对称点,但只在图像范围内
|
||||||
DrawPointIfInBounds(img, bounds, x0+x, y0+y, r8, g8, b8, a8)
|
DrawPointIfInBounds(img, bounds, x0+int(x), y0+int(y), r8, g8, b8, a8)
|
||||||
DrawPointIfInBounds(img, bounds, x0+x, y0-y, r8, g8, b8, a8)
|
DrawPointIfInBounds(img, bounds, x0+int(x), y0-int(y), r8, g8, b8, a8)
|
||||||
DrawPointIfInBounds(img, bounds, x0-x, y0+y, r8, g8, b8, a8)
|
DrawPointIfInBounds(img, bounds, x0-int(x), y0+int(y), r8, g8, b8, a8)
|
||||||
DrawPointIfInBounds(img, bounds, x0-x, y0-y, r8, g8, b8, a8)
|
DrawPointIfInBounds(img, bounds, x0-int(x), y0-int(y), r8, g8, b8, a8)
|
||||||
DrawPointIfInBounds(img, bounds, x0+y, y0+x, r8, g8, b8, a8)
|
DrawPointIfInBounds(img, bounds, x0+int(y), y0+int(x), r8, g8, b8, a8)
|
||||||
DrawPointIfInBounds(img, bounds, x0+y, y0-x, r8, g8, b8, a8)
|
DrawPointIfInBounds(img, bounds, x0+int(y), y0-int(x), r8, g8, b8, a8)
|
||||||
DrawPointIfInBounds(img, bounds, x0-y, y0+x, r8, g8, b8, a8)
|
DrawPointIfInBounds(img, bounds, x0-int(y), y0+int(x), r8, g8, b8, a8)
|
||||||
DrawPointIfInBounds(img, bounds, x0-y, y0-x, r8, g8, b8, a8)
|
DrawPointIfInBounds(img, bounds, x0-int(y), y0-int(x), r8, g8, b8, a8)
|
||||||
|
|
||||||
if d < 0 {
|
if d < 0 {
|
||||||
d += 4*x + 6
|
d += 4*x + 6
|
||||||
|
@ -72,7 +72,7 @@ func (l *Line) AddToSprite(sprite *Sprite) {
|
|||||||
|
|
||||||
type Circle struct {
|
type Circle struct {
|
||||||
Center image.Point
|
Center image.Point
|
||||||
Radius int
|
Radius float64
|
||||||
Color color.Color
|
Color color.Color
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,13 +81,13 @@ func (c *Circle) AddToSprite(sprite *Sprite) {
|
|||||||
// 检查精灵是否已有图像
|
// 检查精灵是否已有图像
|
||||||
if sprite.Image == nil {
|
if sprite.Image == nil {
|
||||||
// 如果没有图像,创建一个新图像
|
// 如果没有图像,创建一个新图像
|
||||||
size := c.Radius * 2
|
size := int(c.Radius * 2)
|
||||||
img := image.NewRGBA(image.Rect(0, 0, size, size))
|
img := image.NewRGBA(image.Rect(0, 0, size, size))
|
||||||
sprite.Image = img
|
sprite.Image = img
|
||||||
// 设置精灵位置为圆心减去半径
|
// 设置精灵位置为圆心减去半径
|
||||||
sprite.Position = image.Point{
|
sprite.Position = image.Point{
|
||||||
X: c.Center.X - c.Radius,
|
X: c.Center.X - int(c.Radius),
|
||||||
Y: c.Center.Y - c.Radius,
|
Y: c.Center.Y - int(c.Radius),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user