Bresenham
Draw a line in golang with the bresenham algorithm
Install / Use
/learn @StephaneBunel/BresenhamREADME
Draw line with the Bresenham algorithm in Golang
Because golang is lacking of a basic drawing library, this is how I rediscovered and implemented the bresenham algorithm to draw a line.
Example
package main
import (
"image"
"image/color"
"image/png"
"os"
"github.com/StephaneBunel/bresenham"
)
func main() {
var imgRect = image.Rect(0, 0, 500, 500)
var img = image.NewRGBA(imgRect)
var colBLUE = color.RGBA{0, 0, 255, 255}
// draw line
bresenham.DrawLine(img, 14, 71, 441, 317, colBLUE)
// save image in example1.png
toimg, _ := os.Create("example1.png")
defer toimg.Close()
png.Encode(toimg, img)
}
bresenham.DrawLine() gets a Plotter interface as it's first argument.
type Plotter interface {
Set(x int, y int, c color.Color)
}
Benchmark
go test -bench=.
NB: On a modern processor with dedicated floating point ALU, naive implementation could be on par (or faster) with the bresenham version.
Related Skills
node-connect
346.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
346.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
346.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
