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.
11 // A Point is an X, Y coordinate pair. The axes increase right and down.
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
) + ")"
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
}
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
}
31 // Mul returns the vector p*k.
32 func (p Point
) Mul(k
int) Point
{
33 return Point
{p
.X
* k
, p
.Y
* k
}
36 // Div returns the vector p/k.
37 func (p Point
) Div(k
int) Point
{
38 return Point
{p
.X
/ k
, p
.Y
/ k
}
41 // In returns whether p is in r.
42 func (p Point
) In(r Rectangle
) bool {
43 return r
.Min
.X
<= p
.X
&& p
.X
< r
.Max
.X
&&
44 r
.Min
.Y
<= p
.Y
&& p
.Y
< r
.Max
.Y
47 // Mod returns the point q in r such that p.X-q.X is a multiple of r's width
48 // and p.Y-q.Y is a multiple of r's height.
49 func (p Point
) Mod(r Rectangle
) Point
{
50 w
, h
:= r
.Dx(), r
.Dy()
63 // Eq returns whether p and q are equal.
64 func (p Point
) Eq(q Point
) bool {
65 return p
.X
== q
.X
&& p
.Y
== q
.Y
68 // ZP is the zero Point.
71 // Pt is shorthand for Point{X, Y}.
72 func Pt(X
, Y
int) Point
{
76 // A Rectangle contains the points with Min.X <= X < Max.X, Min.Y <= Y < Max.Y.
77 // It is well-formed if Min.X <= Max.X and likewise for Y. Points are always
78 // well-formed. A rectangle's methods always return well-formed outputs for
79 // well-formed inputs.
80 type Rectangle
struct {
84 // String returns a string representation of r like "(3,4)-(6,5)".
85 func (r Rectangle
) String() string {
86 return r
.Min
.String() + "-" + r
.Max
.String()
89 // Dx returns r's width.
90 func (r Rectangle
) Dx() int {
91 return r
.Max
.X
- r
.Min
.X
94 // Dy returns r's height.
95 func (r Rectangle
) Dy() int {
96 return r
.Max
.Y
- r
.Min
.Y
99 // Size returns r's width and height.
100 func (r Rectangle
) Size() Point
{
107 // Add returns the rectangle r translated by p.
108 func (r Rectangle
) Add(p Point
) Rectangle
{
110 Point
{r
.Min
.X
+ p
.X
, r
.Min
.Y
+ p
.Y
},
111 Point
{r
.Max
.X
+ p
.X
, r
.Max
.Y
+ p
.Y
},
115 // Sub returns the rectangle r translated by -p.
116 func (r Rectangle
) Sub(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 // Inset returns the rectangle r inset by n, which may be negative. If either
124 // of r's dimensions is less than 2*n then an empty rectangle near the center
125 // of r will be returned.
126 func (r Rectangle
) Inset(n
int) Rectangle
{
128 r
.Min
.X
= (r
.Min
.X
+ r
.Max
.X
) / 2
135 r
.Min
.Y
= (r
.Min
.Y
+ r
.Max
.Y
) / 2
144 // Intersect returns the largest rectangle contained by both r and s. If the
145 // two rectangles do not overlap then the zero rectangle will be returned.
146 func (r Rectangle
) Intersect(s Rectangle
) Rectangle
{
147 if r
.Min
.X
< s
.Min
.X
{
150 if r
.Min
.Y
< s
.Min
.Y
{
153 if r
.Max
.X
> s
.Max
.X
{
156 if r
.Max
.Y
> s
.Max
.Y
{
159 if r
.Min
.X
> r
.Max
.X || r
.Min
.Y
> r
.Max
.Y
{
165 // Union returns the smallest rectangle that contains both r and s.
166 func (r Rectangle
) Union(s Rectangle
) Rectangle
{
167 if r
.Min
.X
> s
.Min
.X
{
170 if r
.Min
.Y
> s
.Min
.Y
{
173 if r
.Max
.X
< s
.Max
.X
{
176 if r
.Max
.Y
< s
.Max
.Y
{
182 // Empty returns whether the rectangle contains no points.
183 func (r Rectangle
) Empty() bool {
184 return r
.Min
.X
>= r
.Max
.X || r
.Min
.Y
>= r
.Max
.Y
187 // Eq returns whether r and s are equal.
188 func (r Rectangle
) Eq(s Rectangle
) bool {
189 return r
.Min
.X
== s
.Min
.X
&& r
.Min
.Y
== s
.Min
.Y
&&
190 r
.Max
.X
== s
.Max
.X
&& r
.Max
.Y
== s
.Max
.Y
193 // Overlaps returns whether r and s have a non-empty intersection.
194 func (r Rectangle
) Overlaps(s Rectangle
) bool {
195 return r
.Min
.X
< s
.Max
.X
&& s
.Min
.X
< r
.Max
.X
&&
196 r
.Min
.Y
< s
.Max
.Y
&& s
.Min
.Y
< r
.Max
.Y
199 // In returns whether every point in r is in s.
200 func (r Rectangle
) In(s Rectangle
) bool {
204 // Note that r.Max is an exclusive bound for r, so that r.In(s)
205 // does not require that r.Max.In(s).
206 return s
.Min
.X
<= r
.Min
.X
&& r
.Max
.X
<= s
.Max
.X
&&
207 s
.Min
.Y
<= r
.Min
.Y
&& r
.Max
.Y
<= s
.Max
.Y
210 // Canon returns the canonical version of r. The returned rectangle has minimum
211 // and maximum coordinates swapped if necessary so that it is well-formed.
212 func (r Rectangle
) Canon() Rectangle
{
213 if r
.Max
.X
< r
.Min
.X
{
214 r
.Min
.X
, r
.Max
.X
= r
.Max
.X
, r
.Min
.X
216 if r
.Max
.Y
< r
.Min
.Y
{
217 r
.Min
.Y
, r
.Max
.Y
= r
.Max
.Y
, r
.Min
.Y
222 // ZR is the zero Rectangle.
225 // Rect is shorthand for Rectangle{Pt(x0, y0), Pt(x1, y1)}.
226 func Rect(x0
, y0
, x1
, y1
int) Rectangle
{
233 return Rectangle
{Point
{x0
, y0
}, Point
{x1
, y1
}}