Got rectangles all sorted
This commit is contained in:
11
artist/shapes/doc.go
Normal file
11
artist/shapes/doc.go
Normal file
@@ -0,0 +1,11 @@
|
||||
// Package shapes provides some basic shape drawing routines.
|
||||
//
|
||||
// A word about patterns:
|
||||
//
|
||||
// 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
|
||||
// patterns directly, but it is entirely possible to have a pattern draw to an
|
||||
// off-screen canvas and then draw a shape based on that canvas. As a little
|
||||
// bonus, you can save the canvas for later so you don't have to render the
|
||||
// pattern again when you need to redraw the shape.
|
||||
package shapes
|
||||
146
artist/shapes/ellipse.go
Normal file
146
artist/shapes/ellipse.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package shapes
|
||||
|
||||
import "math"
|
||||
import "image"
|
||||
import "image/color"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
||||
|
||||
// FillEllipse draws a filled ellipse with the specified pattern.
|
||||
func FillEllipse (
|
||||
destination canvas.Canvas,
|
||||
source artist.Pattern,
|
||||
bounds image.Rectangle,
|
||||
) (
|
||||
updatedRegion image.Rectangle,
|
||||
) {
|
||||
bounds = bounds.Canon()
|
||||
data, stride := destination.Buffer()
|
||||
realWidth, realHeight := bounds.Dx(), bounds.Dy()
|
||||
bounds = bounds.Intersect(destination.Bounds()).Canon()
|
||||
if bounds.Empty() { return }
|
||||
updatedRegion = bounds
|
||||
|
||||
width, height := bounds.Dx(), bounds.Dy()
|
||||
for y := 0; y < height; y ++ {
|
||||
for x := 0; x < width; x ++ {
|
||||
xf := (float64(x) + 0.5) / float64(realWidth) - 0.5
|
||||
yf := (float64(y) + 0.5) / float64(realHeight) - 0.5
|
||||
if math.Sqrt(xf * xf + yf * yf) <= 0.5 {
|
||||
data[x + bounds.Min.X + (y + bounds.Min.Y) * stride] =
|
||||
source.AtWhen(x, y, realWidth, realHeight)
|
||||
}
|
||||
}}
|
||||
return
|
||||
}
|
||||
|
||||
// StrokeEllipse draws the outline of an ellipse with the specified line weight
|
||||
// and pattern.
|
||||
func StrokeEllipse (
|
||||
destination canvas.Canvas,
|
||||
source artist.Pattern,
|
||||
weight int,
|
||||
bounds image.Rectangle,
|
||||
) {
|
||||
if weight < 1 { return }
|
||||
|
||||
data, stride := destination.Buffer()
|
||||
bounds = bounds.Canon().Inset(weight - 1)
|
||||
width, height := bounds.Dx(), bounds.Dy()
|
||||
|
||||
context := ellipsePlottingContext {
|
||||
data: data,
|
||||
stride: stride,
|
||||
source: source,
|
||||
width: width,
|
||||
height: height,
|
||||
weight: weight,
|
||||
bounds: bounds,
|
||||
}
|
||||
|
||||
bounds.Max.X -= 1
|
||||
bounds.Max.Y -= 1
|
||||
|
||||
radii := image.Pt (
|
||||
bounds.Dx() / 2,
|
||||
bounds.Dy() / 2)
|
||||
center := bounds.Min.Add(radii)
|
||||
|
||||
x := float64(0)
|
||||
y := float64(radii.Y)
|
||||
|
||||
// region 1 decision parameter
|
||||
decision1 :=
|
||||
float64(radii.Y * radii.Y) -
|
||||
float64(radii.X * radii.X * radii.Y) +
|
||||
(0.25 * float64(radii.X) * float64(radii.X))
|
||||
decisionX := float64(2 * radii.Y * radii.Y * int(x))
|
||||
decisionY := float64(2 * radii.X * radii.X * int(y))
|
||||
|
||||
// draw region 1
|
||||
for decisionX < decisionY {
|
||||
context.plot( int(x) + center.X, int(y) + center.Y)
|
||||
context.plot(-int(x) + center.X, int(y) + center.Y)
|
||||
context.plot( int(x) + center.X, -int(y) + center.Y)
|
||||
context.plot(-int(x) + center.X, -int(y) + center.Y)
|
||||
|
||||
if (decision1 < 0) {
|
||||
x ++
|
||||
decisionX += float64(2 * radii.Y * radii.Y)
|
||||
decision1 += decisionX + float64(radii.Y * radii.Y)
|
||||
} else {
|
||||
x ++
|
||||
y --
|
||||
decisionX += float64(2 * radii.Y * radii.Y)
|
||||
decisionY -= float64(2 * radii.X * radii.X)
|
||||
decision1 +=
|
||||
decisionX - decisionY +
|
||||
float64(radii.Y * radii.Y)
|
||||
}
|
||||
}
|
||||
|
||||
// region 2 decision parameter
|
||||
decision2 :=
|
||||
float64(radii.Y * radii.Y) * (x + 0.5) * (x + 0.5) +
|
||||
float64(radii.X * radii.X) * (y - 1) * (y - 1) -
|
||||
float64(radii.X * radii.X * radii.Y * radii.Y)
|
||||
|
||||
// draw region 2
|
||||
for y >= 0 {
|
||||
context.plot( int(x) + center.X, int(y) + center.Y)
|
||||
context.plot(-int(x) + center.X, int(y) + center.Y)
|
||||
context.plot( int(x) + center.X, -int(y) + center.Y)
|
||||
context.plot(-int(x) + center.X, -int(y) + center.Y)
|
||||
|
||||
if decision2 > 0 {
|
||||
y --
|
||||
decisionY -= float64(2 * radii.X * radii.X)
|
||||
decision2 += float64(radii.X * radii.X) - decisionY
|
||||
} else {
|
||||
y --
|
||||
x ++
|
||||
decisionX += float64(2 * radii.Y * radii.Y)
|
||||
decisionY -= float64(2 * radii.X * radii.X)
|
||||
decision2 +=
|
||||
decisionX - decisionY +
|
||||
float64(radii.X * radii.X)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ellipsePlottingContext struct {
|
||||
data []color.RGBA
|
||||
stride int
|
||||
source artist.Pattern
|
||||
width, height int
|
||||
weight int
|
||||
bounds image.Rectangle
|
||||
}
|
||||
|
||||
func (context ellipsePlottingContext) plot (x, y int) {
|
||||
if (image.Point { x, y }).In(context.bounds) {
|
||||
squareAround (
|
||||
context.data, context.stride, context.source, x, y,
|
||||
context.width, context.height, context.weight)
|
||||
}
|
||||
}
|
||||
143
artist/shapes/line.go
Normal file
143
artist/shapes/line.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package shapes
|
||||
|
||||
import "image"
|
||||
import "image/color"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
||||
|
||||
// TODO: draw thick lines more efficiently
|
||||
|
||||
// Line draws a line from one point to another with the specified weight and
|
||||
// pattern.
|
||||
func Line (
|
||||
destination canvas.Canvas,
|
||||
source canvas.Canvas,
|
||||
weight int,
|
||||
min image.Point,
|
||||
max image.Point,
|
||||
) (
|
||||
updatedRegion image.Rectangle,
|
||||
) {
|
||||
|
||||
updatedRegion = image.Rectangle { Min: min, Max: max }.Canon()
|
||||
updatedRegion.Max.X ++
|
||||
updatedRegion.Max.Y ++
|
||||
width := updatedRegion.Dx()
|
||||
height := updatedRegion.Dy()
|
||||
|
||||
if abs(max.Y - min.Y) <
|
||||
abs(max.X - min.X) {
|
||||
|
||||
if max.X < min.X {
|
||||
temp := min
|
||||
min = max
|
||||
max = temp
|
||||
}
|
||||
lineLow(destination, source, weight, min, max, width, height)
|
||||
} else {
|
||||
|
||||
if max.Y < min.Y {
|
||||
temp := min
|
||||
min = max
|
||||
max = temp
|
||||
}
|
||||
lineHigh(destination, source, weight, min, max, width, height)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func lineLow (
|
||||
destination canvas.Canvas,
|
||||
source Pattern,
|
||||
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
|
||||
yi := 1
|
||||
|
||||
if deltaY < 0 {
|
||||
yi = -1
|
||||
deltaY *= -1
|
||||
}
|
||||
|
||||
D := (2 * deltaY) - deltaX
|
||||
y := min.Y
|
||||
|
||||
for x := min.X; x < max.X; x ++ {
|
||||
if !(image.Point { x, y }).In(bounds) { break }
|
||||
squareAround(data, stride, source, x, y, width, height, weight)
|
||||
// data[x + y * stride] = source.AtWhen(x, y, width, height)
|
||||
if D > 0 {
|
||||
y += yi
|
||||
D += 2 * (deltaY - deltaX)
|
||||
} else {
|
||||
D += 2 * deltaY
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func lineHigh (
|
||||
destination canvas.Canvas,
|
||||
source Pattern,
|
||||
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
|
||||
|
||||
if deltaX < 0 {
|
||||
xi = -1
|
||||
deltaX *= -1
|
||||
}
|
||||
|
||||
D := (2 * deltaX) - deltaY
|
||||
x := min.X
|
||||
|
||||
for y := min.Y; y < max.Y; y ++ {
|
||||
if !(image.Point { x, y }).In(bounds) { break }
|
||||
squareAround(data, stride, source, x, y, width, height, weight)
|
||||
// data[x + y * stride] = source.AtWhen(x, y, width, height)
|
||||
if D > 0 {
|
||||
x += xi
|
||||
D += 2 * (deltaX - deltaY)
|
||||
} else {
|
||||
D += 2 * deltaX
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func abs (in int) (out int) {
|
||||
if in < 0 { in *= -1}
|
||||
out = in
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: this method of doing things sucks and can cause a segfault. we should
|
||||
// not be doing it this way
|
||||
func squareAround (
|
||||
data []color.RGBA,
|
||||
stride int,
|
||||
source Pattern,
|
||||
x, y, patternWidth, patternHeight, diameter int,
|
||||
) {
|
||||
minY := y - diameter + 1
|
||||
minX := x - diameter + 1
|
||||
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)
|
||||
}}
|
||||
}
|
||||
84
artist/shapes/rectangle.go
Normal file
84
artist/shapes/rectangle.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package shapes
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/shatter"
|
||||
|
||||
// FillRectangle draws a rectangular subset of one canvas onto the other. 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().
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
// specified in "rocks".
|
||||
func FillRectangleShatter (
|
||||
destination canvas.Canvas,
|
||||
source canvas.Canvas,
|
||||
offset image.Point,
|
||||
rocks []image.Rectangle,
|
||||
) {
|
||||
tiles := shatter.Shatter(source.Bounds())
|
||||
for _, tile := range tiles {
|
||||
tile
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user