2023-02-24 01:55:19 +00:00
|
|
|
package shapes
|
2023-01-09 06:03:19 +00:00
|
|
|
|
|
|
|
import "image"
|
2023-01-21 00:52:35 +00:00
|
|
|
import "image/color"
|
2023-04-30 17:45:21 +00:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
2023-01-09 06:03:19 +00:00
|
|
|
|
2023-02-24 07:26:34 +00:00
|
|
|
// ColorLine draws a line from one point to another with the specified weight
|
|
|
|
// and color.
|
|
|
|
func ColorLine (
|
2023-04-30 17:45:21 +00:00
|
|
|
destination artist.Canvas,
|
2023-02-24 07:26:34 +00:00
|
|
|
color color.RGBA,
|
|
|
|
weight int,
|
|
|
|
min image.Point,
|
|
|
|
max image.Point,
|
2023-01-09 06:03:19 +00:00
|
|
|
) (
|
|
|
|
updatedRegion image.Rectangle,
|
|
|
|
) {
|
|
|
|
updatedRegion = image.Rectangle { Min: min, Max: max }.Canon()
|
|
|
|
updatedRegion.Max.X ++
|
|
|
|
updatedRegion.Max.Y ++
|
|
|
|
|
2023-02-24 07:26:34 +00:00
|
|
|
data, stride := destination.Buffer()
|
|
|
|
bounds := destination.Bounds()
|
|
|
|
context := linePlottingContext {
|
2023-02-24 07:51:24 +00:00
|
|
|
plottingContext: plottingContext {
|
|
|
|
dstData: data,
|
|
|
|
dstStride: stride,
|
|
|
|
color: color,
|
|
|
|
weight: weight,
|
|
|
|
bounds: bounds,
|
|
|
|
},
|
|
|
|
min: min,
|
|
|
|
max: max,
|
2023-02-24 07:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if abs(max.Y - min.Y) < abs(max.X - min.X) {
|
|
|
|
if max.X < min.X { context.swap() }
|
|
|
|
context.lineLow()
|
2023-01-09 06:03:19 +00:00
|
|
|
|
|
|
|
} else {
|
2023-02-24 07:26:34 +00:00
|
|
|
if max.Y < min.Y { context.swap() }
|
|
|
|
context.lineHigh()
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-24 07:26:34 +00:00
|
|
|
type linePlottingContext struct {
|
2023-02-24 07:51:24 +00:00
|
|
|
plottingContext
|
|
|
|
min image.Point
|
|
|
|
max image.Point
|
2023-02-24 07:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (context *linePlottingContext) swap () {
|
|
|
|
temp := context.max
|
|
|
|
context.max = context.min
|
|
|
|
context.min = temp
|
|
|
|
}
|
2023-01-14 06:54:57 +00:00
|
|
|
|
2023-02-24 07:26:34 +00:00
|
|
|
func (context linePlottingContext) lineLow () {
|
|
|
|
deltaX := context.max.X - context.min.X
|
|
|
|
deltaY := context.max.Y - context.min.Y
|
2023-01-09 06:03:19 +00:00
|
|
|
yi := 1
|
|
|
|
|
|
|
|
if deltaY < 0 {
|
|
|
|
yi = -1
|
|
|
|
deltaY *= -1
|
|
|
|
}
|
|
|
|
|
|
|
|
D := (2 * deltaY) - deltaX
|
2023-02-24 07:26:34 +00:00
|
|
|
point := context.min
|
2023-01-09 06:03:19 +00:00
|
|
|
|
2023-02-24 07:26:34 +00:00
|
|
|
for ; point.X < context.max.X; point.X ++ {
|
2023-02-24 07:51:24 +00:00
|
|
|
context.plotColor(point)
|
2023-01-09 06:03:19 +00:00
|
|
|
if D > 0 {
|
|
|
|
D += 2 * (deltaY - deltaX)
|
2023-02-24 07:26:34 +00:00
|
|
|
point.Y += yi
|
2023-01-09 06:03:19 +00:00
|
|
|
} else {
|
|
|
|
D += 2 * deltaY
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 07:26:34 +00:00
|
|
|
func (context linePlottingContext) lineHigh () {
|
|
|
|
deltaX := context.max.X - context.min.X
|
|
|
|
deltaY := context.max.Y - context.min.Y
|
2023-01-09 06:03:19 +00:00
|
|
|
xi := 1
|
|
|
|
|
|
|
|
if deltaX < 0 {
|
|
|
|
xi = -1
|
|
|
|
deltaX *= -1
|
|
|
|
}
|
|
|
|
|
|
|
|
D := (2 * deltaX) - deltaY
|
2023-02-24 07:26:34 +00:00
|
|
|
point := context.min
|
2023-01-09 06:03:19 +00:00
|
|
|
|
2023-02-24 07:26:34 +00:00
|
|
|
for ; point.Y < context.max.Y; point.Y ++ {
|
2023-02-24 07:51:24 +00:00
|
|
|
context.plotColor(point)
|
2023-01-09 06:03:19 +00:00
|
|
|
if D > 0 {
|
2023-02-24 07:26:34 +00:00
|
|
|
point.X += xi
|
2023-01-09 06:03:19 +00:00
|
|
|
D += 2 * (deltaX - deltaY)
|
|
|
|
} else {
|
|
|
|
D += 2 * deltaX
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 07:26:34 +00:00
|
|
|
func abs (n int) int {
|
|
|
|
if n < 0 { n *= -1}
|
|
|
|
return n
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|