2010-08-09 20:08:52 -06:00
|
|
|
// 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 (
|
2015-03-02 18:59:23 -07:00
|
|
|
"image/color"
|
2010-08-09 20:08:52 -06:00
|
|
|
"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}
|
|
|
|
}
|
|
|
|
|
2010-09-22 18:58:59 -06:00
|
|
|
// Mul returns the vector p*k.
|
|
|
|
func (p Point) Mul(k int) Point {
|
|
|
|
return Point{p.X * k, p.Y * k}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Div returns the vector p/k.
|
|
|
|
func (p Point) Div(k int) Point {
|
|
|
|
return Point{p.X / k, p.Y / k}
|
|
|
|
}
|
|
|
|
|
2013-07-22 19:59:49 -06:00
|
|
|
// In reports whether p is in r.
|
2011-06-02 21:18:15 -06:00
|
|
|
func (p Point) In(r Rectangle) bool {
|
|
|
|
return r.Min.X <= p.X && p.X < r.Max.X &&
|
|
|
|
r.Min.Y <= p.Y && p.Y < r.Max.Y
|
|
|
|
}
|
|
|
|
|
2010-10-11 20:44:11 -06:00
|
|
|
// Mod returns the point q in r such that p.X-q.X is a multiple of r's width
|
|
|
|
// and p.Y-q.Y is a multiple of r's height.
|
|
|
|
func (p Point) Mod(r Rectangle) Point {
|
|
|
|
w, h := r.Dx(), r.Dy()
|
|
|
|
p = p.Sub(r.Min)
|
2010-10-12 19:05:21 -06:00
|
|
|
p.X = p.X % w
|
|
|
|
if p.X < 0 {
|
|
|
|
p.X += w
|
2010-10-11 20:44:11 -06:00
|
|
|
}
|
2010-10-12 19:05:21 -06:00
|
|
|
p.Y = p.Y % h
|
|
|
|
if p.Y < 0 {
|
|
|
|
p.Y += h
|
2010-10-11 20:44:11 -06:00
|
|
|
}
|
|
|
|
return p.Add(r.Min)
|
|
|
|
}
|
|
|
|
|
2013-07-22 19:59:49 -06:00
|
|
|
// Eq reports whether p and q are equal.
|
2010-09-22 18:58:59 -06:00
|
|
|
func (p Point) Eq(q Point) bool {
|
2015-02-16 22:28:10 -07:00
|
|
|
return p == q
|
2010-09-22 18:58:59 -06:00
|
|
|
}
|
|
|
|
|
2010-08-09 20:08:52 -06:00
|
|
|
// 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.
|
2010-09-09 03:12:54 -06:00
|
|
|
// It is well-formed if Min.X <= Max.X and likewise for Y. Points are always
|
|
|
|
// well-formed. A rectangle's methods always return well-formed outputs for
|
|
|
|
// well-formed inputs.
|
2015-03-02 18:59:23 -07:00
|
|
|
//
|
|
|
|
// A Rectangle is also an Image whose bounds are the rectangle itself. At
|
|
|
|
// returns color.Opaque for points in the rectangle and color.Transparent
|
|
|
|
// otherwise.
|
2010-08-09 20:08:52 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2010-09-22 18:58:59 -06:00
|
|
|
// Size returns r's width and height.
|
|
|
|
func (r Rectangle) Size() Point {
|
|
|
|
return Point{
|
|
|
|
r.Max.X - r.Min.X,
|
|
|
|
r.Max.Y - r.Min.Y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-09 20:08:52 -06:00
|
|
|
// 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},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 16:55:03 -07:00
|
|
|
// Sub returns the rectangle r translated by -p.
|
2010-08-09 20:08:52 -06:00
|
|
|
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},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-09 03:12:54 -06:00
|
|
|
// Inset returns the rectangle r inset by n, which may be negative. If either
|
|
|
|
// of r's dimensions is less than 2*n then an empty rectangle near the center
|
|
|
|
// of r will be returned.
|
2010-08-09 20:08:52 -06:00
|
|
|
func (r Rectangle) Inset(n int) Rectangle {
|
2010-09-09 03:12:54 -06:00
|
|
|
if r.Dx() < 2*n {
|
|
|
|
r.Min.X = (r.Min.X + r.Max.X) / 2
|
|
|
|
r.Max.X = r.Min.X
|
|
|
|
} else {
|
|
|
|
r.Min.X += n
|
|
|
|
r.Max.X -= n
|
|
|
|
}
|
|
|
|
if r.Dy() < 2*n {
|
|
|
|
r.Min.Y = (r.Min.Y + r.Max.Y) / 2
|
|
|
|
r.Max.Y = r.Min.Y
|
|
|
|
} else {
|
|
|
|
r.Min.Y += n
|
|
|
|
r.Max.Y -= n
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intersect returns the largest rectangle contained by both r and s. If the
|
|
|
|
// two rectangles do not overlap then the zero rectangle will be returned.
|
|
|
|
func (r Rectangle) Intersect(s Rectangle) Rectangle {
|
|
|
|
if r.Min.X < s.Min.X {
|
|
|
|
r.Min.X = s.Min.X
|
|
|
|
}
|
|
|
|
if r.Min.Y < s.Min.Y {
|
|
|
|
r.Min.Y = s.Min.Y
|
|
|
|
}
|
|
|
|
if r.Max.X > s.Max.X {
|
|
|
|
r.Max.X = s.Max.X
|
|
|
|
}
|
|
|
|
if r.Max.Y > s.Max.Y {
|
|
|
|
r.Max.Y = s.Max.Y
|
2010-08-09 20:08:52 -06:00
|
|
|
}
|
2017-02-09 20:40:38 -07:00
|
|
|
// Letting r0 and s0 be the values of r and s at the time that the method
|
|
|
|
// is called, this next line is equivalent to:
|
|
|
|
//
|
|
|
|
// if max(r0.Min.X, s0.Min.X) >= min(r0.Max.X, s0.Max.X) || likewiseForY { etc }
|
|
|
|
if r.Empty() {
|
2010-09-09 03:12:54 -06:00
|
|
|
return ZR
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Union returns the smallest rectangle that contains both r and s.
|
|
|
|
func (r Rectangle) Union(s Rectangle) Rectangle {
|
2015-02-16 17:15:06 -07:00
|
|
|
if r.Empty() {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
if s.Empty() {
|
|
|
|
return r
|
|
|
|
}
|
2010-09-09 03:12:54 -06:00
|
|
|
if r.Min.X > s.Min.X {
|
|
|
|
r.Min.X = s.Min.X
|
|
|
|
}
|
|
|
|
if r.Min.Y > s.Min.Y {
|
|
|
|
r.Min.Y = s.Min.Y
|
|
|
|
}
|
|
|
|
if r.Max.X < s.Max.X {
|
|
|
|
r.Max.X = s.Max.X
|
|
|
|
}
|
|
|
|
if r.Max.Y < s.Max.Y {
|
|
|
|
r.Max.Y = s.Max.Y
|
|
|
|
}
|
|
|
|
return r
|
2010-08-09 20:08:52 -06:00
|
|
|
}
|
|
|
|
|
2013-07-22 19:59:49 -06:00
|
|
|
// Empty reports whether the rectangle contains no points.
|
2010-08-09 20:08:52 -06:00
|
|
|
func (r Rectangle) Empty() bool {
|
|
|
|
return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
|
|
|
|
}
|
|
|
|
|
2015-02-16 22:28:10 -07:00
|
|
|
// Eq reports whether r and s contain the same set of points. All empty
|
|
|
|
// rectangles are considered equal.
|
2010-08-09 20:08:52 -06:00
|
|
|
func (r Rectangle) Eq(s Rectangle) bool {
|
2015-02-16 22:28:10 -07:00
|
|
|
return r == s || r.Empty() && s.Empty()
|
2010-08-09 20:08:52 -06:00
|
|
|
}
|
|
|
|
|
2013-07-22 19:59:49 -06:00
|
|
|
// Overlaps reports whether r and s have a non-empty intersection.
|
2010-08-09 20:08:52 -06:00
|
|
|
func (r Rectangle) Overlaps(s Rectangle) bool {
|
2015-02-16 17:15:06 -07:00
|
|
|
return !r.Empty() && !s.Empty() &&
|
|
|
|
r.Min.X < s.Max.X && s.Min.X < r.Max.X &&
|
2010-08-09 20:08:52 -06:00
|
|
|
r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y
|
|
|
|
}
|
|
|
|
|
2013-07-22 19:59:49 -06:00
|
|
|
// In reports whether every point in r is in s.
|
2011-06-02 21:18:15 -06:00
|
|
|
func (r Rectangle) In(s Rectangle) bool {
|
|
|
|
if r.Empty() {
|
2011-06-02 19:43:54 -06:00
|
|
|
return true
|
|
|
|
}
|
2011-06-02 21:18:15 -06:00
|
|
|
// Note that r.Max is an exclusive bound for r, so that r.In(s)
|
|
|
|
// does not require that r.Max.In(s).
|
|
|
|
return s.Min.X <= r.Min.X && r.Max.X <= s.Max.X &&
|
|
|
|
s.Min.Y <= r.Min.Y && r.Max.Y <= s.Max.Y
|
2010-08-10 00:34:57 -06:00
|
|
|
}
|
|
|
|
|
2010-09-09 03:12:54 -06:00
|
|
|
// Canon returns the canonical version of r. The returned rectangle has minimum
|
|
|
|
// and maximum coordinates swapped if necessary so that it is well-formed.
|
2010-08-09 20:08:52 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:59:23 -07:00
|
|
|
// At implements the Image interface.
|
|
|
|
func (r Rectangle) At(x, y int) color.Color {
|
|
|
|
if (Point{x, y}).In(r) {
|
|
|
|
return color.Opaque
|
|
|
|
}
|
|
|
|
return color.Transparent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bounds implements the Image interface.
|
|
|
|
func (r Rectangle) Bounds() Rectangle {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// ColorModel implements the Image interface.
|
|
|
|
func (r Rectangle) ColorModel() color.Model {
|
|
|
|
return color.Alpha16Model
|
|
|
|
}
|
|
|
|
|
2010-08-09 20:08:52 -06:00
|
|
|
// ZR is the zero Rectangle.
|
|
|
|
var ZR Rectangle
|
|
|
|
|
2015-02-16 22:28:10 -07:00
|
|
|
// Rect is shorthand for Rectangle{Pt(x0, y0), Pt(x1, y1)}. The returned
|
|
|
|
// rectangle has minimum and maximum coordinates swapped if necessary so that
|
|
|
|
// it is well-formed.
|
2010-08-09 20:08:52 -06:00
|
|
|
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}}
|
|
|
|
}
|