package sprite import ( "image" "image/color" "image/draw" "git.lxtend.com/lixiangwuxian/imagedd/drawhelper" ) type Line struct { Start image.Point End image.Point Width int Color color.Color } // AddToSprite 在精灵图上绘制线条,保留原有的图像 func (l *Line) AddToSprite(sprite *Sprite) { // log.Printf("开始绘制线条: 起点(%d,%d) 终点(%d,%d) 宽度%d", // l.Start.X, l.Start.Y, l.End.X, l.End.Y, l.Width) // 检查精灵是否已有图像 if sprite.Image == nil { // 如果没有图像,创建一个新图像 width := l.End.X - l.Start.X + 1 height := l.End.Y - l.Start.Y + 1 if width < 1 { width = 1 } if height < 1 { height = 1 } img := image.NewRGBA(image.Rect(0, 0, width, height)) sprite.Image = img sprite.Position = l.Start // log.Printf("创建新图像: 大小(%d,%d) 位置(%d,%d)", // width, height, sprite.Position.X, sprite.Position.Y) } // 获取当前图像大小和位置 bounds := sprite.Image.Bounds() // width, height := bounds.Dx(), bounds.Dy() // log.Printf("当前图像: 大小(%d,%d) 位置(%d,%d)", // width, height, sprite.Position.X, sprite.Position.Y) // 直接使用原图像,不再扩展 var newImage *image.RGBA newImage, _ = sprite.Image.(*image.RGBA) if newImage == nil { // 如果原图像不是RGBA,转换为RGBA newImage = image.NewRGBA(bounds) draw.Draw(newImage, bounds, sprite.Image, bounds.Min, draw.Src) // log.Printf("转换图像为RGBA格式") } // 在图像上绘制线条,根据线宽决定使用哪个函数 // 超出图像边界的部分将被自动丢弃AddToSprite if l.Width <= 1 { // 使用标准线条绘制算法 drawhelper.DrawLineOnImage(newImage, l.Start.X, l.Start.Y, l.End.X, l.End.Y, l.Color) } else { // 使用粗线条绘制算法 drawhelper.DrawThickLineOnImage(newImage, l.Start.X, l.Start.Y, l.End.X, l.End.Y, l.Width, l.Color) } // 更新精灵图像 sprite.Image = newImage // log.Printf("线条绘制完成") } type Circle struct { Center image.Point Radius float64 Color color.Color } // AddToSprite 在精灵图上绘制圆形,保留原有的图像 func (c *Circle) AddToSprite(sprite *Sprite) { // 检查精灵是否已有图像 if sprite.Image == nil { // 如果没有图像,创建一个新图像 size := int(c.Radius * 2) img := image.NewRGBA(image.Rect(0, 0, size, size)) sprite.Image = img // 设置精灵位置为圆心减去半径 sprite.Position = image.Point{ X: c.Center.X - int(c.Radius), Y: c.Center.Y - int(c.Radius), } } // 获取当前图像大小和位置 bounds := sprite.Image.Bounds() // 直接使用原图像,不再扩展 var newImage *image.RGBA newImage, _ = sprite.Image.(*image.RGBA) if newImage == nil { // 如果原图像不是RGBA,转换为RGBA newImage = image.NewRGBA(bounds) draw.Draw(newImage, bounds, sprite.Image, bounds.Min, draw.Src) } // 在新图像上绘制圆 // 超出图像边界的部分将被自动丢弃 drawhelper.DrawCircleOnImage(newImage, c.Center.X, c.Center.Y, c.Radius, c.Color) // 更新精灵图像 sprite.Image = newImage } type ProjectMatrix struct { Matrix [2][2]float64 } func (p *ProjectMatrix) ProjectPoint(point image.Point) image.Point { return image.Point{ X: int(p.Matrix[0][0]*float64(point.X) + p.Matrix[0][1]*float64(point.Y)), Y: int(p.Matrix[1][0]*float64(point.X) + p.Matrix[1][1]*float64(point.Y)), } } func (p *ProjectMatrix) ProjectLine(line Line) Line { return Line{ Start: p.ProjectPoint(line.Start), End: p.ProjectPoint(line.End), } }