Fixed up the dotted pattern

This commit is contained in:
Sasha Koshka 2023-01-24 10:25:37 -05:00
parent bbe41881ac
commit 269c70ebb1
3 changed files with 99 additions and 0 deletions

33
artist/checkered.go Normal file
View File

@ -0,0 +1,33 @@
package artist
import "image/color"
// Checkered is a pattern that produces a grid of two alternating colors.
type Checkered struct {
First Pattern
Second Pattern
CellWidth, CellHeight int
}
// AtWhen satisfies the Pattern interface.
func (pattern Checkered) AtWhen (x, y, width, height int) (c color.RGBA) {
twidth := pattern.CellWidth * 2
theight := pattern.CellHeight * 2
x %= twidth
y %= theight
if x < 0 { x += twidth }
if y < 0 { x += theight }
n := 0
if x >= pattern.CellWidth { n ++ }
if y >= pattern.CellHeight { n ++ }
x %= pattern.CellWidth
y %= pattern.CellHeight
if n % 2 == 0 {
return pattern.First.AtWhen(x, y, pattern.CellWidth, pattern.CellHeight)
} else {
return pattern.Second.AtWhen(x, y, pattern.CellWidth, pattern.CellHeight)
}
}

30
artist/dotted.go Normal file
View File

@ -0,0 +1,30 @@
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)
}
}

View File

@ -139,6 +139,42 @@ func (element *Artist) Resize (width, height int) {
artist.NewUniform(hex(0xFF00FFFF)),
},
element.cellAt(0, 5))
// 1, 5
artist.FillRectangle (
element,
artist.Checkered {
First: artist.QuadBeveled {
artist.NewUniform(hex(0x880000FF)),
artist.NewUniform(hex(0x00FF00FF)),
artist.NewUniform(hex(0x0000FFFF)),
artist.NewUniform(hex(0xFF00FFFF)),
},
Second: artist.Striped {
First: artist.Stroke { Pattern: uhex(0xFF8800FF), Weight: 1 },
Second: artist.Stroke { Pattern: uhex(0x0088FFFF), Weight: 1 },
Orientation: artist.OrientationVertical,
},
CellWidth: 32,
CellHeight: 16,
},
element.cellAt(1, 5))
// 2, 5
artist.FillRectangle (
element,
artist.Dotted {
Foreground: uhex(0x00FF00FF),
Background: artist.Checkered {
First: uhex(0x444444FF),
Second: uhex(0x888888FF),
CellWidth: 16,
CellHeight: 16,
},
Size: 8,
Spacing: 16,
},
element.cellAt(2, 5))
}
func (element *Artist) lines (weight int, bounds image.Rectangle) {