Ellipse and rectangle have both color and source routines

This commit is contained in:
Sasha Koshka 2023-02-24 16:31:42 -05:00
parent 211219eb01
commit bf2fdb5eaa
4 changed files with 215 additions and 60 deletions

View File

@ -2,6 +2,7 @@ package shapes
import "math" import "math"
import "image" import "image"
import "image/color"
import "git.tebibyte.media/sashakoshka/tomo/canvas" import "git.tebibyte.media/sashakoshka/tomo/canvas"
// FillEllipse draws the content of one canvas onto another, clipped by an // FillEllipse draws the content of one canvas onto another, clipped by an
@ -19,26 +20,25 @@ func FillEllipse (
dstData, dstStride := destination.Buffer() dstData, dstStride := destination.Buffer()
srcData, srcStride := source.Buffer() srcData, srcStride := source.Buffer()
bounds := source.Bounds() bounds := source.Bounds().Intersect(destination.Bounds()).Canon()
realWidth, realHeight := bounds.Dx(), bounds.Dy() realBounds := source.Bounds()
bounds = bounds.Intersect(destination.Bounds()).Canon()
if bounds.Empty() { return } if bounds.Empty() { return }
updatedRegion = bounds updatedRegion = bounds
width, height := bounds.Dx(), bounds.Dy() point := image.Point { }
for y := 0; y < height; y ++ { for point.Y = bounds.Min.Y; point.Y < bounds.Max.Y; point.Y ++ {
for x := 0; x < width; x ++ { for point.X = bounds.Min.X; point.X < bounds.Max.X; point.X ++ {
xf := (float64(x) + 0.5) / float64(realWidth) - 0.5 if inEllipse(point, realBounds) {
yf := (float64(y) + 0.5) / float64(realHeight) - 0.5 offsetPoint := point.Add(offset)
if math.Sqrt(xf * xf + yf * yf) <= 0.5 { dstIndex := offsetPoint.X + (offsetPoint.Y) * dstStride
dstData[x + offset.X + (y + offset.Y) * dstStride] = srcIndex := point.X + point.Y * srcStride
srcData[x + y * srcStride] dstData[dstIndex] = srcData[srcIndex]
} }
}} }}
return return
} }
// StrokeRectangle is similar to FillEllipse, but it draws an elliptical inset // StrokeEllipse is similar to FillEllipse, but it draws an elliptical inset
// outline of the source canvas onto the destination canvas. To prevent the // 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 // entire source canvas's bounds from being used, it must be cut with
// canvas.Cut(). // canvas.Cut().
@ -55,82 +55,176 @@ func StrokeEllipse (
bounds := source.Bounds().Inset(weight - 1) bounds := source.Bounds().Inset(weight - 1)
context := plottingContext { context := ellipsePlottingContext {
dstData: dstData, plottingContext: plottingContext {
dstStride: dstStride, dstData: dstData,
srcData: srcData, dstStride: dstStride,
srcStride: srcStride, srcData: srcData,
weight: weight, srcStride: srcStride,
offset: offset, weight: weight,
bounds: bounds.Intersect(destination.Bounds()), offset: offset,
bounds: bounds.Intersect(destination.Bounds()),
},
radii: image.Pt(bounds.Dx() / 2 - 1, bounds.Dy() / 2 - 1),
} }
context.center = bounds.Min.Add(context.radii)
context.plotEllipse()
}
bounds.Max.X -= 1 type ellipsePlottingContext struct {
bounds.Max.Y -= 1 plottingContext
radii image.Point
radii := image.Pt ( center image.Point
bounds.Dx() / 2, }
bounds.Dy() / 2)
center := bounds.Min.Add(radii)
func (context ellipsePlottingContext) plotEllipse () {
x := float64(0) x := float64(0)
y := float64(radii.Y) y := float64(context.radii.Y)
// region 1 decision parameter // region 1 decision parameter
decision1 := decision1 :=
float64(radii.Y * radii.Y) - float64(context.radii.Y * context.radii.Y) -
float64(radii.X * radii.X * radii.Y) + float64(context.radii.X * context.radii.X * context.radii.Y) +
(0.25 * float64(radii.X) * float64(radii.X)) (0.25 * float64(context.radii.X) * float64(context.radii.X))
decisionX := float64(2 * radii.Y * radii.Y * int(x)) decisionX := float64(2 * context.radii.Y * context.radii.Y * int(x))
decisionY := float64(2 * radii.X * radii.X * int(y)) decisionY := float64(2 * context.radii.X * context.radii.X * int(y))
// draw region 1 // draw region 1
for decisionX < decisionY { for decisionX < decisionY {
context.plotSource(image.Pt( int(x) + center.X, int(y) + center.Y)) points := []image.Point {
context.plotSource(image.Pt(-int(x) + center.X, int(y) + center.Y)) image.Pt(-int(x) + context.center.X, -int(y) + context.center.Y),
context.plotSource(image.Pt( int(x) + center.X, -int(y) + center.Y)) image.Pt( int(x) + context.center.X, -int(y) + context.center.Y),
context.plotSource(image.Pt(-int(x) + center.X, -int(y) + center.Y)) image.Pt(-int(x) + context.center.X, int(y) + context.center.Y),
image.Pt( int(x) + context.center.X, int(y) + context.center.Y),
}
if context.srcData == nil {
context.plotColor(points[0])
context.plotColor(points[1])
context.plotColor(points[2])
context.plotColor(points[3])
} else {
context.plotSource(points[0])
context.plotSource(points[1])
context.plotSource(points[2])
context.plotSource(points[3])
}
if (decision1 < 0) { if (decision1 < 0) {
x ++ x ++
decisionX += float64(2 * radii.Y * radii.Y) decisionX += float64(2 * context.radii.Y * context.radii.Y)
decision1 += decisionX + float64(radii.Y * radii.Y) decision1 += decisionX + float64(context.radii.Y * context.radii.Y)
} else { } else {
x ++ x ++
y -- y --
decisionX += float64(2 * radii.Y * radii.Y) decisionX += float64(2 * context.radii.Y * context.radii.Y)
decisionY -= float64(2 * radii.X * radii.X) decisionY -= float64(2 * context.radii.X * context.radii.X)
decision1 += decision1 +=
decisionX - decisionY + decisionX - decisionY +
float64(radii.Y * radii.Y) float64(context.radii.Y * context.radii.Y)
} }
} }
// region 2 decision parameter // region 2 decision parameter
decision2 := decision2 :=
float64(radii.Y * radii.Y) * (x + 0.5) * (x + 0.5) + float64(context.radii.Y * context.radii.Y) * (x + 0.5) * (x + 0.5) +
float64(radii.X * radii.X) * (y - 1) * (y - 1) - float64(context.radii.X * context.radii.X) * (y - 1) * (y - 1) -
float64(radii.X * radii.X * radii.Y * radii.Y) float64(context.radii.X * context.radii.X * context.radii.Y * context.radii.Y)
// draw region 2 // draw region 2
for y >= 0 { for y >= 0 {
context.plotSource(image.Pt( int(x) + center.X, int(y) + center.Y)) points := []image.Point {
context.plotSource(image.Pt(-int(x) + center.X, int(y) + center.Y)) image.Pt( int(x) + context.center.X, int(y) + context.center.Y),
context.plotSource(image.Pt( int(x) + center.X, -int(y) + center.Y)) image.Pt(-int(x) + context.center.X, int(y) + context.center.Y),
context.plotSource(image.Pt(-int(x) + center.X, -int(y) + center.Y)) image.Pt( int(x) + context.center.X, -int(y) + context.center.Y),
image.Pt(-int(x) + context.center.X, -int(y) + context.center.Y),
}
if context.srcData == nil {
context.plotColor(points[0])
context.plotColor(points[1])
context.plotColor(points[2])
context.plotColor(points[3])
} else {
context.plotSource(points[0])
context.plotSource(points[1])
context.plotSource(points[2])
context.plotSource(points[3])
}
if decision2 > 0 { if decision2 > 0 {
y -- y --
decisionY -= float64(2 * radii.X * radii.X) decisionY -= float64(2 * context.radii.X * context.radii.X)
decision2 += float64(radii.X * radii.X) - decisionY decision2 += float64(context.radii.X * context.radii.X) - decisionY
} else { } else {
y -- y --
x ++ x ++
decisionX += float64(2 * radii.Y * radii.Y) decisionX += float64(2 * context.radii.Y * context.radii.Y)
decisionY -= float64(2 * radii.X * radii.X) decisionY -= float64(2 * context.radii.X * context.radii.X)
decision2 += decision2 +=
decisionX - decisionY + decisionX - decisionY +
float64(radii.X * radii.X) float64(context.radii.X * context.radii.X)
} }
} }
} }
// FillColorEllipse fills an ellipse within the destination canvas with a solid
// color.
func FillColorEllipse (
destination canvas.Canvas,
color color.RGBA,
bounds image.Rectangle,
) (
updatedRegion image.Rectangle,
) {
dstData, dstStride := destination.Buffer()
realBounds := bounds
bounds = bounds.Intersect(destination.Bounds()).Canon()
if bounds.Empty() { return }
updatedRegion = bounds
point := image.Point { }
for point.Y = bounds.Min.Y; point.Y < bounds.Max.Y; point.Y ++ {
for point.X = bounds.Min.X; point.X < bounds.Max.X; point.X ++ {
if inEllipse(point, realBounds) {
dstData[point.X + point.Y * dstStride] = color
}
}}
return
}
// StrokeColorEllipse is similar to FillColorEllipse, but it draws an inset
// outline of an ellipse instead.
func StrokeColorEllipse (
destination canvas.Canvas,
color color.RGBA,
bounds image.Rectangle,
weight int,
) (
updatedRegion image.Rectangle,
) {
if weight < 1 { return }
dstData, dstStride := destination.Buffer()
bounds = bounds.Inset(weight - 1)
context := ellipsePlottingContext {
plottingContext: plottingContext {
dstData: dstData,
dstStride: dstStride,
color: color,
weight: weight,
bounds: bounds.Intersect(destination.Bounds()),
},
radii: image.Pt(bounds.Dx() / 2 - 1, bounds.Dy() / 2 - 1),
}
context.center = bounds.Min.Add(context.radii)
context.plotEllipse()
return
}
func inEllipse (point image.Point, bounds image.Rectangle) bool {
point = point.Sub(bounds.Min)
x := (float64(point.X) + 0.5) / float64(bounds.Dx()) - 0.5
y := (float64(point.Y) + 0.5) / float64(bounds.Dy()) - 0.5
return math.Hypot(x, y) <= 0.5
}

View File

@ -4,8 +4,6 @@ import "image"
import "image/color" import "image/color"
import "git.tebibyte.media/sashakoshka/tomo/canvas" import "git.tebibyte.media/sashakoshka/tomo/canvas"
// TODO: draw thick lines more efficiently
// ColorLine draws a line from one point to another with the specified weight // ColorLine draws a line from one point to another with the specified weight
// and color. // and color.
func ColorLine ( func ColorLine (

View File

@ -3,6 +3,8 @@ package shapes
import "image" import "image"
import "image/color" import "image/color"
// FIXME? drawing a ton of overlapping squares might be a bit wasteful.
type plottingContext struct { type plottingContext struct {
dstData []color.RGBA dstData []color.RGBA
dstStride int dstStride int
@ -18,6 +20,7 @@ func (context plottingContext) square (center image.Point) image.Rectangle {
return image.Rect(0, 0, context.weight, context.weight). return image.Rect(0, 0, context.weight, context.weight).
Sub(image.Pt(context.weight / 2, context.weight / 2)). Sub(image.Pt(context.weight / 2, context.weight / 2)).
Add(center). Add(center).
Add(context.offset).
Intersect(context.bounds) Intersect(context.bounds)
} }
@ -33,8 +36,13 @@ func (context plottingContext) plotSource (center image.Point) {
square := context.square(center) square := context.square(center)
for y := square.Min.Y; y < square.Min.Y; y ++ { for y := square.Min.Y; y < square.Min.Y; y ++ {
for x := square.Min.X; x < square.Min.X; x ++ { for x := square.Min.X; x < square.Min.X; x ++ {
context.dstData[x + y * context.dstStride] = // we offset srcIndex here because we have already applied the
context.srcData [ // offset to the square, and we need to reverse that to get the
x + y * context.dstStride] // proper source coordinates.
srcIndex :=
x - context.offset.X +
(y - context.offset.Y) * context.dstStride
dstIndex := x + y * context.dstStride
context.dstData[dstIndex] = context.srcData [srcIndex]
}} }}
} }

View File

@ -1,9 +1,12 @@
package shapes package shapes
import "image" import "image"
import "image/color"
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"
// TODO: return updatedRegion for all routines in this package
// FillRectangle draws the content of one canvas onto another. The offset point // 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 // 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 // to the origin point of the destination canvas. To prevent the entire source
@ -59,8 +62,60 @@ func FillRectangleShatter (
offset image.Point, offset image.Point,
rocks ...image.Rectangle, rocks ...image.Rectangle,
) { ) {
tiles := shatter.Shatter(source.Bounds(), rocks...) tiles := shatter.Shatter(source.Bounds().Sub(offset), rocks...)
for _, tile := range tiles { for _, tile := range tiles {
FillRectangle(destination, canvas.Cut(source, tile), offset) FillRectangle(destination, canvas.Cut(source, tile), offset)
} }
} }
// FillColorRectangle fills a rectangle within the destination canvas with a
// solid color.
func FillColorRectangle (
destination canvas.Canvas,
color color.RGBA,
bounds image.Rectangle,
) (
updatedRegion image.Rectangle,
) {
dstData, dstStride := destination.Buffer()
bounds = bounds.Canon().Intersect(destination.Bounds())
if bounds.Empty() { return }
updatedRegion = bounds
for y := bounds.Min.Y; y < bounds.Max.Y; y ++ {
for x := bounds.Min.X; x < bounds.Max.X; x ++ {
dstData[x + y * dstStride] = color
}}
return
}
// FillColorRectangleShatter is like FillColorRectangle, but it does not draw in
// areas specified in "rocks".
func FillColorRectangleShatter (
destination canvas.Canvas,
color color.RGBA,
bounds image.Rectangle,
rocks ...image.Rectangle,
) {
tiles := shatter.Shatter(bounds, rocks...)
for _, tile := range tiles {
FillColorRectangle(destination, color, tile)
}
}
// StrokeColorRectangle is similar to FillColorRectangle, but it draws an inset
// outline of the given rectangle instead.
func StrokeColorRectangle (
destination canvas.Canvas,
color color.RGBA,
bounds image.Rectangle,
weight int,
) {
insetBounds := bounds.Inset(weight)
if insetBounds.Empty() {
FillColorRectangle(destination, color, bounds)
return
}
FillColorRectangleShatter(destination, color, bounds, insetBounds)
}