SkillAgentSearch skills...

Bresenham

Draw a line in golang with the bresenham algorithm

Install / Use

/learn @StephaneBunel/Bresenham
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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

View on GitHub
GitHub Stars31
CategoryDevelopment
Updated4mo ago
Forks4

Languages

Go

Security Score

87/100

Audited on Nov 13, 2025

No findings