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/patterns/dotted.go

31 lines
753 B
Go
Raw Normal View History

2023-01-24 08:25:37 -07:00
package artist
import "math"
import "image/color"
// Dotted is a pattern that produces a grid of circles.
type Dotted struct {
Background Pattern
Foreground Pattern
Size int
Spacing int
}
// AtWhen satisfies the Pattern interface.
func (pattern Dotted) AtWhen (x, y, width, height int) (c color.RGBA) {
xm := x % pattern.Spacing
ym := y % pattern.Spacing
if xm < 0 { xm += pattern.Spacing }
if ym < 0 { xm += pattern.Spacing }
radius := float64(pattern.Size) / 2
spacing := float64(pattern.Spacing) / 2 - 0.5
xf := float64(xm) - spacing
yf := float64(ym) - spacing
if math.Sqrt(xf * xf + yf * yf) > radius {
return pattern.Background.AtWhen(x, y, width, height)
} else {
return pattern.Foreground.AtWhen(x, y, width, height)
}
}