1
0
mirror of https://github.com/golang/go synced 2024-11-22 05:54:40 -07:00

image: rename Contains and ContainsRectangle to In.

R=r
CC=golang-dev
https://golang.org/cl/4539104
This commit is contained in:
Nigel Tao 2011-06-03 13:18:15 +10:00
parent 56668283f1
commit f531ef3279
4 changed files with 44 additions and 44 deletions

View File

@ -174,18 +174,18 @@ func TestClip(t *testing.T) {
// Check that the clipped rectangle is contained by the dst / src / mask // Check that the clipped rectangle is contained by the dst / src / mask
// rectangles, in their respective co-ordinate spaces. // rectangles, in their respective co-ordinate spaces.
if !c.dr.ContainsRectangle(r) { if !r.In(c.dr) {
t.Errorf("%s: c.dr %v does not contain r %v", c.desc, c.dr, r) t.Errorf("%s: c.dr %v does not contain r %v", c.desc, c.dr, r)
} }
// sr is r translated into src's co-ordinate space. // sr is r translated into src's co-ordinate space.
sr := r.Add(c.sp.Sub(c.dr.Min)) sr := r.Add(c.sp.Sub(c.dr.Min))
if !c.sr.ContainsRectangle(sr) { if !sr.In(c.sr) {
t.Errorf("%s: c.sr %v does not contain sr %v", c.desc, c.sr, sr) t.Errorf("%s: c.sr %v does not contain sr %v", c.desc, c.sr, sr)
} }
if !c.nilMask { if !c.nilMask {
// mr is r translated into mask's co-ordinate space. // mr is r translated into mask's co-ordinate space.
mr := r.Add(c.mp.Sub(c.dr.Min)) mr := r.Add(c.mp.Sub(c.dr.Min))
if !c.mr.ContainsRectangle(mr) { if !mr.In(c.mr) {
t.Errorf("%s: c.mr %v does not contain mr %v", c.desc, c.mr, mr) t.Errorf("%s: c.mr %v does not contain mr %v", c.desc, c.mr, mr)
} }
} }

View File

@ -38,6 +38,12 @@ func (p Point) Div(k int) Point {
return Point{p.X / k, p.Y / k} return Point{p.X / k, p.Y / k}
} }
// In returns whether p is in r.
func (p Point) In(r Rectangle) bool {
return r.Min.X <= p.X && p.X < r.Max.X &&
r.Min.Y <= p.Y && p.Y < r.Max.Y
}
// Mod returns the point q in r such that p.X-q.X is a multiple of r's width // Mod returns the point q in r such that p.X-q.X is a multiple of r's width
// and p.Y-q.Y is a multiple of r's height. // and p.Y-q.Y is a multiple of r's height.
func (p Point) Mod(r Rectangle) Point { func (p Point) Mod(r Rectangle) Point {
@ -190,21 +196,15 @@ func (r Rectangle) Overlaps(s Rectangle) bool {
r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y
} }
// Contains returns whether r contains p. // In returns whether every point in r is in s.
func (r Rectangle) Contains(p Point) bool { func (r Rectangle) In(s Rectangle) bool {
return r.Min.X <= p.X && p.X < r.Max.X && if r.Empty() {
r.Min.Y <= p.Y && p.Y < r.Max.Y
}
// ContainsRectangle returns whether r contains every point in s.
func (r Rectangle) ContainsRectangle(s Rectangle) bool {
if s.Empty() {
return true return true
} }
// Note that s.Max is an exclusive bound for s, so that r.ContainsRectangle(s) // Note that r.Max is an exclusive bound for r, so that r.In(s)
// does not require that r.Contains(s.Max). // does not require that r.Max.In(s).
return r.Min.X <= s.Min.X && s.Max.X <= r.Max.X && return s.Min.X <= r.Min.X && r.Max.X <= s.Max.X &&
r.Min.Y <= s.Min.Y && s.Max.Y <= r.Max.Y s.Min.Y <= r.Min.Y && r.Max.Y <= s.Max.Y
} }
// Canon returns the canonical version of r. The returned rectangle has minimum // Canon returns the canonical version of r. The returned rectangle has minimum

View File

@ -38,21 +38,21 @@ func (p *RGBA) ColorModel() ColorModel { return RGBAColorModel }
func (p *RGBA) Bounds() Rectangle { return p.Rect } func (p *RGBA) Bounds() Rectangle { return p.Rect }
func (p *RGBA) At(x, y int) Color { func (p *RGBA) At(x, y int) Color {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return RGBAColor{} return RGBAColor{}
} }
return p.Pix[y*p.Stride+x] return p.Pix[y*p.Stride+x]
} }
func (p *RGBA) Set(x, y int, c Color) { func (p *RGBA) Set(x, y int, c Color) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = toRGBAColor(c).(RGBAColor) p.Pix[y*p.Stride+x] = toRGBAColor(c).(RGBAColor)
} }
func (p *RGBA) SetRGBA(x, y int, c RGBAColor) { func (p *RGBA) SetRGBA(x, y int, c RGBAColor) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = c p.Pix[y*p.Stride+x] = c
@ -107,21 +107,21 @@ func (p *RGBA64) ColorModel() ColorModel { return RGBA64ColorModel }
func (p *RGBA64) Bounds() Rectangle { return p.Rect } func (p *RGBA64) Bounds() Rectangle { return p.Rect }
func (p *RGBA64) At(x, y int) Color { func (p *RGBA64) At(x, y int) Color {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return RGBA64Color{} return RGBA64Color{}
} }
return p.Pix[y*p.Stride+x] return p.Pix[y*p.Stride+x]
} }
func (p *RGBA64) Set(x, y int, c Color) { func (p *RGBA64) Set(x, y int, c Color) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = toRGBA64Color(c).(RGBA64Color) p.Pix[y*p.Stride+x] = toRGBA64Color(c).(RGBA64Color)
} }
func (p *RGBA64) SetRGBA64(x, y int, c RGBA64Color) { func (p *RGBA64) SetRGBA64(x, y int, c RGBA64Color) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = c p.Pix[y*p.Stride+x] = c
@ -176,21 +176,21 @@ func (p *NRGBA) ColorModel() ColorModel { return NRGBAColorModel }
func (p *NRGBA) Bounds() Rectangle { return p.Rect } func (p *NRGBA) Bounds() Rectangle { return p.Rect }
func (p *NRGBA) At(x, y int) Color { func (p *NRGBA) At(x, y int) Color {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return NRGBAColor{} return NRGBAColor{}
} }
return p.Pix[y*p.Stride+x] return p.Pix[y*p.Stride+x]
} }
func (p *NRGBA) Set(x, y int, c Color) { func (p *NRGBA) Set(x, y int, c Color) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = toNRGBAColor(c).(NRGBAColor) p.Pix[y*p.Stride+x] = toNRGBAColor(c).(NRGBAColor)
} }
func (p *NRGBA) SetNRGBA(x, y int, c NRGBAColor) { func (p *NRGBA) SetNRGBA(x, y int, c NRGBAColor) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = c p.Pix[y*p.Stride+x] = c
@ -245,21 +245,21 @@ func (p *NRGBA64) ColorModel() ColorModel { return NRGBA64ColorModel }
func (p *NRGBA64) Bounds() Rectangle { return p.Rect } func (p *NRGBA64) Bounds() Rectangle { return p.Rect }
func (p *NRGBA64) At(x, y int) Color { func (p *NRGBA64) At(x, y int) Color {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return NRGBA64Color{} return NRGBA64Color{}
} }
return p.Pix[y*p.Stride+x] return p.Pix[y*p.Stride+x]
} }
func (p *NRGBA64) Set(x, y int, c Color) { func (p *NRGBA64) Set(x, y int, c Color) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = toNRGBA64Color(c).(NRGBA64Color) p.Pix[y*p.Stride+x] = toNRGBA64Color(c).(NRGBA64Color)
} }
func (p *NRGBA64) SetNRGBA64(x, y int, c NRGBA64Color) { func (p *NRGBA64) SetNRGBA64(x, y int, c NRGBA64Color) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = c p.Pix[y*p.Stride+x] = c
@ -314,21 +314,21 @@ func (p *Alpha) ColorModel() ColorModel { return AlphaColorModel }
func (p *Alpha) Bounds() Rectangle { return p.Rect } func (p *Alpha) Bounds() Rectangle { return p.Rect }
func (p *Alpha) At(x, y int) Color { func (p *Alpha) At(x, y int) Color {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return AlphaColor{} return AlphaColor{}
} }
return p.Pix[y*p.Stride+x] return p.Pix[y*p.Stride+x]
} }
func (p *Alpha) Set(x, y int, c Color) { func (p *Alpha) Set(x, y int, c Color) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = toAlphaColor(c).(AlphaColor) p.Pix[y*p.Stride+x] = toAlphaColor(c).(AlphaColor)
} }
func (p *Alpha) SetAlpha(x, y int, c AlphaColor) { func (p *Alpha) SetAlpha(x, y int, c AlphaColor) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = c p.Pix[y*p.Stride+x] = c
@ -383,21 +383,21 @@ func (p *Alpha16) ColorModel() ColorModel { return Alpha16ColorModel }
func (p *Alpha16) Bounds() Rectangle { return p.Rect } func (p *Alpha16) Bounds() Rectangle { return p.Rect }
func (p *Alpha16) At(x, y int) Color { func (p *Alpha16) At(x, y int) Color {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return Alpha16Color{} return Alpha16Color{}
} }
return p.Pix[y*p.Stride+x] return p.Pix[y*p.Stride+x]
} }
func (p *Alpha16) Set(x, y int, c Color) { func (p *Alpha16) Set(x, y int, c Color) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = toAlpha16Color(c).(Alpha16Color) p.Pix[y*p.Stride+x] = toAlpha16Color(c).(Alpha16Color)
} }
func (p *Alpha16) SetAlpha16(x, y int, c Alpha16Color) { func (p *Alpha16) SetAlpha16(x, y int, c Alpha16Color) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = c p.Pix[y*p.Stride+x] = c
@ -452,21 +452,21 @@ func (p *Gray) ColorModel() ColorModel { return GrayColorModel }
func (p *Gray) Bounds() Rectangle { return p.Rect } func (p *Gray) Bounds() Rectangle { return p.Rect }
func (p *Gray) At(x, y int) Color { func (p *Gray) At(x, y int) Color {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return GrayColor{} return GrayColor{}
} }
return p.Pix[y*p.Stride+x] return p.Pix[y*p.Stride+x]
} }
func (p *Gray) Set(x, y int, c Color) { func (p *Gray) Set(x, y int, c Color) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = toGrayColor(c).(GrayColor) p.Pix[y*p.Stride+x] = toGrayColor(c).(GrayColor)
} }
func (p *Gray) SetGray(x, y int, c GrayColor) { func (p *Gray) SetGray(x, y int, c GrayColor) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = c p.Pix[y*p.Stride+x] = c
@ -507,21 +507,21 @@ func (p *Gray16) ColorModel() ColorModel { return Gray16ColorModel }
func (p *Gray16) Bounds() Rectangle { return p.Rect } func (p *Gray16) Bounds() Rectangle { return p.Rect }
func (p *Gray16) At(x, y int) Color { func (p *Gray16) At(x, y int) Color {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return Gray16Color{} return Gray16Color{}
} }
return p.Pix[y*p.Stride+x] return p.Pix[y*p.Stride+x]
} }
func (p *Gray16) Set(x, y int, c Color) { func (p *Gray16) Set(x, y int, c Color) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = toGray16Color(c).(Gray16Color) p.Pix[y*p.Stride+x] = toGray16Color(c).(Gray16Color)
} }
func (p *Gray16) SetGray16(x, y int, c Gray16Color) { func (p *Gray16) SetGray16(x, y int, c Gray16Color) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = c p.Pix[y*p.Stride+x] = c
@ -604,21 +604,21 @@ func (p *Paletted) At(x, y int) Color {
if len(p.Palette) == 0 { if len(p.Palette) == 0 {
return nil return nil
} }
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return p.Palette[0] return p.Palette[0]
} }
return p.Palette[p.Pix[y*p.Stride+x]] return p.Palette[p.Pix[y*p.Stride+x]]
} }
func (p *Paletted) ColorIndexAt(x, y int) uint8 { func (p *Paletted) ColorIndexAt(x, y int) uint8 {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return 0 return 0
} }
return p.Pix[y*p.Stride+x] return p.Pix[y*p.Stride+x]
} }
func (p *Paletted) SetColorIndex(x, y int, index uint8) { func (p *Paletted) SetColorIndex(x, y int, index uint8) {
if !p.Rect.Contains(Point{x, y}) { if !(Point{x, y}.In(p.Rect)) {
return return
} }
p.Pix[y*p.Stride+x] = index p.Pix[y*p.Stride+x] = index

View File

@ -142,7 +142,7 @@ func (p *YCbCr) Bounds() image.Rectangle {
} }
func (p *YCbCr) At(x, y int) image.Color { func (p *YCbCr) At(x, y int) image.Color {
if !p.Rect.Contains(image.Point{x, y}) { if !(image.Point{x, y}.In(p.Rect)) {
return YCbCrColor{} return YCbCrColor{}
} }
switch p.SubsampleRatio { switch p.SubsampleRatio {