bresenham

package module
v0.0.0-...-ec76d7b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2021 License: BSD-3-Clause Imports: 2 Imported by: 12

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bresenham

func Bresenham(p Plotter, x1, y1, x2, y2 int, col color.Color)

Generalized with integer

func BresenhamDxXRYD

func BresenhamDxXRYD(img draw.Image, x1, y1, x2, y2 int, col color.Color)

dx > dy; x1 < x2; y1 < y2

func BresenhamDxXRYU

func BresenhamDxXRYU(img draw.Image, x1, y1, x2, y2 int, col color.Color)

dx > dy; x1 < x2; y1 > y2

func BresenhamDyXRYD

func BresenhamDyXRYD(img draw.Image, x1, y1, x2, y2 int, col color.Color)

dy > dx; x1 < x2; y1 < y2

func BresenhamDyXRYU

func BresenhamDyXRYU(img draw.Image, x1, y1, x2, y2 int, col color.Color)

func Bresenham_1

func Bresenham_1(img draw.Image, x1, y1, x2, y2 int, col color.Color)

Floating point

func Bresenham_2

func Bresenham_2(img draw.Image, x1, y1, x2, y2 int, col color.Color)

Floating point with error accumulator

func Bresenham_3

func Bresenham_3(img draw.Image, x1, y1, x2, y2 int, col color.Color)

Integer float -> float * dx -> integer

func Bresenham_4

func Bresenham_4(img draw.Image, x1, y1, x2, y2 int, col color.Color)

Integer; remove comparison (cmp -> bit test); remove variables; float -> float * 2 * dx -> integer

func DrawLine

func DrawLine(p Plotter, x1, y1, x2, y2 int, col color.Color)

Types

type Plotter

type Plotter interface {
	Set(x int, y int, c color.Color)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL