3 Star 0 Fork 0

江苏岚江智能科技有限公司 / supply-point

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
draw.go 2.72 KB
一键复制 编辑 原始数据 按行查看 历史
吴文凯 提交于 2023-06-09 10:35 . 123
package supply_point
import (
"image"
"image/color"
"image/draw"
"image/png"
"os"
)
type Putpixel func(x, y int)
// 求绝对值
func abs(x int) int {
if x >= 0 {
return x
}
return -x
}
func drawLine(x0, y0, x1, y1 int, brush Putpixel) {
dx := abs(x1 - x0)
dy := abs(y1 - y0)
sx, sy := 1, 1
if x0 >= x1 {
sx = -1
}
if y0 >= y1 {
sy = -1
}
err := dx - dy
for {
brush(x0, y0)
if x0 == x1 && y0 == y1 {
break
}
e2 := 2 * err
if e2 > -dy {
err -= dy
x0 += sx
}
if e2 < dx {
err += dx
y0 += sy
}
}
}
func drawPathPoint(pp []*PathPoint, corners []*PathPoint, name string) {
m := image.NewRGBA(image.Rect(0, 0, 1000, 2000))
for i := 1; i < len(pp); i++ {
drawLine(pp[i-1].X, pp[i-1].Y, pp[i].X, pp[i].Y, func(x, y int) {
m.Set(x, y, color.RGBA{R: 255, A: 255})
})
drawPoint(m, pp[i-1].X, pp[i-1].Y, color.RGBA{G: 255, A: 255})
}
for i := 0; i < len(corners); i++ {
//fmt.Println(cornersint[i-1][0], cornersint[i-1][1])
drawPoint(m, corners[i].X, corners[i].Y, color.White)
}
//point := image.Point{
// X: ljint[0][0],
// Y: ljint[0][1],
//}
drawPoint(m, pp[0].X, pp[0].Y, color.RGBA{B: 255, A: 255})
f, _ := os.Create("res/" + name)
png.Encode(f, m)
f.Close()
}
func drawPathPointXy(pp []*PathPoint, corners []int, name string) {
m := image.NewRGBA(image.Rect(0, 0, 1000, 2000))
for i := 1; i < len(pp); i++ {
drawLine(pp[i-1].X, pp[i-1].Y, pp[i].X, pp[i].Y, func(x, y int) {
m.Set(x, y, color.RGBA{R: 255, A: 255})
})
drawPoint(m, pp[i-1].X, pp[i-1].Y, color.RGBA{G: 255, A: 255})
}
for i := 0; i < len(corners); i++ {
//fmt.Println(cornersint[i-1][0], cornersint[i-1][1])
drawPoint(m, pp[corners[i]].X, pp[corners[i]].Y, color.White)
}
//point := image.Point{
// X: ljint[0][0],
// Y: ljint[0][1],
//}
drawPoint(m, pp[0].X, pp[0].Y, color.RGBA{B: 255, A: 255})
f, _ := os.Create("res/" + name)
png.Encode(f, m)
f.Close()
}
func drawPoint(dst draw.Image, x, y int, c color.Color) {
r := image.Rectangle{
Min: image.Point{
X: x,
Y: y,
},
Max: image.Point{
X: x + 10,
Y: y + 10,
},
}
draw.Draw(dst, r, &image.Uniform{c}, image.ZP, draw.Src)
}
func drawSupplyPoint(pp []*PathPoint, corners [][]int) {
m := image.NewRGBA(image.Rect(0, 0, 1000, 2000))
for i := 1; i < len(pp); i++ {
drawLine(pp[i-1].X, pp[i-1].Y, pp[i].X, pp[i].Y, func(x, y int) {
m.Set(x, y, color.RGBA{R: 255, A: 255})
})
drawPoint(m, pp[i-1].X, pp[i-1].Y, color.RGBA{G: 255, A: 255})
}
for i := 0; i < len(corners); i++ {
for j := 0; j < len(corners[i]); j++ {
drawPoint(m, pp[corners[i][j]].X, pp[corners[i][j]].Y, color.White)
}
}
drawPoint(m, pp[0].X, pp[0].Y, color.RGBA{B: 255, A: 255})
f, _ := os.Create("test.png")
png.Encode(f, m)
f.Close()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jiangsu-lanjiang-intelligent/supply-point.git
git@gitee.com:jiangsu-lanjiang-intelligent/supply-point.git
jiangsu-lanjiang-intelligent
supply-point
supply-point
v0.1.20

搜索帮助

344bd9b3 5694891 D2dac590 5694891