summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRoger Peppe <rogpeppe@gmail.com>2010-05-27 17:19:47 -0700
committerRoger Peppe <rogpeppe@gmail.com>2010-05-27 17:19:47 -0700
commitba7b236fe5897c8eb604717942fc6f22856e4b6f (patch)
tree91bb8c95e9710ff6dadacbc51a7a5524fc83d690 /src
parent818876b00baa57abe242e7aceda8a4d84abd5703 (diff)
downloadgolang-ba7b236fe5897c8eb604717942fc6f22856e4b6f.tar.gz
Add Rectangle.Eq and Point.In.
Fix Rectangle.Clip. It could return a non-canonical rectangle if its arguments did not overlap. e.g. Rect(0, 0, 10, 10).Clip(Rect(0, 20, 10, 30)) -> Rect(0, 20, 10, 10) R=rsc, r CC=golang-dev, golang-dev http://codereview.appspot.com/1290041 Committer: Rob Pike <r@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/pkg/exp/draw/arith.go14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/pkg/exp/draw/arith.go b/src/pkg/exp/draw/arith.go
index 2b9033b8c..b72242aaa 100644
--- a/src/pkg/exp/draw/arith.go
+++ b/src/pkg/exp/draw/arith.go
@@ -44,6 +44,12 @@ func (p Point) Div(k int) Point { return Point{p.X / k, p.Y / k} }
// Eq returns true if p and q are equal.
func (p Point) Eq(q Point) bool { return p.X == q.X && p.Y == q.Y }
+// In returns true if p is within r.
+func (p Point) In(r Rectangle) bool {
+ return p.X >= r.Min.X && p.X < r.Max.X &&
+ p.Y >= r.Min.Y && p.Y < r.Max.Y
+}
+
// Inset returns the rectangle r inset by n: Rect(r.Min.X+n, r.Min.Y+n, r.Max.X-n, r.Max.Y-n).
func (r Rectangle) Inset(n int) Rectangle {
return Rectangle{Point{r.Min.X + n, r.Min.Y + n}, Point{r.Max.X - n, r.Max.Y - n}}
@@ -119,6 +125,9 @@ func (r Rectangle) Clip(r1 Rectangle) Rectangle {
if r1.Empty() {
return r1
}
+ if !r.Overlaps(r1) {
+ return Rectangle{r.Min, r.Min}
+ }
if r.Min.X < r1.Min.X {
r.Min.X = r1.Min.X
}
@@ -139,3 +148,8 @@ func (r Rectangle) Dx() int { return r.Max.X - r.Min.X }
// Dy returns the width of the rectangle r: r.Max.Y - r.Min.Y.
func (r Rectangle) Dy() int { return r.Max.Y - r.Min.Y }
+
+// Eq returns true if r and r1 are equal.
+func (r Rectangle) Eq(r1 Rectangle) bool {
+ return r.Min.Eq(r1.Min) && r.Max.Eq(r1.Max)
+}