data-oriented-patterns #9
@ -1,6 +1,6 @@
|
|||||||
// Package shapes provides some basic shape drawing routines.
|
// Package shapes provides some basic shape drawing routines.
|
||||||
//
|
//
|
||||||
// A word about patterns:
|
// A word about using patterns with shape routines:
|
||||||
//
|
//
|
||||||
// Most drawing routines have a version that samples from other canvases, and a
|
// Most drawing routines have a version that samples from other canvases, and a
|
||||||
// version that samples from a solid color. None of these routines can use
|
// version that samples from a solid color. None of these routines can use
|
||||||
|
@ -3,19 +3,25 @@ package shapes
|
|||||||
import "math"
|
import "math"
|
||||||
import "image"
|
import "image"
|
||||||
import "image/color"
|
import "image/color"
|
||||||
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
// import "git.tebibyte.media/sashakoshka/tomo/artist"
|
||||||
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
||||||
|
|
||||||
// FillEllipse draws a filled ellipse with the specified pattern.
|
// FillEllipse draws the content of one canvas onto another, clipped by an
|
||||||
|
// ellipse stretched to the bounds of the source canvas. 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's bounds from being used, it must be cut with canvas.Cut().
|
||||||
func FillEllipse (
|
func FillEllipse (
|
||||||
destination canvas.Canvas,
|
destination canvas.Canvas,
|
||||||
source artist.Pattern,
|
source canvas.Canvas,
|
||||||
bounds image.Rectangle,
|
offset image.Point,
|
||||||
) (
|
) (
|
||||||
updatedRegion image.Rectangle,
|
updatedRegion image.Rectangle,
|
||||||
) {
|
) {
|
||||||
bounds = bounds.Canon()
|
dstData, dstStride := destination.Buffer()
|
||||||
data, stride := destination.Buffer()
|
srcData, srcStride := source.Buffer()
|
||||||
|
|
||||||
|
bounds := source.Bounds()
|
||||||
realWidth, realHeight := bounds.Dx(), bounds.Dy()
|
realWidth, realHeight := bounds.Dx(), bounds.Dy()
|
||||||
bounds = bounds.Intersect(destination.Bounds()).Canon()
|
bounds = bounds.Intersect(destination.Bounds()).Canon()
|
||||||
if bounds.Empty() { return }
|
if bounds.Empty() { return }
|
||||||
@ -27,35 +33,38 @@ func FillEllipse (
|
|||||||
xf := (float64(x) + 0.5) / float64(realWidth) - 0.5
|
xf := (float64(x) + 0.5) / float64(realWidth) - 0.5
|
||||||
yf := (float64(y) + 0.5) / float64(realHeight) - 0.5
|
yf := (float64(y) + 0.5) / float64(realHeight) - 0.5
|
||||||
if math.Sqrt(xf * xf + yf * yf) <= 0.5 {
|
if math.Sqrt(xf * xf + yf * yf) <= 0.5 {
|
||||||
data[x + bounds.Min.X + (y + bounds.Min.Y) * stride] =
|
dstData[x + offset.X + (y + offset.Y) * dstStride] =
|
||||||
source.AtWhen(x, y, realWidth, realHeight)
|
srcData[x + y * srcStride]
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// StrokeEllipse draws the outline of an ellipse with the specified line weight
|
// StrokeRectangle is similar to FillEllipse, but it draws an elliptical inset
|
||||||
// and pattern.
|
// 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 StrokeEllipse (
|
func StrokeEllipse (
|
||||||
destination canvas.Canvas,
|
destination canvas.Canvas,
|
||||||
source artist.Pattern,
|
source canvas.Canvas,
|
||||||
weight int,
|
offset image.Point,
|
||||||
bounds image.Rectangle,
|
weight int,
|
||||||
) {
|
) {
|
||||||
if weight < 1 { return }
|
if weight < 1 { return }
|
||||||
|
|
||||||
data, stride := destination.Buffer()
|
dstData, dstStride := destination.Buffer()
|
||||||
bounds = bounds.Canon().Inset(weight - 1)
|
srcData, srcStride := source.Buffer()
|
||||||
width, height := bounds.Dx(), bounds.Dy()
|
|
||||||
|
bounds := source.Bounds().Inset(weight - 1)
|
||||||
|
|
||||||
context := ellipsePlottingContext {
|
context := ellipsePlottingContext {
|
||||||
data: data,
|
dstData: dstData,
|
||||||
stride: stride,
|
dstStride: dstStride,
|
||||||
source: source,
|
srcData: srcData,
|
||||||
width: width,
|
srcStride: srcStride,
|
||||||
height: height,
|
weight: weight,
|
||||||
weight: weight,
|
offset: offset,
|
||||||
bounds: bounds,
|
bounds: bounds.Intersect(destination.Bounds()),
|
||||||
}
|
}
|
||||||
|
|
||||||
bounds.Max.X -= 1
|
bounds.Max.X -= 1
|
||||||
@ -129,18 +138,26 @@ func StrokeEllipse (
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ellipsePlottingContext struct {
|
type ellipsePlottingContext struct {
|
||||||
data []color.RGBA
|
dstData []color.RGBA
|
||||||
stride int
|
dstStride int
|
||||||
source artist.Pattern
|
srcData []color.RGBA
|
||||||
width, height int
|
srcStride int
|
||||||
weight int
|
weight int
|
||||||
bounds image.Rectangle
|
offset image.Point
|
||||||
|
bounds image.Rectangle
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context ellipsePlottingContext) plot (x, y int) {
|
func (context ellipsePlottingContext) plot (x, y int) {
|
||||||
if (image.Point { x, y }).In(context.bounds) {
|
square :=
|
||||||
squareAround (
|
image.Rect(0, 0, context.weight, context.weight).
|
||||||
context.data, context.stride, context.source, x, y,
|
Sub(image.Pt(context.weight / 2, context.weight / 2)).
|
||||||
context.width, context.height, context.weight)
|
Add(image.Pt(x, y)).
|
||||||
}
|
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.srcData [
|
||||||
|
x + y * context.dstStride]
|
||||||
|
}}
|
||||||
}
|
}
|
||||||
|
@ -6,14 +6,14 @@ import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
|||||||
|
|
||||||
// TODO: draw thick lines more efficiently
|
// TODO: draw thick lines more efficiently
|
||||||
|
|
||||||
// Line draws a line from one point to another with the specified weight and
|
// ColorLine draws a line from one point to another with the specified weight
|
||||||
// pattern.
|
// and color.
|
||||||
func Line (
|
func ColorLine (
|
||||||
destination canvas.Canvas,
|
destination canvas.Canvas,
|
||||||
source canvas.Canvas,
|
color color.RGBA,
|
||||||
weight int,
|
weight int,
|
||||||
min image.Point,
|
min image.Point,
|
||||||
max image.Point,
|
max image.Point,
|
||||||
) (
|
) (
|
||||||
updatedRegion image.Rectangle,
|
updatedRegion image.Rectangle,
|
||||||
) {
|
) {
|
||||||
@ -21,43 +21,49 @@ func Line (
|
|||||||
updatedRegion = image.Rectangle { Min: min, Max: max }.Canon()
|
updatedRegion = image.Rectangle { Min: min, Max: max }.Canon()
|
||||||
updatedRegion.Max.X ++
|
updatedRegion.Max.X ++
|
||||||
updatedRegion.Max.Y ++
|
updatedRegion.Max.Y ++
|
||||||
width := updatedRegion.Dx()
|
|
||||||
height := updatedRegion.Dy()
|
|
||||||
|
|
||||||
if abs(max.Y - min.Y) <
|
data, stride := destination.Buffer()
|
||||||
abs(max.X - min.X) {
|
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()
|
||||||
|
|
||||||
if max.X < min.X {
|
|
||||||
temp := min
|
|
||||||
min = max
|
|
||||||
max = temp
|
|
||||||
}
|
|
||||||
lineLow(destination, source, weight, min, max, width, height)
|
|
||||||
} else {
|
} else {
|
||||||
|
if max.Y < min.Y { context.swap() }
|
||||||
if max.Y < min.Y {
|
context.lineHigh()
|
||||||
temp := min
|
|
||||||
min = max
|
|
||||||
max = temp
|
|
||||||
}
|
|
||||||
lineHigh(destination, source, weight, min, max, width, height)
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func lineLow (
|
type linePlottingContext struct {
|
||||||
destination canvas.Canvas,
|
dstData []color.RGBA
|
||||||
source Pattern,
|
dstStride int
|
||||||
weight int,
|
color color.RGBA
|
||||||
min image.Point,
|
weight int
|
||||||
max image.Point,
|
bounds image.Rectangle
|
||||||
width, height int,
|
min image.Point
|
||||||
) {
|
max image.Point
|
||||||
data, stride := destination.Buffer()
|
}
|
||||||
bounds := destination.Bounds()
|
|
||||||
|
|
||||||
deltaX := max.X - min.X
|
func (context *linePlottingContext) swap () {
|
||||||
deltaY := max.Y - min.Y
|
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
|
||||||
yi := 1
|
yi := 1
|
||||||
|
|
||||||
if deltaY < 0 {
|
if deltaY < 0 {
|
||||||
@ -66,34 +72,23 @@ func lineLow (
|
|||||||
}
|
}
|
||||||
|
|
||||||
D := (2 * deltaY) - deltaX
|
D := (2 * deltaY) - deltaX
|
||||||
y := min.Y
|
point := context.min
|
||||||
|
|
||||||
for x := min.X; x < max.X; x ++ {
|
for ; point.X < context.max.X; point.X ++ {
|
||||||
if !(image.Point { x, y }).In(bounds) { break }
|
if !point.In(context.bounds) { break }
|
||||||
squareAround(data, stride, source, x, y, width, height, weight)
|
context.plot(point)
|
||||||
// data[x + y * stride] = source.AtWhen(x, y, width, height)
|
|
||||||
if D > 0 {
|
if D > 0 {
|
||||||
y += yi
|
|
||||||
D += 2 * (deltaY - deltaX)
|
D += 2 * (deltaY - deltaX)
|
||||||
|
point.Y += yi
|
||||||
} else {
|
} else {
|
||||||
D += 2 * deltaY
|
D += 2 * deltaY
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func lineHigh (
|
func (context linePlottingContext) lineHigh () {
|
||||||
destination canvas.Canvas,
|
deltaX := context.max.X - context.min.X
|
||||||
source Pattern,
|
deltaY := context.max.Y - context.min.Y
|
||||||
weight int,
|
|
||||||
min image.Point,
|
|
||||||
max image.Point,
|
|
||||||
width, height int,
|
|
||||||
) {
|
|
||||||
data, stride := destination.Buffer()
|
|
||||||
bounds := destination.Bounds()
|
|
||||||
|
|
||||||
deltaX := max.X - min.X
|
|
||||||
deltaY := max.Y - min.Y
|
|
||||||
xi := 1
|
xi := 1
|
||||||
|
|
||||||
if deltaX < 0 {
|
if deltaX < 0 {
|
||||||
@ -102,14 +97,13 @@ func lineHigh (
|
|||||||
}
|
}
|
||||||
|
|
||||||
D := (2 * deltaX) - deltaY
|
D := (2 * deltaX) - deltaY
|
||||||
x := min.X
|
point := context.min
|
||||||
|
|
||||||
for y := min.Y; y < max.Y; y ++ {
|
for ; point.Y < context.max.Y; point.Y ++ {
|
||||||
if !(image.Point { x, y }).In(bounds) { break }
|
if !point.In(context.bounds) { break }
|
||||||
squareAround(data, stride, source, x, y, width, height, weight)
|
context.plot(point)
|
||||||
// data[x + y * stride] = source.AtWhen(x, y, width, height)
|
|
||||||
if D > 0 {
|
if D > 0 {
|
||||||
x += xi
|
point.X += xi
|
||||||
D += 2 * (deltaX - deltaY)
|
D += 2 * (deltaX - deltaY)
|
||||||
} else {
|
} else {
|
||||||
D += 2 * deltaX
|
D += 2 * deltaX
|
||||||
@ -117,27 +111,20 @@ func lineHigh (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func abs (in int) (out int) {
|
func abs (n int) int {
|
||||||
if in < 0 { in *= -1}
|
if n < 0 { n *= -1}
|
||||||
out = in
|
return n
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this method of doing things sucks and can cause a segfault. we should
|
func (context linePlottingContext) plot (center image.Point) {
|
||||||
// not be doing it this way
|
square :=
|
||||||
func squareAround (
|
image.Rect(0, 0, context.weight, context.weight).
|
||||||
data []color.RGBA,
|
Sub(image.Pt(context.weight / 2, context.weight / 2)).
|
||||||
stride int,
|
Add(center).
|
||||||
source Pattern,
|
Intersect(context.bounds)
|
||||||
x, y, patternWidth, patternHeight, diameter int,
|
|
||||||
) {
|
for y := square.Min.Y; y < square.Min.Y; y ++ {
|
||||||
minY := y - diameter + 1
|
for x := square.Min.X; x < square.Min.X; x ++ {
|
||||||
minX := x - diameter + 1
|
context.dstData[x + y * context.dstStride] = context.color
|
||||||
maxY := y + diameter
|
|
||||||
maxX := x + diameter
|
|
||||||
for y = minY; y < maxY; y ++ {
|
|
||||||
for x = minX; x < maxX; x ++ {
|
|
||||||
data[x + y * stride] =
|
|
||||||
source.AtWhen(x, y, patternWidth, patternHeight)
|
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,10 @@ import "image"
|
|||||||
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
||||||
import "git.tebibyte.media/sashakoshka/tomo/shatter"
|
import "git.tebibyte.media/sashakoshka/tomo/shatter"
|
||||||
|
|
||||||
// FillRectangle draws a rectangular subset of one canvas onto the other. The
|
// FillRectangle draws the content of one canvas onto another. The offset point
|
||||||
// offset point defines where the origin point of the source canvas is
|
// defines where the origin point of the source canvas is positioned in relation
|
||||||
// positioned in relation to the origin point of the destination canvas. To
|
// to the origin point of the destination canvas. To prevent the entire source
|
||||||
// prevent the entire source canvas from being drawn, it must be cut with
|
// canvas from being drawn, it must be cut with canvas.Cut().
|
||||||
// canvas.Cut().
|
|
||||||
func FillRectangle (
|
func FillRectangle (
|
||||||
destination canvas.Canvas,
|
destination canvas.Canvas,
|
||||||
source canvas.Canvas,
|
source canvas.Canvas,
|
||||||
@ -49,24 +48,7 @@ func StrokeRectangle (
|
|||||||
FillRectangle(destination, source, offset)
|
FillRectangle(destination, source, offset)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
FillRectangleShatter(destination, source, offset, insetBounds)
|
||||||
top := image.Rect (
|
|
||||||
bounds.Min.X, bounds.Min.Y,
|
|
||||||
bounds.Max.X, insetBounds.Min.Y)
|
|
||||||
bottom := image.Rect (
|
|
||||||
bounds.Min.X, insetBounds.Max.Y,
|
|
||||||
bounds.Max.X, bounds.Max.Y)
|
|
||||||
left := image.Rect (
|
|
||||||
bounds.Min.X, insetBounds.Min.Y,
|
|
||||||
insetBounds.Min.X, insetBounds.Max.Y)
|
|
||||||
right := image.Rect (
|
|
||||||
insetBounds.Max.X, insetBounds.Min.Y,
|
|
||||||
bounds.Max.X, insetBounds.Max.Y)
|
|
||||||
|
|
||||||
FillRectangle (destination, canvas.Cut(source, top), offset)
|
|
||||||
FillRectangle (destination, canvas.Cut(source, bottom), offset)
|
|
||||||
FillRectangle (destination, canvas.Cut(source, left), offset)
|
|
||||||
FillRectangle (destination, canvas.Cut(source, right), offset)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FillRectangleShatter is like FillRectangle, but it does not draw in areas
|
// FillRectangleShatter is like FillRectangle, but it does not draw in areas
|
||||||
@ -75,10 +57,10 @@ func FillRectangleShatter (
|
|||||||
destination canvas.Canvas,
|
destination canvas.Canvas,
|
||||||
source canvas.Canvas,
|
source canvas.Canvas,
|
||||||
offset image.Point,
|
offset image.Point,
|
||||||
rocks []image.Rectangle,
|
rocks ...image.Rectangle,
|
||||||
) {
|
) {
|
||||||
tiles := shatter.Shatter(source.Bounds())
|
tiles := shatter.Shatter(source.Bounds(), rocks...)
|
||||||
for _, tile := range tiles {
|
for _, tile := range tiles {
|
||||||
tile
|
FillRectangle(destination, canvas.Cut(source, tile), offset)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user