Commit 93159e32 authored by Nigel Tao's avatar Nigel Tao

image: add an offset to Tiled.

R=r, r2
CC=golang-dev
https://golang.org/cl/2469041
parent 712109f1
...@@ -43,15 +43,13 @@ func (p Point) Div(k int) Point { ...@@ -43,15 +43,13 @@ func (p Point) Div(k int) Point {
func (p Point) Mod(r Rectangle) Point { func (p Point) Mod(r Rectangle) Point {
w, h := r.Dx(), r.Dy() w, h := r.Dx(), r.Dy()
p = p.Sub(r.Min) p = p.Sub(r.Min)
if p.X >= 0 { p.X = p.X % w
p.X = p.X % w if p.X < 0 {
} else { p.X += w
p.X = w - 1 - (-1-p.X)%w
} }
if p.Y >= 0 { p.Y = p.Y % h
p.Y = p.Y % h if p.Y < 0 {
} else { p.Y += h
p.Y = h - 1 - (-1-p.Y)%h
} }
return p.Add(r.Min) return p.Add(r.Min)
} }
......
...@@ -15,7 +15,7 @@ var ( ...@@ -15,7 +15,7 @@ var (
Opaque = NewColorImage(Alpha16Color{0xffff}) Opaque = NewColorImage(Alpha16Color{0xffff})
) )
// A ColorImage is a practically infinite-sized Image of uniform Color. // A ColorImage is an infinite-sized Image of uniform Color.
// It implements both the Color and Image interfaces. // It implements both the Color and Image interfaces.
type ColorImage struct { type ColorImage struct {
C Color C Color
...@@ -43,11 +43,12 @@ func NewColorImage(c Color) *ColorImage { ...@@ -43,11 +43,12 @@ func NewColorImage(c Color) *ColorImage {
return &ColorImage{c} return &ColorImage{c}
} }
// A Tiled is a practically infinite-sized Image that repeats another Image in // A Tiled is an infinite-sized Image that repeats another Image in both
// both directions. Tiled{i}.At(x, y) will equal i.At(x, y) for all points // directions. Tiled{i, p}.At(x, y) will equal i.At(x+p.X, y+p.Y) for all
// within i's Bounds. // points {x+p.X, y+p.Y} within i's Bounds.
type Tiled struct { type Tiled struct {
I Image I Image
Offset Point
} }
func (t *Tiled) ColorModel() ColorModel { func (t *Tiled) ColorModel() ColorModel {
...@@ -57,10 +58,10 @@ func (t *Tiled) ColorModel() ColorModel { ...@@ -57,10 +58,10 @@ func (t *Tiled) ColorModel() ColorModel {
func (t *Tiled) Bounds() Rectangle { return Rectangle{Point{-1e9, -1e9}, Point{1e9, 1e9}} } func (t *Tiled) Bounds() Rectangle { return Rectangle{Point{-1e9, -1e9}, Point{1e9, 1e9}} }
func (t *Tiled) At(x, y int) Color { func (t *Tiled) At(x, y int) Color {
p := Point{x, y}.Mod(t.I.Bounds()) p := Point{x, y}.Add(t.Offset).Mod(t.I.Bounds())
return t.I.At(p.X, p.Y) return t.I.At(p.X, p.Y)
} }
func NewTiled(i Image) *Tiled { func NewTiled(i Image, offset Point) *Tiled {
return &Tiled{i} return &Tiled{i, offset}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment