Ellipse and line share code

This commit is contained in:
Sasha Koshka 2023-02-24 02:51:24 -05:00
parent 79ab1c8ac0
commit 211219eb01
3 changed files with 63 additions and 66 deletions

View File

@ -2,8 +2,6 @@ package shapes
import "math" import "math"
import "image" import "image"
import "image/color"
// import "git.tebibyte.media/sashakoshka/tomo/artist"
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
@ -57,7 +55,7 @@ func StrokeEllipse (
bounds := source.Bounds().Inset(weight - 1) bounds := source.Bounds().Inset(weight - 1)
context := ellipsePlottingContext { context := plottingContext {
dstData: dstData, dstData: dstData,
dstStride: dstStride, dstStride: dstStride,
srcData: srcData, srcData: srcData,
@ -88,10 +86,10 @@ func StrokeEllipse (
// draw region 1 // draw region 1
for decisionX < decisionY { for decisionX < decisionY {
context.plot( int(x) + center.X, int(y) + center.Y) context.plotSource(image.Pt( int(x) + center.X, int(y) + center.Y))
context.plot(-int(x) + center.X, int(y) + center.Y) context.plotSource(image.Pt(-int(x) + center.X, int(y) + center.Y))
context.plot( int(x) + center.X, -int(y) + center.Y) context.plotSource(image.Pt( int(x) + center.X, -int(y) + center.Y))
context.plot(-int(x) + center.X, -int(y) + center.Y) context.plotSource(image.Pt(-int(x) + center.X, -int(y) + center.Y))
if (decision1 < 0) { if (decision1 < 0) {
x ++ x ++
@ -116,10 +114,10 @@ func StrokeEllipse (
// draw region 2 // draw region 2
for y >= 0 { for y >= 0 {
context.plot( int(x) + center.X, int(y) + center.Y) context.plotSource(image.Pt( int(x) + center.X, int(y) + center.Y))
context.plot(-int(x) + center.X, int(y) + center.Y) context.plotSource(image.Pt(-int(x) + center.X, int(y) + center.Y))
context.plot( int(x) + center.X, -int(y) + center.Y) context.plotSource(image.Pt( int(x) + center.X, -int(y) + center.Y))
context.plot(-int(x) + center.X, -int(y) + center.Y) context.plotSource(image.Pt(-int(x) + center.X, -int(y) + center.Y))
if decision2 > 0 { if decision2 > 0 {
y -- y --
@ -136,28 +134,3 @@ func StrokeEllipse (
} }
} }
} }
type ellipsePlottingContext struct {
dstData []color.RGBA
dstStride int
srcData []color.RGBA
srcStride int
weight int
offset image.Point
bounds image.Rectangle
}
func (context ellipsePlottingContext) plot (x, y int) {
square :=
image.Rect(0, 0, context.weight, context.weight).
Sub(image.Pt(context.weight / 2, context.weight / 2)).
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]
}}
}

View File

@ -17,7 +17,6 @@ func ColorLine (
) ( ) (
updatedRegion image.Rectangle, updatedRegion image.Rectangle,
) { ) {
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 ++
@ -25,13 +24,15 @@ func ColorLine (
data, stride := destination.Buffer() data, stride := destination.Buffer()
bounds := destination.Bounds() bounds := destination.Bounds()
context := linePlottingContext { context := linePlottingContext {
dstData: data, plottingContext: plottingContext {
dstStride: stride, dstData: data,
color: color, dstStride: stride,
weight: weight, color: color,
bounds: bounds, weight: weight,
min: min, bounds: bounds,
max: max, },
min: min,
max: max,
} }
if abs(max.Y - min.Y) < abs(max.X - min.X) { if abs(max.Y - min.Y) < abs(max.X - min.X) {
@ -46,13 +47,9 @@ func ColorLine (
} }
type linePlottingContext struct { type linePlottingContext struct {
dstData []color.RGBA plottingContext
dstStride int min image.Point
color color.RGBA max image.Point
weight int
bounds image.Rectangle
min image.Point
max image.Point
} }
func (context *linePlottingContext) swap () { func (context *linePlottingContext) swap () {
@ -76,7 +73,7 @@ func (context linePlottingContext) lineLow () {
for ; point.X < context.max.X; point.X ++ { for ; point.X < context.max.X; point.X ++ {
if !point.In(context.bounds) { break } if !point.In(context.bounds) { break }
context.plot(point) context.plotColor(point)
if D > 0 { if D > 0 {
D += 2 * (deltaY - deltaX) D += 2 * (deltaY - deltaX)
point.Y += yi point.Y += yi
@ -101,7 +98,7 @@ func (context linePlottingContext) lineHigh () {
for ; point.Y < context.max.Y; point.Y ++ { for ; point.Y < context.max.Y; point.Y ++ {
if !point.In(context.bounds) { break } if !point.In(context.bounds) { break }
context.plot(point) context.plotColor(point)
if D > 0 { if D > 0 {
point.X += xi point.X += xi
D += 2 * (deltaX - deltaY) D += 2 * (deltaX - deltaY)
@ -115,16 +112,3 @@ func abs (n int) int {
if n < 0 { n *= -1} if n < 0 { n *= -1}
return n return n
} }
func (context linePlottingContext) plot (center image.Point) {
square :=
image.Rect(0, 0, context.weight, context.weight).
Sub(image.Pt(context.weight / 2, context.weight / 2)).
Add(center).
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.color
}}
}

40
artist/shapes/plot.go Normal file
View File

@ -0,0 +1,40 @@
package shapes
import "image"
import "image/color"
type plottingContext struct {
dstData []color.RGBA
dstStride int
srcData []color.RGBA
srcStride int
color color.RGBA
weight int
offset image.Point
bounds image.Rectangle
}
func (context plottingContext) square (center image.Point) image.Rectangle {
return image.Rect(0, 0, context.weight, context.weight).
Sub(image.Pt(context.weight / 2, context.weight / 2)).
Add(center).
Intersect(context.bounds)
}
func (context plottingContext) plotColor (center image.Point) {
square := context.square(center)
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.color
}}
}
func (context plottingContext) plotSource (center image.Point) {
square := context.square(center)
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]
}}
}