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

147 lines
3.6 KiB
Go
Raw Normal View History

2023-02-23 18:55:19 -07:00
package shapes
import "math"
import "image"
2023-01-20 19:59:48 -07:00
import "image/color"
2023-02-23 18:55:19 -07:00
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-02-01 23:48:16 -07:00
import "git.tebibyte.media/sashakoshka/tomo/canvas"
// FillEllipse draws a filled ellipse with the specified pattern.
func FillEllipse (
2023-02-01 23:48:16 -07:00
destination canvas.Canvas,
2023-02-23 18:55:19 -07:00
source artist.Pattern,
bounds image.Rectangle,
) (
updatedRegion image.Rectangle,
) {
2023-01-20 19:59:48 -07:00
bounds = bounds.Canon()
data, stride := destination.Buffer()
realWidth, realHeight := bounds.Dx(), bounds.Dy()
2023-01-20 19:59:48 -07:00
bounds = bounds.Intersect(destination.Bounds()).Canon()
if bounds.Empty() { return }
updatedRegion = bounds
2023-01-20 19:59:48 -07:00
width, height := bounds.Dx(), bounds.Dy()
for y := 0; y < height; y ++ {
for x := 0; x < width; x ++ {
2023-01-20 20:13:14 -07:00
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
}
2023-01-20 19:59:48 -07:00
// StrokeEllipse draws the outline of an ellipse with the specified line weight
// and pattern.
func StrokeEllipse (
2023-02-01 23:48:16 -07:00
destination canvas.Canvas,
2023-02-23 18:55:19 -07:00
source artist.Pattern,
2023-01-20 19:59:48 -07:00
weight int,
bounds image.Rectangle,
) {
if weight < 1 { return }
data, stride := destination.Buffer()
2023-01-20 20:13:14 -07:00
bounds = bounds.Canon().Inset(weight - 1)
2023-01-20 19:59:48 -07:00
width, height := bounds.Dx(), bounds.Dy()
context := ellipsePlottingContext {
data: data,
stride: stride,
source: source,
width: width,
height: height,
weight: weight,
bounds: bounds,
}
2023-01-20 20:13:14 -07:00
bounds.Max.X -= 1
bounds.Max.Y -= 1
radii := image.Pt (
bounds.Dx() / 2,
bounds.Dy() / 2)
center := bounds.Min.Add(radii)
2023-01-20 19:59:48 -07:00
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
2023-02-23 18:55:19 -07:00
source artist.Pattern
2023-01-20 19:59:48 -07:00
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)
}
}