This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/artist/shapes/ellipse.go

232 lines
6.5 KiB
Go
Raw Normal View History

2023-02-23 18:55:19 -07:00
package shapes
import "math"
import "image"
import "image/color"
2023-04-30 11:45:21 -06:00
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-02-26 12:27:38 -07:00
// TODO: redo fill ellipse, stroke ellipse, etc. so that it only takes in
// destination and source, using the bounds of destination as the bounds of the
// ellipse and the bounds of source as the "clipping rectangle". Line up the Min
// of both canvases.
func FillEllipse (
2023-04-30 11:45:21 -06:00
destination artist.Canvas,
source artist.Canvas,
bounds image.Rectangle,
) (
updatedRegion image.Rectangle,
) {
dstData, dstStride := destination.Buffer()
srcData, srcStride := source.Buffer()
2023-02-26 12:27:38 -07:00
offset := source.Bounds().Min.Sub(destination.Bounds().Min)
drawBounds :=
source.Bounds().Sub(offset).
Intersect(destination.Bounds()).
Intersect(bounds)
if bounds.Empty() { return }
updatedRegion = bounds
point := image.Point { }
for point.Y = drawBounds.Min.Y; point.Y < drawBounds.Max.Y; point.Y ++ {
for point.X = drawBounds.Min.X; point.X < drawBounds.Max.X; point.X ++ {
if inEllipse(point, bounds) {
offsetPoint := point.Add(offset)
2023-02-26 12:27:38 -07:00
dstIndex := point.X + point.Y * dstStride
srcIndex := offsetPoint.X + offsetPoint.Y * srcStride
dstData[dstIndex] = srcData[srcIndex]
}
}}
return
}
2023-01-20 19:59:48 -07:00
func StrokeEllipse (
2023-04-30 11:45:21 -06:00
destination artist.Canvas,
source artist.Canvas,
bounds image.Rectangle,
weight int,
2023-01-20 19:59:48 -07:00
) {
if weight < 1 { return }
dstData, dstStride := destination.Buffer()
srcData, srcStride := source.Buffer()
drawBounds := destination.Bounds().Inset(weight - 1)
2023-02-26 12:27:38 -07:00
offset := source.Bounds().Min.Sub(destination.Bounds().Min)
if drawBounds.Empty() { return }
2023-01-20 19:59:48 -07:00
context := ellipsePlottingContext {
plottingContext: plottingContext {
dstData: dstData,
dstStride: dstStride,
srcData: srcData,
srcStride: srcStride,
weight: weight,
offset: offset,
bounds: bounds,
},
radii: image.Pt(drawBounds.Dx() / 2, drawBounds.Dy() / 2),
2023-01-20 19:59:48 -07:00
}
2023-03-11 23:23:20 -07:00
context.center = drawBounds.Min.Add(context.radii)
context.plotEllipse()
}
2023-01-20 20:13:14 -07:00
type ellipsePlottingContext struct {
plottingContext
radii image.Point
center image.Point
}
2023-01-20 19:59:48 -07:00
func (context ellipsePlottingContext) plotEllipse () {
2023-01-20 19:59:48 -07:00
x := float64(0)
y := float64(context.radii.Y)
2023-01-20 19:59:48 -07:00
// region 1 decision parameter
decision1 :=
float64(context.radii.Y * context.radii.Y) -
float64(context.radii.X * context.radii.X * context.radii.Y) +
(0.25 * float64(context.radii.X) * float64(context.radii.X))
decisionX := float64(2 * context.radii.Y * context.radii.Y * int(x))
decisionY := float64(2 * context.radii.X * context.radii.X * int(y))
2023-01-20 19:59:48 -07:00
// draw region 1
for decisionX < decisionY {
points := []image.Point {
image.Pt(-int(x) + context.center.X, -int(y) + context.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),
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])
}
2023-01-20 19:59:48 -07:00
if (decision1 < 0) {
x ++
decisionX += float64(2 * context.radii.Y * context.radii.Y)
decision1 += decisionX + float64(context.radii.Y * context.radii.Y)
2023-01-20 19:59:48 -07:00
} else {
x ++
y --
decisionX += float64(2 * context.radii.Y * context.radii.Y)
decisionY -= float64(2 * context.radii.X * context.radii.X)
2023-01-20 19:59:48 -07:00
decision1 +=
decisionX - decisionY +
float64(context.radii.Y * context.radii.Y)
2023-01-20 19:59:48 -07:00
}
}
// region 2 decision parameter
decision2 :=
float64(context.radii.Y * context.radii.Y) * (x + 0.5) * (x + 0.5) +
float64(context.radii.X * context.radii.X) * (y - 1) * (y - 1) -
float64(context.radii.X * context.radii.X * context.radii.Y * context.radii.Y)
2023-01-20 19:59:48 -07:00
// draw region 2
for y >= 0 {
points := []image.Point {
image.Pt( int(x) + context.center.X, int(y) + context.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),
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])
}
2023-01-20 19:59:48 -07:00
if decision2 > 0 {
y --
decisionY -= float64(2 * context.radii.X * context.radii.X)
decision2 += float64(context.radii.X * context.radii.X) - decisionY
2023-01-20 19:59:48 -07:00
} else {
y --
x ++
decisionX += float64(2 * context.radii.Y * context.radii.Y)
decisionY -= float64(2 * context.radii.X * context.radii.X)
2023-01-20 19:59:48 -07:00
decision2 +=
decisionX - decisionY +
float64(context.radii.X * context.radii.X)
}
}
}
// FillColorEllipse fills an ellipse within the destination canvas with a solid
// color.
func FillColorEllipse (
2023-04-30 11:45:21 -06:00
destination artist.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
2023-01-20 19:59:48 -07:00
}
}}
return
}
// StrokeColorEllipse is similar to FillColorEllipse, but it draws an inset
// outline of an ellipse instead.
func StrokeColorEllipse (
2023-04-30 11:45:21 -06:00
destination artist.Canvas,
color color.RGBA,
bounds image.Rectangle,
weight int,
) (
updatedRegion image.Rectangle,
) {
if weight < 1 { return }
dstData, dstStride := destination.Buffer()
2023-02-26 12:27:38 -07:00
insetBounds := bounds.Inset(weight - 1)
context := ellipsePlottingContext {
plottingContext: plottingContext {
dstData: dstData,
dstStride: dstStride,
color: color,
weight: weight,
bounds: bounds.Intersect(destination.Bounds()),
},
2023-02-26 12:27:38 -07:00
radii: image.Pt(insetBounds.Dx() / 2, insetBounds.Dy() / 2),
2023-01-20 19:59:48 -07:00
}
2023-02-26 12:27:38 -07:00
context.center = insetBounds.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
2023-01-20 19:59:48 -07:00
}