git.lucas.co / go_mono
git clone https://git.lucas.co/go_mono.git

commitd9c0088098f91b3d6e080f260217cdddc270052e
parentb137518d47
authorNigel Tao <[email protected]>
date2015-08-17 11:44
math/fixed: add I and P helper functions.

The P function is especially common in e.g. Freetype code that works
with both the integer-pixel (X,Y) coordinates used by the stdlib image
package and sub-pixel (X,Y) coordinates used by its raster package.

Change-Id: I68be8ca71ceb24f40277ecc39a0896323f6671f5
Reviewed-on: https://go-review.googlesource.com/13652
Reviewed-by: Rob Pike <[email protected]>

 math/fixed/fixed.go | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/math/fixed/fixed.go b/math/fixed/fixed.go
index 8e78e6c..60f83c8 100644
--- a/math/fixed/fixed.go
+++ b/math/fixed/fixed.go
@@ -11,6 +11,13 @@ import (
 
 // TODO: implement fmt.Formatter for %f and %g.
 
+// I returns the integer value i as an Int26_6.
+//
+// For example, the integer value 2 is the Int26_6 128.
+func I(i int) Int26_6 {
+	return Int26_6(i << 6)
+}
+
 // Int26_6 is a signed 26.6 fixed-point number.
 //
 // The integer part ranges from -33554432 to 33554431, inclusive. The
@@ -58,6 +65,13 @@ func (x Int52_12) String() string {
 	return "-2251799813685248:0000" // The minimum value is -(1<<51).
 }
 
+// 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).
+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.
 type Point26_6 struct {
 	X, Y Int26_6