Created the split pattern

This commit is contained in:
2023-01-20 23:19:54 -05:00
parent 4c1bf070fe
commit 83d5064803
3 changed files with 65 additions and 29 deletions

43
artist/split.go Normal file
View File

@@ -0,0 +1,43 @@
package artist
import "image/color"
// Orientation specifies an eight-way pattern orientation.
type Orientation int
const (
OrientationVertical Orientation = iota
OrientationDiagonalRight
OrientationHorizontal
OrientationDiagonalLeft
)
// Split is a pattern that is divided in half between two sub-patterns.
type Split struct {
First Pattern
Second Pattern
Orientation
}
// AtWhen satisfies the Pattern interface.
func (pattern Split) AtWhen (x, y, width, height int) (c color.RGBA) {
var first bool
switch pattern.Orientation {
case OrientationVertical:
first = x < width / 2
case OrientationDiagonalRight:
first = float64(x) / float64(width) +
float64(y) / float64(height) < 1
case OrientationHorizontal:
first = y < height / 2
case OrientationDiagonalLeft:
first = float64(width - x) / float64(width) +
float64(y) / float64(height) < 1
}
if first {
return pattern.First.AtWhen(x, y, width, height)
} else {
return pattern.Second.AtWhen(x, y, width, height)
}
}

View File

@@ -2,34 +2,24 @@ package artist
import "image/color"
// StripeDirection specifies the direction of stripes.
type StripeDirection int
const (
StripeDirectionVertical StripeDirection = iota
StripeDirectionDiagonalRight
StripeDirectionHorizontal
StripeDirectionDiagonalLeft
)
// Striped is a pattern that produces stripes of two alternating colors.
type Striped struct {
First Stroke
Second Stroke
Direction StripeDirection
First Stroke
Second Stroke
Orientation
}
// AtWhen satisfies the Pattern interface.
func (pattern Striped) AtWhen (x, y, width, height int) (c color.RGBA) {
position := 0
switch pattern.Direction {
case StripeDirectionVertical:
switch pattern.Orientation {
case OrientationVertical:
position = x
case StripeDirectionDiagonalRight:
case OrientationDiagonalRight:
position = x + y
case StripeDirectionHorizontal:
case OrientationHorizontal:
position = y
case StripeDirectionDiagonalLeft:
case OrientationDiagonalLeft:
position = x - y
}
@@ -40,8 +30,8 @@ func (pattern Striped) AtWhen (x, y, width, height int) (c color.RGBA) {
}
if position < pattern.First.Weight {
return pattern.First.Pattern.AtWhen(x, y, width, height)
return pattern.First.AtWhen(x, y, width, height)
} else {
return pattern.Second.Pattern.AtWhen(x, y, width, height)
return pattern.Second.AtWhen(x, y, width, height)
}
}