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/rectangle.go

67 lines
2.0 KiB
Go
Raw Normal View History

2023-02-24 01:55:19 +00:00
package shapes
import "image"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/shatter"
// FillRectangle draws the content of one canvas onto another. The offset point
// defines where the origin point of the source canvas is positioned in relation
// to the origin point of the destination canvas. To prevent the entire source
// canvas from being drawn, it must be cut with canvas.Cut().
2023-02-24 01:55:19 +00:00
func FillRectangle (
destination canvas.Canvas,
source canvas.Canvas,
offset image.Point,
) (
updatedRegion image.Rectangle,
) {
dstData, dstStride := destination.Buffer()
srcData, srcStride := source.Buffer()
sourceBounds :=
source.Bounds().Canon().
Intersect(destination.Bounds().Sub(offset))
if sourceBounds.Empty() { return }
updatedRegion = sourceBounds.Add(offset)
for y := sourceBounds.Min.Y; y < sourceBounds.Max.Y; y ++ {
for x := sourceBounds.Min.X; x < sourceBounds.Max.X; x ++ {
dstData[x + offset.X + (y + offset.Y) * dstStride] =
srcData[x + y * srcStride]
}}
return
}
// StrokeRectangle is similar to FillRectangle, but it draws an inset outline of
// the source canvas onto the destination canvas. To prevent the entire source
// canvas's bounds from being used, it must be cut with canvas.Cut().
func StrokeRectangle (
destination canvas.Canvas,
source canvas.Canvas,
offset image.Point,
weight int,
) {
bounds := source.Bounds()
insetBounds := bounds.Inset(weight)
if insetBounds.Empty() {
FillRectangle(destination, source, offset)
return
}
FillRectangleShatter(destination, source, offset, insetBounds)
2023-02-24 01:55:19 +00:00
}
// FillRectangleShatter is like FillRectangle, but it does not draw in areas
// specified in "rocks".
func FillRectangleShatter (
destination canvas.Canvas,
source canvas.Canvas,
offset image.Point,
rocks ...image.Rectangle,
2023-02-24 01:55:19 +00:00
) {
tiles := shatter.Shatter(source.Bounds(), rocks...)
2023-02-24 01:55:19 +00:00
for _, tile := range tiles {
FillRectangle(destination, canvas.Cut(source, tile), offset)
2023-02-24 01:55:19 +00:00
}
}