This is an all integer go-implementation of Bresenham line algorithm.
It is generalized to its purpose as it takes a function as argument that handles each generated coordinate along the line. This makes it suitable for more purposes than just the obvious line drawing in images.
To draw a line from point (p1x, p1y) to (p2x, p2y) simply call bresenham.Bresenham
function.
bresenham.Bresenham
function.// This function is applied to every line coordinate that Bresenham line algorithm generates.
drawPixelFn := func(x, y int) { myImage.Set(x, y, c) } // Put generated pixel on image
bresenham.Bresenham(p1x, p1y, p2x, p2y, drawPixelFn)
See also examples below.
It is not a requirement by the license, but if you do improve the code then any feedback or pull request is very appreciated. Sharing is caring.
printBresenhamPlotCoordinate := func(x, y int) { fmt.Printf("(%d,%d) ", x, y) }
p1 := image.Pt(0, 0)
p2 := image.Pt(10, 5)
fmt.Printf("Print plotted coordinates from %+v to %+v using bresenham algorithm.\n", p1, p2)
bresenham.Bresenham(p1.X, p1.Y, p2.X, p2.Y, printBresenhamPlotCoordinate)
fmt.Println()
Print plotted coordinates from (0,0) to (10,5) using bresenham algorithm. (0,0) (1,1) (2,1) (3,2) (4,2) (5,3) (6,3) (7,4) (8,4) (9,5) (10,5)
func Line(p1 image.Point, p2 image.Point, c color.Color, img draw.Image) {
drawPixelFn := func(x, y int) { img.Set(x, y, c) }
bresenham.Bresenham(p1.X, p1.Y, p2.X, p2.Y, drawPixelFn)
}
var lineCoordinates []image.Point
saveBresenhamPlotCoordinate := func(x, y int) { lineCoordinates = append(lineCoordinates, image.Pt(x, y)) }
p1 := image.Pt(0, 0)
p2 := image.Pt(10, 5)
bresenham.Bresenham(p1.X, p1.Y, p2.X, p2.Y, saveBresenhamPlotCoordinate)
fmt.Printf("Saved plotted coordinates from %+v to %+v using bresenham algorithm.\n%v\n", p1, p2, lineCoordinates)
Saved plotted coordinates from (0,0) to (10,5) using bresenham algorithm. [(0,0) (1,1) (2,1) (3,2) (4,2) (5,3) (6,3) (7,4) (8,4) (9,5) (10,5)]