2018-06-17 17:29:57 +00:00
|
|
|
package draw
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
2019-12-23 02:21:58 +00:00
|
|
|
"git.kirsle.net/go/render"
|
2018-06-17 17:29:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Line is a generator that returns the X,Y coordinates to draw a line.
|
|
|
|
// https://en.wikipedia.org/wiki/Digital_differential_analyzer_(graphics_algorithm)
|
2018-07-24 03:10:53 +00:00
|
|
|
func Line(x1, y1, x2, y2 int32) chan render.Point {
|
|
|
|
generator := make(chan render.Point)
|
2018-06-17 17:29:57 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
var (
|
|
|
|
dx = float64(x2 - x1)
|
|
|
|
dy = float64(y2 - y1)
|
|
|
|
)
|
|
|
|
var step float64
|
|
|
|
if math.Abs(dx) >= math.Abs(dy) {
|
|
|
|
step = math.Abs(dx)
|
|
|
|
} else {
|
|
|
|
step = math.Abs(dy)
|
|
|
|
}
|
|
|
|
|
|
|
|
dx = dx / step
|
|
|
|
dy = dy / step
|
|
|
|
x := float64(x1)
|
|
|
|
y := float64(y1)
|
|
|
|
for i := 0; i <= int(step); i++ {
|
2018-07-24 03:10:53 +00:00
|
|
|
generator <- render.Point{
|
2018-06-17 17:29:57 +00:00
|
|
|
X: int32(x),
|
|
|
|
Y: int32(y),
|
|
|
|
}
|
|
|
|
x += dx
|
|
|
|
y += dy
|
|
|
|
}
|
|
|
|
|
|
|
|
close(generator)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return generator
|
|
|
|
}
|