Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 1 | // Copyright 2010 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package image |
| 6 | |
| 7 | import ( |
| 8 | "strconv" |
| 9 | ) |
| 10 | |
| 11 | // A Point is an X, Y coordinate pair. The axes increase right and down. |
| 12 | type Point struct { |
| 13 | X, Y int |
| 14 | } |
| 15 | |
| 16 | // String returns a string representation of p like "(3,4)". |
| 17 | func (p Point) String() string { |
| 18 | return "(" + strconv.Itoa(p.X) + "," + strconv.Itoa(p.Y) + ")" |
| 19 | } |
| 20 | |
| 21 | // Add returns the vector p+q. |
| 22 | func (p Point) Add(q Point) Point { |
| 23 | return Point{p.X + q.X, p.Y + q.Y} |
| 24 | } |
| 25 | |
| 26 | // Sub returns the vector p-q. |
| 27 | func (p Point) Sub(q Point) Point { |
| 28 | return Point{p.X - q.X, p.Y - q.Y} |
| 29 | } |
| 30 | |
Roger Peppe | 0f17173 | 2010-09-23 10:58:59 +1000 | [diff] [blame] | 31 | // Mul returns the vector p*k. |
| 32 | func (p Point) Mul(k int) Point { |
| 33 | return Point{p.X * k, p.Y * k} |
| 34 | } |
| 35 | |
| 36 | // Div returns the vector p/k. |
| 37 | func (p Point) Div(k int) Point { |
| 38 | return Point{p.X / k, p.Y / k} |
| 39 | } |
| 40 | |
Nigel Tao | b5a480f | 2010-10-12 13:44:11 +1100 | [diff] [blame^] | 41 | // Mod returns the point q in r such that p.X-q.X is a multiple of r's width |
| 42 | // and p.Y-q.Y is a multiple of r's height. |
| 43 | func (p Point) Mod(r Rectangle) Point { |
| 44 | w, h := r.Dx(), r.Dy() |
| 45 | p = p.Sub(r.Min) |
| 46 | if p.X >= 0 { |
| 47 | p.X = p.X % w |
| 48 | } else { |
| 49 | p.X = w - 1 - (-1-p.X)%w |
| 50 | } |
| 51 | if p.Y >= 0 { |
| 52 | p.Y = p.Y % h |
| 53 | } else { |
| 54 | p.Y = h - 1 - (-1-p.Y)%h |
| 55 | } |
| 56 | return p.Add(r.Min) |
| 57 | } |
| 58 | |
Roger Peppe | 0f17173 | 2010-09-23 10:58:59 +1000 | [diff] [blame] | 59 | // Eq returns whether p and q are equal. |
| 60 | func (p Point) Eq(q Point) bool { |
| 61 | return p.X == q.X && p.Y == q.Y |
| 62 | } |
| 63 | |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 64 | // ZP is the zero Point. |
| 65 | var ZP Point |
| 66 | |
| 67 | // Pt is shorthand for Point{X, Y}. |
| 68 | func Pt(X, Y int) Point { |
| 69 | return Point{X, Y} |
| 70 | } |
| 71 | |
| 72 | // A Rectangle contains the points with Min.X <= X < Max.X, Min.Y <= Y < Max.Y. |
Nigel Tao | 2deee29 | 2010-09-09 19:12:54 +1000 | [diff] [blame] | 73 | // It is well-formed if Min.X <= Max.X and likewise for Y. Points are always |
| 74 | // well-formed. A rectangle's methods always return well-formed outputs for |
| 75 | // well-formed inputs. |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 76 | type Rectangle struct { |
| 77 | Min, Max Point |
| 78 | } |
| 79 | |
| 80 | // String returns a string representation of r like "(3,4)-(6,5)". |
| 81 | func (r Rectangle) String() string { |
| 82 | return r.Min.String() + "-" + r.Max.String() |
| 83 | } |
| 84 | |
| 85 | // Dx returns r's width. |
| 86 | func (r Rectangle) Dx() int { |
| 87 | return r.Max.X - r.Min.X |
| 88 | } |
| 89 | |
| 90 | // Dy returns r's height. |
| 91 | func (r Rectangle) Dy() int { |
| 92 | return r.Max.Y - r.Min.Y |
| 93 | } |
| 94 | |
Roger Peppe | 0f17173 | 2010-09-23 10:58:59 +1000 | [diff] [blame] | 95 | // Size returns r's width and height. |
| 96 | func (r Rectangle) Size() Point { |
| 97 | return Point{ |
| 98 | r.Max.X - r.Min.X, |
| 99 | r.Max.Y - r.Min.Y, |
| 100 | } |
| 101 | } |
| 102 | |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 103 | // Add returns the rectangle r translated by p. |
| 104 | func (r Rectangle) Add(p Point) Rectangle { |
| 105 | return Rectangle{ |
| 106 | Point{r.Min.X + p.X, r.Min.Y + p.Y}, |
| 107 | Point{r.Max.X + p.X, r.Max.Y + p.Y}, |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Add returns the rectangle r translated by -p. |
| 112 | func (r Rectangle) Sub(p Point) Rectangle { |
| 113 | return Rectangle{ |
| 114 | Point{r.Min.X - p.X, r.Min.Y - p.Y}, |
| 115 | Point{r.Max.X - p.X, r.Max.Y - p.Y}, |
| 116 | } |
| 117 | } |
| 118 | |
Nigel Tao | 2deee29 | 2010-09-09 19:12:54 +1000 | [diff] [blame] | 119 | // Inset returns the rectangle r inset by n, which may be negative. If either |
| 120 | // of r's dimensions is less than 2*n then an empty rectangle near the center |
| 121 | // of r will be returned. |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 122 | func (r Rectangle) Inset(n int) Rectangle { |
Nigel Tao | 2deee29 | 2010-09-09 19:12:54 +1000 | [diff] [blame] | 123 | if r.Dx() < 2*n { |
| 124 | r.Min.X = (r.Min.X + r.Max.X) / 2 |
| 125 | r.Max.X = r.Min.X |
| 126 | } else { |
| 127 | r.Min.X += n |
| 128 | r.Max.X -= n |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 129 | } |
Nigel Tao | 2deee29 | 2010-09-09 19:12:54 +1000 | [diff] [blame] | 130 | if r.Dy() < 2*n { |
| 131 | r.Min.Y = (r.Min.Y + r.Max.Y) / 2 |
| 132 | r.Max.Y = r.Min.Y |
| 133 | } else { |
| 134 | r.Min.Y += n |
| 135 | r.Max.Y -= n |
| 136 | } |
| 137 | return r |
| 138 | } |
| 139 | |
| 140 | // Intersect returns the largest rectangle contained by both r and s. If the |
| 141 | // two rectangles do not overlap then the zero rectangle will be returned. |
| 142 | func (r Rectangle) Intersect(s Rectangle) Rectangle { |
| 143 | if r.Min.X < s.Min.X { |
| 144 | r.Min.X = s.Min.X |
| 145 | } |
| 146 | if r.Min.Y < s.Min.Y { |
| 147 | r.Min.Y = s.Min.Y |
| 148 | } |
| 149 | if r.Max.X > s.Max.X { |
| 150 | r.Max.X = s.Max.X |
| 151 | } |
| 152 | if r.Max.Y > s.Max.Y { |
| 153 | r.Max.Y = s.Max.Y |
| 154 | } |
| 155 | if r.Min.X > r.Max.X || r.Min.Y > r.Max.Y { |
| 156 | return ZR |
| 157 | } |
| 158 | return r |
| 159 | } |
| 160 | |
| 161 | // Union returns the smallest rectangle that contains both r and s. |
| 162 | func (r Rectangle) Union(s Rectangle) Rectangle { |
| 163 | if r.Min.X > s.Min.X { |
| 164 | r.Min.X = s.Min.X |
| 165 | } |
| 166 | if r.Min.Y > s.Min.Y { |
| 167 | r.Min.Y = s.Min.Y |
| 168 | } |
| 169 | if r.Max.X < s.Max.X { |
| 170 | r.Max.X = s.Max.X |
| 171 | } |
| 172 | if r.Max.Y < s.Max.Y { |
| 173 | r.Max.Y = s.Max.Y |
| 174 | } |
| 175 | return r |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // Empty returns whether the rectangle contains no points. |
| 179 | func (r Rectangle) Empty() bool { |
| 180 | return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y |
| 181 | } |
| 182 | |
| 183 | // Eq returns whether r and s are equal. |
| 184 | func (r Rectangle) Eq(s Rectangle) bool { |
| 185 | return r.Min.X == s.Min.X && r.Min.Y == s.Min.Y && |
| 186 | r.Max.X == s.Max.X && r.Max.Y == s.Max.Y |
| 187 | } |
| 188 | |
| 189 | // Overlaps returns whether r and s have a non-empty intersection. |
| 190 | func (r Rectangle) Overlaps(s Rectangle) bool { |
| 191 | return r.Min.X < s.Max.X && s.Min.X < r.Max.X && |
| 192 | r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y |
| 193 | } |
| 194 | |
Nigel Tao | b50a3d9 | 2010-08-10 16:34:57 +1000 | [diff] [blame] | 195 | // Contains returns whether r contains p. |
| 196 | func (r Rectangle) Contains(p Point) bool { |
| 197 | return p.X >= r.Min.X && p.X < r.Max.X && |
| 198 | p.Y >= r.Min.Y && p.Y < r.Max.Y |
| 199 | } |
| 200 | |
Nigel Tao | 2deee29 | 2010-09-09 19:12:54 +1000 | [diff] [blame] | 201 | // Canon returns the canonical version of r. The returned rectangle has minimum |
| 202 | // and maximum coordinates swapped if necessary so that it is well-formed. |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 203 | func (r Rectangle) Canon() Rectangle { |
| 204 | if r.Max.X < r.Min.X { |
| 205 | r.Min.X, r.Max.X = r.Max.X, r.Min.X |
| 206 | } |
| 207 | if r.Max.Y < r.Min.Y { |
| 208 | r.Min.Y, r.Max.Y = r.Max.Y, r.Min.Y |
| 209 | } |
| 210 | return r |
| 211 | } |
| 212 | |
| 213 | // ZR is the zero Rectangle. |
| 214 | var ZR Rectangle |
| 215 | |
| 216 | // Rect is shorthand for Rectangle{Pt(x0, y0), Pt(x1, y1)}. |
| 217 | func Rect(x0, y0, x1, y1 int) Rectangle { |
| 218 | if x0 > x1 { |
| 219 | x0, x1 = x1, x0 |
| 220 | } |
| 221 | if y0 > y1 { |
| 222 | y0, y1 = y1, y0 |
| 223 | } |
| 224 | return Rectangle{Point{x0, y0}, Point{x1, y1}} |
| 225 | } |