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 ( |
Nigel Tao | 848e2fe | 2015-03-03 12:59:23 +1100 | [diff] [blame] | 8 | "image/color" |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 9 | "strconv" |
| 10 | ) |
| 11 | |
| 12 | // A Point is an X, Y coordinate pair. The axes increase right and down. |
| 13 | type Point struct { |
| 14 | X, Y int |
| 15 | } |
| 16 | |
| 17 | // String returns a string representation of p like "(3,4)". |
| 18 | func (p Point) String() string { |
| 19 | return "(" + strconv.Itoa(p.X) + "," + strconv.Itoa(p.Y) + ")" |
| 20 | } |
| 21 | |
| 22 | // Add returns the vector p+q. |
| 23 | func (p Point) Add(q Point) Point { |
| 24 | return Point{p.X + q.X, p.Y + q.Y} |
| 25 | } |
| 26 | |
| 27 | // Sub returns the vector p-q. |
| 28 | func (p Point) Sub(q Point) Point { |
| 29 | return Point{p.X - q.X, p.Y - q.Y} |
| 30 | } |
| 31 | |
Roger Peppe | 0f17173 | 2010-09-23 10:58:59 +1000 | [diff] [blame] | 32 | // Mul returns the vector p*k. |
| 33 | func (p Point) Mul(k int) Point { |
| 34 | return Point{p.X * k, p.Y * k} |
| 35 | } |
| 36 | |
| 37 | // Div returns the vector p/k. |
| 38 | func (p Point) Div(k int) Point { |
| 39 | return Point{p.X / k, p.Y / k} |
| 40 | } |
| 41 | |
Rob Pike | abe384f | 2013-07-23 11:59:49 +1000 | [diff] [blame] | 42 | // In reports whether p is in r. |
Nigel Tao | f531ef3 | 2011-06-03 13:18:15 +1000 | [diff] [blame] | 43 | func (p Point) In(r Rectangle) bool { |
| 44 | return r.Min.X <= p.X && p.X < r.Max.X && |
| 45 | r.Min.Y <= p.Y && p.Y < r.Max.Y |
| 46 | } |
| 47 | |
Nigel Tao | b5a480f | 2010-10-12 13:44:11 +1100 | [diff] [blame] | 48 | // Mod returns the point q in r such that p.X-q.X is a multiple of r's width |
| 49 | // and p.Y-q.Y is a multiple of r's height. |
| 50 | func (p Point) Mod(r Rectangle) Point { |
| 51 | w, h := r.Dx(), r.Dy() |
| 52 | p = p.Sub(r.Min) |
Nigel Tao | 93159e3 | 2010-10-13 12:05:21 +1100 | [diff] [blame] | 53 | p.X = p.X % w |
| 54 | if p.X < 0 { |
| 55 | p.X += w |
Nigel Tao | b5a480f | 2010-10-12 13:44:11 +1100 | [diff] [blame] | 56 | } |
Nigel Tao | 93159e3 | 2010-10-13 12:05:21 +1100 | [diff] [blame] | 57 | p.Y = p.Y % h |
| 58 | if p.Y < 0 { |
| 59 | p.Y += h |
Nigel Tao | b5a480f | 2010-10-12 13:44:11 +1100 | [diff] [blame] | 60 | } |
| 61 | return p.Add(r.Min) |
| 62 | } |
| 63 | |
Rob Pike | abe384f | 2013-07-23 11:59:49 +1000 | [diff] [blame] | 64 | // Eq reports whether p and q are equal. |
Roger Peppe | 0f17173 | 2010-09-23 10:58:59 +1000 | [diff] [blame] | 65 | func (p Point) Eq(q Point) bool { |
Nigel Tao | 84c7a65 | 2015-02-17 16:28:10 +1100 | [diff] [blame] | 66 | return p == q |
Roger Peppe | 0f17173 | 2010-09-23 10:58:59 +1000 | [diff] [blame] | 67 | } |
| 68 | |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 69 | // ZP is the zero Point. |
| 70 | var ZP Point |
| 71 | |
| 72 | // Pt is shorthand for Point{X, Y}. |
| 73 | func Pt(X, Y int) Point { |
| 74 | return Point{X, Y} |
| 75 | } |
| 76 | |
| 77 | // 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] | 78 | // It is well-formed if Min.X <= Max.X and likewise for Y. Points are always |
| 79 | // well-formed. A rectangle's methods always return well-formed outputs for |
| 80 | // well-formed inputs. |
Nigel Tao | 848e2fe | 2015-03-03 12:59:23 +1100 | [diff] [blame] | 81 | // |
| 82 | // A Rectangle is also an Image whose bounds are the rectangle itself. At |
| 83 | // returns color.Opaque for points in the rectangle and color.Transparent |
| 84 | // otherwise. |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 85 | type Rectangle struct { |
| 86 | Min, Max Point |
| 87 | } |
| 88 | |
| 89 | // String returns a string representation of r like "(3,4)-(6,5)". |
| 90 | func (r Rectangle) String() string { |
| 91 | return r.Min.String() + "-" + r.Max.String() |
| 92 | } |
| 93 | |
| 94 | // Dx returns r's width. |
| 95 | func (r Rectangle) Dx() int { |
| 96 | return r.Max.X - r.Min.X |
| 97 | } |
| 98 | |
| 99 | // Dy returns r's height. |
| 100 | func (r Rectangle) Dy() int { |
| 101 | return r.Max.Y - r.Min.Y |
| 102 | } |
| 103 | |
Roger Peppe | 0f17173 | 2010-09-23 10:58:59 +1000 | [diff] [blame] | 104 | // Size returns r's width and height. |
| 105 | func (r Rectangle) Size() Point { |
| 106 | return Point{ |
| 107 | r.Max.X - r.Min.X, |
| 108 | r.Max.Y - r.Min.Y, |
| 109 | } |
| 110 | } |
| 111 | |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 112 | // Add returns the rectangle r translated by p. |
| 113 | func (r Rectangle) Add(p Point) Rectangle { |
| 114 | return Rectangle{ |
| 115 | Point{r.Min.X + p.X, r.Min.Y + p.Y}, |
| 116 | Point{r.Max.X + p.X, r.Max.Y + p.Y}, |
| 117 | } |
| 118 | } |
| 119 | |
Nigel Tao | a5a16ee | 2012-01-18 10:55:03 +1100 | [diff] [blame] | 120 | // Sub returns the rectangle r translated by -p. |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 121 | func (r Rectangle) Sub(p Point) Rectangle { |
| 122 | return Rectangle{ |
| 123 | Point{r.Min.X - p.X, r.Min.Y - p.Y}, |
| 124 | Point{r.Max.X - p.X, r.Max.Y - p.Y}, |
| 125 | } |
| 126 | } |
| 127 | |
Nigel Tao | 2deee29 | 2010-09-09 19:12:54 +1000 | [diff] [blame] | 128 | // Inset returns the rectangle r inset by n, which may be negative. If either |
| 129 | // of r's dimensions is less than 2*n then an empty rectangle near the center |
| 130 | // of r will be returned. |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 131 | func (r Rectangle) Inset(n int) Rectangle { |
Nigel Tao | 2deee29 | 2010-09-09 19:12:54 +1000 | [diff] [blame] | 132 | if r.Dx() < 2*n { |
| 133 | r.Min.X = (r.Min.X + r.Max.X) / 2 |
| 134 | r.Max.X = r.Min.X |
| 135 | } else { |
| 136 | r.Min.X += n |
| 137 | r.Max.X -= n |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 138 | } |
Nigel Tao | 2deee29 | 2010-09-09 19:12:54 +1000 | [diff] [blame] | 139 | if r.Dy() < 2*n { |
| 140 | r.Min.Y = (r.Min.Y + r.Max.Y) / 2 |
| 141 | r.Max.Y = r.Min.Y |
| 142 | } else { |
| 143 | r.Min.Y += n |
| 144 | r.Max.Y -= n |
| 145 | } |
| 146 | return r |
| 147 | } |
| 148 | |
| 149 | // Intersect returns the largest rectangle contained by both r and s. If the |
| 150 | // two rectangles do not overlap then the zero rectangle will be returned. |
| 151 | func (r Rectangle) Intersect(s Rectangle) Rectangle { |
| 152 | if r.Min.X < s.Min.X { |
| 153 | r.Min.X = s.Min.X |
| 154 | } |
| 155 | if r.Min.Y < s.Min.Y { |
| 156 | r.Min.Y = s.Min.Y |
| 157 | } |
| 158 | if r.Max.X > s.Max.X { |
| 159 | r.Max.X = s.Max.X |
| 160 | } |
| 161 | if r.Max.Y > s.Max.Y { |
| 162 | r.Max.Y = s.Max.Y |
| 163 | } |
| 164 | if r.Min.X > r.Max.X || r.Min.Y > r.Max.Y { |
| 165 | return ZR |
| 166 | } |
| 167 | return r |
| 168 | } |
| 169 | |
| 170 | // Union returns the smallest rectangle that contains both r and s. |
| 171 | func (r Rectangle) Union(s Rectangle) Rectangle { |
Nigel Tao | 5c8f9e3 | 2015-02-17 11:15:06 +1100 | [diff] [blame] | 172 | if r.Empty() { |
| 173 | return s |
| 174 | } |
| 175 | if s.Empty() { |
| 176 | return r |
| 177 | } |
Nigel Tao | 2deee29 | 2010-09-09 19:12:54 +1000 | [diff] [blame] | 178 | if r.Min.X > s.Min.X { |
| 179 | r.Min.X = s.Min.X |
| 180 | } |
| 181 | if r.Min.Y > s.Min.Y { |
| 182 | r.Min.Y = s.Min.Y |
| 183 | } |
| 184 | if r.Max.X < s.Max.X { |
| 185 | r.Max.X = s.Max.X |
| 186 | } |
| 187 | if r.Max.Y < s.Max.Y { |
| 188 | r.Max.Y = s.Max.Y |
| 189 | } |
| 190 | return r |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 191 | } |
| 192 | |
Rob Pike | abe384f | 2013-07-23 11:59:49 +1000 | [diff] [blame] | 193 | // Empty reports whether the rectangle contains no points. |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 194 | func (r Rectangle) Empty() bool { |
| 195 | return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y |
| 196 | } |
| 197 | |
Nigel Tao | 84c7a65 | 2015-02-17 16:28:10 +1100 | [diff] [blame] | 198 | // Eq reports whether r and s contain the same set of points. All empty |
| 199 | // rectangles are considered equal. |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 200 | func (r Rectangle) Eq(s Rectangle) bool { |
Nigel Tao | 84c7a65 | 2015-02-17 16:28:10 +1100 | [diff] [blame] | 201 | return r == s || r.Empty() && s.Empty() |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 202 | } |
| 203 | |
Rob Pike | abe384f | 2013-07-23 11:59:49 +1000 | [diff] [blame] | 204 | // Overlaps reports whether r and s have a non-empty intersection. |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 205 | func (r Rectangle) Overlaps(s Rectangle) bool { |
Nigel Tao | 5c8f9e3 | 2015-02-17 11:15:06 +1100 | [diff] [blame] | 206 | return !r.Empty() && !s.Empty() && |
| 207 | r.Min.X < s.Max.X && s.Min.X < r.Max.X && |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 208 | r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y |
| 209 | } |
| 210 | |
Rob Pike | abe384f | 2013-07-23 11:59:49 +1000 | [diff] [blame] | 211 | // In reports whether every point in r is in s. |
Nigel Tao | f531ef3 | 2011-06-03 13:18:15 +1000 | [diff] [blame] | 212 | func (r Rectangle) In(s Rectangle) bool { |
| 213 | if r.Empty() { |
Nigel Tao | ae5a972 | 2011-06-03 11:43:54 +1000 | [diff] [blame] | 214 | return true |
| 215 | } |
Nigel Tao | f531ef3 | 2011-06-03 13:18:15 +1000 | [diff] [blame] | 216 | // Note that r.Max is an exclusive bound for r, so that r.In(s) |
| 217 | // does not require that r.Max.In(s). |
| 218 | return s.Min.X <= r.Min.X && r.Max.X <= s.Max.X && |
| 219 | s.Min.Y <= r.Min.Y && r.Max.Y <= s.Max.Y |
Nigel Tao | b50a3d9 | 2010-08-10 16:34:57 +1000 | [diff] [blame] | 220 | } |
| 221 | |
Nigel Tao | 2deee29 | 2010-09-09 19:12:54 +1000 | [diff] [blame] | 222 | // Canon returns the canonical version of r. The returned rectangle has minimum |
| 223 | // and maximum coordinates swapped if necessary so that it is well-formed. |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 224 | func (r Rectangle) Canon() Rectangle { |
| 225 | if r.Max.X < r.Min.X { |
| 226 | r.Min.X, r.Max.X = r.Max.X, r.Min.X |
| 227 | } |
| 228 | if r.Max.Y < r.Min.Y { |
| 229 | r.Min.Y, r.Max.Y = r.Max.Y, r.Min.Y |
| 230 | } |
| 231 | return r |
| 232 | } |
| 233 | |
Nigel Tao | 848e2fe | 2015-03-03 12:59:23 +1100 | [diff] [blame] | 234 | // At implements the Image interface. |
| 235 | func (r Rectangle) At(x, y int) color.Color { |
| 236 | if (Point{x, y}).In(r) { |
| 237 | return color.Opaque |
| 238 | } |
| 239 | return color.Transparent |
| 240 | } |
| 241 | |
| 242 | // Bounds implements the Image interface. |
| 243 | func (r Rectangle) Bounds() Rectangle { |
| 244 | return r |
| 245 | } |
| 246 | |
| 247 | // ColorModel implements the Image interface. |
| 248 | func (r Rectangle) ColorModel() color.Model { |
| 249 | return color.Alpha16Model |
| 250 | } |
| 251 | |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 252 | // ZR is the zero Rectangle. |
| 253 | var ZR Rectangle |
| 254 | |
Nigel Tao | 84c7a65 | 2015-02-17 16:28:10 +1100 | [diff] [blame] | 255 | // Rect is shorthand for Rectangle{Pt(x0, y0), Pt(x1, y1)}. The returned |
| 256 | // rectangle has minimum and maximum coordinates swapped if necessary so that |
| 257 | // it is well-formed. |
Nigel Tao | 5eb35e4 | 2010-08-10 12:08:52 +1000 | [diff] [blame] | 258 | func Rect(x0, y0, x1, y1 int) Rectangle { |
| 259 | if x0 > x1 { |
| 260 | x0, x1 = x1, x0 |
| 261 | } |
| 262 | if y0 > y1 { |
| 263 | y0, y1 = y1, y0 |
| 264 | } |
| 265 | return Rectangle{Point{x0, y0}, Point{x1, y1}} |
| 266 | } |