8000 GitHub - chran554/go-bresenham: A (cpu) optimized line drawing algorithm in go. The algorithm is generilized for multipurpose use (not only pixel drawing in images, although it can). It is not optimized for GPUs only CPUs..
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

A (cpu) optimized line drawing algorithm in go. The algorithm is generilized for multipurpose use (not only pixel drawing in images, although it can). It is not optimized for GPUs only CPUs..

License

Notifications You must be signed in to change notification settings

chran554/go-bresenham

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bresenham line

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.

Install

go get github.com/chran554/go-bresenham@latest
import "github.com/chran554/go-bresenham"

Usage

To draw a line from point (p1x, p1y) to (p2x, p2y) simply call bresenham.Bresenham function.

Usage of 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.

License

CC0

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.

Dependencies

Nope. No exotic dependencies to mention.

Examples

Print coordinates

Print all generated coordinates along the line to std out.
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()
Output
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)

Draw line onto an image

Plot a line in an image
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)
}

Save coordinates in slice

Save line plot coordinates in a slice
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)
Output
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)]

Source of information

About

A (cpu) optimized line drawing algorithm in go. The algorithm is generilized for multipurpose use (not only pixel drawing in images, although it can). It is not optimized for GPUs only CPUs..

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

0