From 8ab1ac6834edd43d91cbe24272897a87ce7e835e Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Mon, 24 Aug 2015 10:24:01 +1000 Subject: [PATCH] math/fixed: add rectangle types and the R function. Change-Id: Ibb3785bcd57f13c7b6f1dc37526f65e44b4ac695 Reviewed-on: https://go-review.googlesource.com/13823 Reviewed-by: Rob Pike --- math/fixed/fixed.go | 53 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/math/fixed/fixed.go b/math/fixed/fixed.go index 60f83c8..2f25d0e 100644 --- a/math/fixed/fixed.go +++ b/math/fixed/fixed.go @@ -13,7 +13,7 @@ import ( // I returns the integer value i as an Int26_6. // -// For example, the integer value 2 is the Int26_6 128. +// For example, passing the integer value 2 yields Int26_6(128). func I(i int) Int26_6 { return Int26_6(i << 6) } @@ -67,12 +67,14 @@ func (x Int52_12) String() string { // P returns the integer values x and y as a Point26_6. // -// For example, the integer value pair (2, -3) is the Point26_6 (128, -192). +// For example, passing the integer values (2, -3) yields Point26_6{128, -192}. func P(x, y int) Point26_6 { return Point26_6{Int26_6(x << 6), Int26_6(y << 6)} } // Point26_6 is a 26.6 fixed-point coordinate pair. +// +// It is analogous to the image.Point type in the standard library. type Point26_6 struct { X, Y Int26_6 } @@ -98,6 +100,8 @@ func (p Point26_6) Div(k Int26_6) Point26_6 { } // Point52_12 is a 52.12 fixed-point coordinate pair. +// +// It is analogous to the image.Point type in the standard library. type Point52_12 struct { X, Y Int52_12 } @@ -121,3 +125,48 @@ func (p Point52_12) Mul(k Int52_12) Point52_12 { func (p Point52_12) Div(k Int52_12) Point52_12 { return Point52_12{p.X * 4096 / k, p.Y * 4096 / k} } + +// R returns the integer values minX, minY, maxX, maxY as a Rectangle26_6. +// +// For example, passing the integer values (0, 1, 2, 3) yields +// Rectangle26_6{Point26_6{0, 64}, Point26_6{128, 192}}. +// +// Like the image.Rect function in the standard library, the returned rectangle +// has minimum and maximum coordinates swapped if necessary so that it is +// well-formed. +func R(minX, minY, maxX, maxY int) Rectangle26_6 { + if minX > maxX { + minX, maxX = maxX, minX + } + if minY > maxY { + minY, maxY = maxY, minY + } + return Rectangle26_6{ + Point26_6{ + Int26_6(minX << 6), + Int26_6(minY << 6), + }, + Point26_6{ + Int26_6(maxX << 6), + Int26_6(maxY << 6), + }, + } +} + +// Rectangle26_6 is a 26.6 fixed-point coordinate rectangle. The Min bound is +// inclusive and the Max bound is exclusive. It is well-formed if Min.X <= +// Max.X and likewise for Y. +// +// It is analogous to the image.Rectangle type in the standard library. +type Rectangle26_6 struct { + Min, Max Point26_6 +} + +// Rectangle52_12 is a 52.12 fixed-point coordinate rectangle. The Min bound is +// inclusive and the Max bound is exclusive. It is well-formed if Min.X <= +// Max.X and likewise for Y. +// +// It is analogous to the image.Rectangle type in the standard library. +type Rectangle52_12 struct { + Min, Max Point52_12 +}