This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/artist/shapes/line.go

131 lines
2.6 KiB
Go
Raw Normal View History

2023-02-23 18:55:19 -07:00
package shapes
2023-01-08 23:03:19 -07:00
import "image"
2023-01-20 17:52:35 -07:00
import "image/color"
2023-02-01 23:48:16 -07:00
import "git.tebibyte.media/sashakoshka/tomo/canvas"
2023-01-08 23:03:19 -07:00
2023-01-20 17:52:35 -07:00
// TODO: draw thick lines more efficiently
// ColorLine draws a line from one point to another with the specified weight
// and color.
func ColorLine (
2023-02-01 23:48:16 -07:00
destination canvas.Canvas,
color color.RGBA,
weight int,
min image.Point,
max image.Point,
2023-01-08 23:03:19 -07:00
) (
updatedRegion image.Rectangle,
) {
updatedRegion = image.Rectangle { Min: min, Max: max }.Canon()
updatedRegion.Max.X ++
updatedRegion.Max.Y ++
data, stride := destination.Buffer()
bounds := destination.Bounds()
context := linePlottingContext {
dstData: data,
dstStride: stride,
color: color,
weight: weight,
bounds: bounds,
min: min,
max: max,
}
if abs(max.Y - min.Y) < abs(max.X - min.X) {
if max.X < min.X { context.swap() }
context.lineLow()
2023-01-08 23:03:19 -07:00
} else {
if max.Y < min.Y { context.swap() }
context.lineHigh()
2023-01-08 23:03:19 -07:00
}
return
}
type linePlottingContext struct {
dstData []color.RGBA
dstStride int
color color.RGBA
weight int
bounds image.Rectangle
min image.Point
max image.Point
}
func (context *linePlottingContext) swap () {
temp := context.max
context.max = context.min
context.min = temp
}
func (context linePlottingContext) lineLow () {
deltaX := context.max.X - context.min.X
deltaY := context.max.Y - context.min.Y
2023-01-08 23:03:19 -07:00
yi := 1
if deltaY < 0 {
yi = -1
deltaY *= -1
}
D := (2 * deltaY) - deltaX
point := context.min
2023-01-08 23:03:19 -07:00
for ; point.X < context.max.X; point.X ++ {
if !point.In(context.bounds) { break }
context.plot(point)
2023-01-08 23:03:19 -07:00
if D > 0 {
D += 2 * (deltaY - deltaX)
point.Y += yi
2023-01-08 23:03:19 -07:00
} else {
D += 2 * deltaY
}
}
}
func (context linePlottingContext) lineHigh () {
deltaX := context.max.X - context.min.X
deltaY := context.max.Y - context.min.Y
2023-01-08 23:03:19 -07:00
xi := 1
if deltaX < 0 {
xi = -1
deltaX *= -1
}
D := (2 * deltaX) - deltaY
point := context.min
2023-01-08 23:03:19 -07:00
for ; point.Y < context.max.Y; point.Y ++ {
if !point.In(context.bounds) { break }
context.plot(point)
2023-01-08 23:03:19 -07:00
if D > 0 {
point.X += xi
2023-01-08 23:03:19 -07:00
D += 2 * (deltaX - deltaY)
} else {
D += 2 * deltaX
}
}
}
func abs (n int) int {
if n < 0 { n *= -1}
return n
2023-01-08 23:03:19 -07:00
}
2023-01-20 17:52:35 -07:00
func (context linePlottingContext) plot (center image.Point) {
square :=
image.Rect(0, 0, context.weight, context.weight).
Sub(image.Pt(context.weight / 2, context.weight / 2)).
Add(center).
Intersect(context.bounds)
for y := square.Min.Y; y < square.Min.Y; y ++ {
for x := square.Min.X; x < square.Min.X; x ++ {
context.dstData[x + y * context.dstStride] = context.color
2023-01-20 17:52:35 -07:00
}}
}