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.
13 // A Point is an X, Y coordinate pair. The axes increase right and down.
18 // String returns a string representation of p like "(3,4)".
19 func (p Point) String() string {
20 return "(" + strconv.Itoa(p.X) + "," + strconv.Itoa(p.Y) + ")"
23 // Add returns the vector p+q.
24 func (p Point) Add(q Point) Point {
25 return Point{p.X + q.X, p.Y + q.Y}
28 // Sub returns the vector p-q.
29 func (p Point) Sub(q Point) Point {
30 return Point{p.X - q.X, p.Y - q.Y}
33 // Mul returns the vector p*k.
34 func (p Point) Mul(k int) Point {
35 return Point{p.X * k, p.Y * k}
38 // Div returns the vector p/k.
39 func (p Point) Div(k int) Point {
40 return Point{p.X / k, p.Y / k}
43 // In reports whether p is in r.
44 func (p Point) In(r Rectangle) bool {
45 return r.Min.X <= p.X && p.X < r.Max.X &&
46 r.Min.Y <= p.Y && p.Y < r.Max.Y
49 // Mod returns the point q in r such that p.X-q.X is a multiple of r's width
50 // and p.Y-q.Y is a multiple of r's height.
51 func (p Point) Mod(r Rectangle) Point {
52 w, h := r.Dx(), r.Dy()
65 // Eq reports whether p and q are equal.
66 func (p Point) Eq(q Point) bool {
70 // ZP is the zero [Point].
72 // Deprecated: Use a literal [image.Point] instead.
75 // Pt is shorthand for [Point]{X, Y}.
76 func Pt(X, Y int) Point {
80 // A Rectangle contains the points with Min.X <= X < Max.X, Min.Y <= Y < Max.Y.
81 // It is well-formed if Min.X <= Max.X and likewise for Y. Points are always
82 // well-formed. A rectangle's methods always return well-formed outputs for
83 // well-formed inputs.
85 // A Rectangle is also an [Image] whose bounds are the rectangle itself. At
86 // returns color.Opaque for points in the rectangle and color.Transparent
88 type Rectangle struct {
92 // String returns a string representation of r like "(3,4)-(6,5)".
93 func (r Rectangle) String() string {
94 return r.Min.String() + "-" + r.Max.String()
97 // Dx returns r's width.
98 func (r Rectangle) Dx() int {
99 return r.Max.X - r.Min.X
102 // Dy returns r's height.
103 func (r Rectangle) Dy() int {
104 return r.Max.Y - r.Min.Y
107 // Size returns r's width and height.
108 func (r Rectangle) Size() Point {
115 // Add returns the rectangle r translated by p.
116 func (r Rectangle) Add(p Point) Rectangle {
118 Point{r.Min.X + p.X, r.Min.Y + p.Y},
119 Point{r.Max.X + p.X, r.Max.Y + p.Y},
123 // Sub returns the rectangle r translated by -p.
124 func (r Rectangle) Sub(p Point) Rectangle {
126 Point{r.Min.X - p.X, r.Min.Y - p.Y},
127 Point{r.Max.X - p.X, r.Max.Y - p.Y},
131 // Inset returns the rectangle r inset by n, which may be negative. If either
132 // of r's dimensions is less than 2*n then an empty rectangle near the center
133 // of r will be returned.
134 func (r Rectangle) Inset(n int) Rectangle {
136 r.Min.X = (r.Min.X + r.Max.X) / 2
143 r.Min.Y = (r.Min.Y + r.Max.Y) / 2
152 // Intersect returns the largest rectangle contained by both r and s. If the
153 // two rectangles do not overlap then the zero rectangle will be returned.
154 func (r Rectangle) Intersect(s Rectangle) Rectangle {
155 if r.Min.X < s.Min.X {
158 if r.Min.Y < s.Min.Y {
161 if r.Max.X > s.Max.X {
164 if r.Max.Y > s.Max.Y {
167 // Letting r0 and s0 be the values of r and s at the time that the method
168 // is called, this next line is equivalent to:
170 // if max(r0.Min.X, s0.Min.X) >= min(r0.Max.X, s0.Max.X) || likewiseForY { etc }
177 // Union returns the smallest rectangle that contains both r and s.
178 func (r Rectangle) Union(s Rectangle) Rectangle {
185 if r.Min.X > s.Min.X {
188 if r.Min.Y > s.Min.Y {
191 if r.Max.X < s.Max.X {
194 if r.Max.Y < s.Max.Y {
200 // Empty reports whether the rectangle contains no points.
201 func (r Rectangle) Empty() bool {
202 return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
205 // Eq reports whether r and s contain the same set of points. All empty
206 // rectangles are considered equal.
207 func (r Rectangle) Eq(s Rectangle) bool {
208 return r == s || r.Empty() && s.Empty()
211 // Overlaps reports whether r and s have a non-empty intersection.
212 func (r Rectangle) Overlaps(s Rectangle) bool {
213 return !r.Empty() && !s.Empty() &&
214 r.Min.X < s.Max.X && s.Min.X < r.Max.X &&
215 r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y
218 // In reports whether every point in r is in s.
219 func (r Rectangle) In(s Rectangle) bool {
223 // Note that r.Max is an exclusive bound for r, so that r.In(s)
224 // does not require that r.Max.In(s).
225 return s.Min.X <= r.Min.X && r.Max.X <= s.Max.X &&
226 s.Min.Y <= r.Min.Y && r.Max.Y <= s.Max.Y
229 // Canon returns the canonical version of r. The returned rectangle has minimum
230 // and maximum coordinates swapped if necessary so that it is well-formed.
231 func (r Rectangle) Canon() Rectangle {
232 if r.Max.X < r.Min.X {
233 r.Min.X, r.Max.X = r.Max.X, r.Min.X
235 if r.Max.Y < r.Min.Y {
236 r.Min.Y, r.Max.Y = r.Max.Y, r.Min.Y
241 // At implements the [Image] interface.
242 func (r Rectangle) At(x, y int) color.Color {
243 if (Point{x, y}).In(r) {
246 return color.Transparent
249 // RGBA64At implements the [RGBA64Image] interface.
250 func (r Rectangle) RGBA64At(x, y int) color.RGBA64 {
251 if (Point{x, y}).In(r) {
252 return color.RGBA64{0xffff, 0xffff, 0xffff, 0xffff}
254 return color.RGBA64{}
257 // Bounds implements the [Image] interface.
258 func (r Rectangle) Bounds() Rectangle {
262 // ColorModel implements the [Image] interface.
263 func (r Rectangle) ColorModel() color.Model {
264 return color.Alpha16Model
267 // ZR is the zero [Rectangle].
269 // Deprecated: Use a literal [image.Rectangle] instead.
272 // Rect is shorthand for [Rectangle]{Pt(x0, y0), [Pt](x1, y1)}. The returned
273 // rectangle has minimum and maximum coordinates swapped if necessary so that
274 // it is well-formed.
275 func Rect(x0, y0, x1, y1 int) Rectangle {
282 return Rectangle{Point{x0, y0}, Point{x1, y1}}
285 // mul3NonNeg returns (x * y * z), unless at least one argument is negative or
286 // if the computation overflows the int type, in which case it returns -1.
287 func mul3NonNeg(x int, y int, z int) int {
288 if (x < 0) || (y < 0) || (z < 0) {
291 hi, lo := bits.Mul64(uint64(x), uint64(y))
295 hi, lo = bits.Mul64(lo, uint64(z))
300 if (a < 0) || (uint64(a) != lo) {
306 // add2NonNeg returns (x + y), unless at least one argument is negative or if
307 // the computation overflows the int type, in which case it returns -1.
308 func add2NonNeg(x int, y int) int {
309 if (x < 0) || (y < 0) {