From 211219eb0146d22b46a7dbb4c656cdc696f69428 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 24 Feb 2023 02:51:24 -0500 Subject: [PATCH] Ellipse and line share code --- artist/shapes/ellipse.go | 45 ++++++++-------------------------------- artist/shapes/line.go | 44 +++++++++++++-------------------------- artist/shapes/plot.go | 40 +++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 66 deletions(-) create mode 100644 artist/shapes/plot.go diff --git a/artist/shapes/ellipse.go b/artist/shapes/ellipse.go index 654b905..e4b46a5 100644 --- a/artist/shapes/ellipse.go +++ b/artist/shapes/ellipse.go @@ -2,8 +2,6 @@ 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 the content of one canvas onto another, clipped by an @@ -57,7 +55,7 @@ func StrokeEllipse ( bounds := source.Bounds().Inset(weight - 1) - context := ellipsePlottingContext { + context := plottingContext { dstData: dstData, dstStride: dstStride, srcData: srcData, @@ -88,10 +86,10 @@ func StrokeEllipse ( // 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) + context.plotSource(image.Pt( int(x) + center.X, int(y) + center.Y)) + context.plotSource(image.Pt(-int(x) + center.X, int(y) + center.Y)) + context.plotSource(image.Pt( int(x) + center.X, -int(y) + center.Y)) + context.plotSource(image.Pt(-int(x) + center.X, -int(y) + center.Y)) if (decision1 < 0) { x ++ @@ -116,10 +114,10 @@ func StrokeEllipse ( // 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) + context.plotSource(image.Pt( int(x) + center.X, int(y) + center.Y)) + context.plotSource(image.Pt(-int(x) + center.X, int(y) + center.Y)) + context.plotSource(image.Pt( int(x) + center.X, -int(y) + center.Y)) + context.plotSource(image.Pt(-int(x) + center.X, -int(y) + center.Y)) if decision2 > 0 { 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] - }} -} diff --git a/artist/shapes/line.go b/artist/shapes/line.go index 85ed642..6d4050e 100644 --- a/artist/shapes/line.go +++ b/artist/shapes/line.go @@ -17,7 +17,6 @@ func ColorLine ( ) ( updatedRegion image.Rectangle, ) { - updatedRegion = image.Rectangle { Min: min, Max: max }.Canon() updatedRegion.Max.X ++ updatedRegion.Max.Y ++ @@ -25,13 +24,15 @@ func ColorLine ( data, stride := destination.Buffer() bounds := destination.Bounds() context := linePlottingContext { - dstData: data, - dstStride: stride, - color: color, - weight: weight, - bounds: bounds, - min: min, - max: max, + plottingContext: plottingContext { + 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) { @@ -46,13 +47,9 @@ func ColorLine ( } type linePlottingContext struct { - dstData []color.RGBA - dstStride int - color color.RGBA - weight int - bounds image.Rectangle - min image.Point - max image.Point + plottingContext + min image.Point + max image.Point } func (context *linePlottingContext) swap () { @@ -76,7 +73,7 @@ func (context linePlottingContext) lineLow () { for ; point.X < context.max.X; point.X ++ { if !point.In(context.bounds) { break } - context.plot(point) + context.plotColor(point) if D > 0 { D += 2 * (deltaY - deltaX) point.Y += yi @@ -101,7 +98,7 @@ func (context linePlottingContext) lineHigh () { for ; point.Y < context.max.Y; point.Y ++ { if !point.In(context.bounds) { break } - context.plot(point) + context.plotColor(point) if D > 0 { point.X += xi D += 2 * (deltaX - deltaY) @@ -115,16 +112,3 @@ func abs (n int) int { if n < 0 { n *= -1} 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 - }} -} diff --git a/artist/shapes/plot.go b/artist/shapes/plot.go new file mode 100644 index 0000000..a35751a --- /dev/null +++ b/artist/shapes/plot.go @@ -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] + }} +}