| // Copyright 2010 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package image |
| |
| import ( |
| "strconv" |
| ) |
| |
| // A Point is an X, Y coordinate pair. The axes increase right and down. |
| type Point struct { |
| X, Y int |
| } |
| |
| // String returns a string representation of p like "(3,4)". |
| func (p Point) String() string { |
| return "(" + strconv.Itoa(p.X) + "," + strconv.Itoa(p.Y) + ")" |
| } |
| |
| // Add returns the vector p+q. |
| func (p Point) Add(q Point) Point { |
| return Point{p.X + q.X, p.Y + q.Y} |
| } |
| |
| // Sub returns the vector p-q. |
| func (p Point) Sub(q Point) Point { |
| return Point{p.X - q.X, p.Y - q.Y} |
| } |
| |
| // ZP is the zero Point. |
| var ZP Point |
| |
| // Pt is shorthand for Point{X, Y}. |
| func Pt(X, Y int) Point { |
| return Point{X, Y} |
| } |
| |
| // A Rectangle contains the points with Min.X <= X < Max.X, Min.Y <= Y < Max.Y. |
| type Rectangle struct { |
| Min, Max Point |
| } |
| |
| // String returns a string representation of r like "(3,4)-(6,5)". |
| func (r Rectangle) String() string { |
| return r.Min.String() + "-" + r.Max.String() |
| } |
| |
| // Dx returns r's width. |
| func (r Rectangle) Dx() int { |
| return r.Max.X - r.Min.X |
| } |
| |
| // Dy returns r's height. |
| func (r Rectangle) Dy() int { |
| return r.Max.Y - r.Min.Y |
| } |
| |
| // Add returns the rectangle r translated by p. |
| func (r Rectangle) Add(p Point) Rectangle { |
| return Rectangle{ |
| Point{r.Min.X + p.X, r.Min.Y + p.Y}, |
| Point{r.Max.X + p.X, r.Max.Y + p.Y}, |
| } |
| } |
| |
| // Add returns the rectangle r translated by -p. |
| func (r Rectangle) Sub(p Point) Rectangle { |
| return Rectangle{ |
| Point{r.Min.X - p.X, r.Min.Y - p.Y}, |
| Point{r.Max.X - p.X, r.Max.Y - p.Y}, |
| } |
| } |
| |
| // Inset returns the rectangle r inset by n, which may be negative. |
| func (r Rectangle) Inset(n int) Rectangle { |
| return Rectangle{ |
| Point{r.Min.X + n, r.Min.Y + n}, |
| Point{r.Max.X - n, r.Max.Y - n}, |
| } |
| } |
| |
| // Empty returns whether the rectangle contains no points. |
| func (r Rectangle) Empty() bool { |
| return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y |
| } |
| |
| // Eq returns whether r and s are equal. |
| func (r Rectangle) Eq(s Rectangle) bool { |
| return r.Min.X == s.Min.X && r.Min.Y == s.Min.Y && |
| r.Max.X == s.Max.X && r.Max.Y == s.Max.Y |
| } |
| |
| // Overlaps returns whether r and s have a non-empty intersection. |
| func (r Rectangle) Overlaps(s Rectangle) bool { |
| return r.Min.X < s.Max.X && s.Min.X < r.Max.X && |
| r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y |
| } |
| |
| // Contains returns whether r contains p. |
| func (r Rectangle) Contains(p Point) bool { |
| return p.X >= r.Min.X && p.X < r.Max.X && |
| p.Y >= r.Min.Y && p.Y < r.Max.Y |
| } |
| |
| // Canon returns the canonical version of r. The returned rectangle has |
| // minimum and maximum coordinates swapped if necessary so that Min.X <= Max.X |
| // and Min.Y <= Max.Y. |
| func (r Rectangle) Canon() Rectangle { |
| if r.Max.X < r.Min.X { |
| r.Min.X, r.Max.X = r.Max.X, r.Min.X |
| } |
| if r.Max.Y < r.Min.Y { |
| r.Min.Y, r.Max.Y = r.Max.Y, r.Min.Y |
| } |
| return r |
| } |
| |
| // ZR is the zero Rectangle. |
| var ZR Rectangle |
| |
| // Rect is shorthand for Rectangle{Pt(x0, y0), Pt(x1, y1)}. |
| func Rect(x0, y0, x1, y1 int) Rectangle { |
| if x0 > x1 { |
| x0, x1 = x1, x0 |
| } |
| if y0 > y1 { |
| y0, y1 = y1, y0 |
| } |
| return Rectangle{Point{x0, y0}, Point{x1, y1}} |
| } |