Fixed the Texture pattern

This commit is contained in:
Sasha Koshka 2023-04-02 22:37:38 -04:00
parent 6c3230c0f8
commit 46a4858597
1 changed files with 28 additions and 12 deletions

View File

@ -10,42 +10,58 @@ type Texture struct {
}
// Draw tiles the pattern's canvas within the given bounds. The minimum
// points of the pattern's canvas and the destination canvas will be lined up.
// point of the pattern's canvas will be lined up with the minimum point of the
// bounding rectangle.
func (pattern Texture) Draw (destination canvas.Canvas, bounds image.Rectangle) {
drawBounds := bounds.Canon().Intersect(destination.Bounds())
if drawBounds.Empty() { return }
dstBounds := bounds.Canon().Intersect(destination.Bounds())
if dstBounds.Empty() { return }
dstData, dstStride := destination.Buffer()
srcData, srcStride := pattern.Buffer()
srcBounds := pattern.Bounds()
dstPoint := image.Point { }
srcPoint := drawBounds.Min.Sub(bounds.Min).Add(srcBounds.Min)
// offset is a vector that is added to points in destination space to
// convert them to points in source space
offset := srcBounds.Min.Sub(bounds.Min)
// calculate the starting position in source space
srcPoint := dstBounds.Min.Add(offset)
srcPoint.X = wrap(srcPoint.X, srcBounds.Min.X, srcBounds.Max.X)
srcPoint.Y = wrap(srcPoint.Y, srcBounds.Min.Y, srcBounds.Max.Y)
for dstPoint.Y = drawBounds.Min.Y; dstPoint.Y < drawBounds.Max.Y; dstPoint.Y ++ {
srcPoint.X = srcBounds.Min.X
dstPoint.X = drawBounds.Min.X
srcStartPoint := srcPoint
// for each row
dstPoint := image.Point { }
for dstPoint.Y = dstBounds.Min.Y; dstPoint.Y < dstBounds.Max.Y; dstPoint.Y ++ {
srcPoint.X = srcStartPoint.X
dstPoint.X = dstBounds.Min.X
dstYComponent := dstPoint.Y * dstStride
srcYComponent := srcPoint.Y * srcStride
// for each pixel in the row
for {
// draw pixel
dstIndex := dstYComponent + dstPoint.X
srcIndex := srcYComponent + srcPoint.X
dstData[dstIndex] = srcData[srcIndex]
// increment X in source space. wrap to start if out of
// bounds.
srcPoint.X ++
if srcPoint.X >= srcBounds.Max.X {
srcPoint.X = srcBounds.Min.X
}
// increment X in destination space. stop drawing this
// row if out of bounds.
dstPoint.X ++
if dstPoint.X >= drawBounds.Max.X {
if dstPoint.X >= dstBounds.Max.X {
break
}
}
// increment row in source space. wrap to start if out of
// bounds.
srcPoint.Y ++
if srcPoint.Y >= srcBounds.Max.Y {
srcPoint.Y = srcBounds.Min.Y