imagedd/README.md

189 lines
3.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# imagedd
golang绘图库
## 功能概述
imagedd是一个Go语言绘图库提供了精灵图(Sprite)管理、2D图形绘制、图像渲染等功能。
## 主要组件
### 精灵图 (Sprite)
精灵图是基本的渲染单元,包含图像数据和位置信息。
```go
type Sprite struct {
Name string // 精灵名称
Position image.Point // 精灵位置
Image image.Image // 精灵图像
Index int // 用于排序的索引
}
```
#### 精灵图操作
```go
// 移动精灵
sprite.Move(x, y int)
// 旋转精灵
sprite.Rotate(angle float64)
// 投影变换
sprite.Project(projectMatrix *ProjectMatrix)
// 在精灵上绘制线条
sprite.DrawLine(line *Line)
```
### 精灵板 (NamedSpriteBoard)
精灵板用于管理多个精灵,提供高效的添加、查找、排序功能。
```go
// 创建新的精灵板
board := sprite.NewNamedSpriteBoard()
// 添加精灵
board.AddSprite(sprite)
// 按名称查找精灵
foundSprite := board.GetSpriteByName("精灵名称")
// 获取特定索引的所有精灵
sprites := board.GetSpritesByIndex(1)
// 按名称删除精灵
board.RemoveSpriteByName("精灵名称")
// 更新精灵的索引
board.UpdateSpriteIndex("精灵名称", 2)
// 更新精灵的名称
board.UpdateSpriteName("旧名称", "新名称")
// 获取所有精灵按Index主排序Name次排序
allSprites := board.GetAllSprites()
// 渲染精灵板为图像
img := board.RenderToImage()
// 保存为PNG文件
board.SaveToPng("output.png")
```
### 2D图形组件
#### 线条 (Line)
```go
// 创建线条
line := &sprite.Line{
Start: image.Point{X: 10, Y: 10},
End: image.Point{X: 100, Y: 100},
Width: 2,
Color: color.RGBA{255, 0, 0, 255}, // 红色
}
// 添加到精灵
line.AddToSprite(sprite)
```
#### 圆形 (Circle)
```go
// 创建圆形
circle := &sprite.Circle{
Center: image.Point{X: 50, Y: 50},
Radius: 30,
Color: color.RGBA{0, 0, 255, 255}, // 蓝色
}
// 添加到精灵
circle.AddToSprite(sprite)
```
#### 投影矩阵 (ProjectMatrix)
```go
// 创建投影矩阵
matrix := &sprite.ProjectMatrix{
Matrix: [2][2]float64{
{1.0, 0.5},
{0.0, 1.0},
},
}
// 投影点
newPoint := matrix.ProjectPoint(image.Point{X: 10, Y: 20})
// 投影线条
newLine := matrix.ProjectLine(line)
```
## 渲染特性
- 支持精灵的层级渲染通过Index控制
- 支持精灵的名称索引O(1)时间复杂度查找)
- 支持负坐标位置的精灵
- 自动计算画布大小以适应所有精灵
- 处理超出边界的精灵(裁剪)
## 使用示例
```go
package main
import (
"image"
"image/color"
"git.lxtend.com/lixiangwuxian/imagedd/sprite"
)
func main() {
// 创建精灵板
board := sprite.NewNamedSpriteBoard()
// 创建精灵
sprite1 := &sprite.Sprite{
Name: "背景",
Position: image.Point{0, 0},
Index: 0,
}
// 创建圆形并添加到精灵
circle := &sprite.Circle{
Center: image.Point{50, 50},
Radius: 30,
Color: color.RGBA{0, 0, 255, 255},
}
circle.AddToSprite(sprite1)
// 添加到精灵板
board.AddSprite(sprite1)
// 创建第二个精灵
sprite2 := &sprite.Sprite{
Name: "线条",
Position: image.Point{20, 20},
Index: 1,
}
// 创建线条并添加到精灵
line := &sprite.Line{
Start: image.Point{0, 0},
End: image.Point{100, 100},
Width: 2,
Color: color.RGBA{255, 0, 0, 255},
}
line.AddToSprite(sprite2)
// 添加到精灵板
board.AddSprite(sprite2)
// 渲染并保存
board.SaveToPng("output.png")
}
```