github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/image/math/fixed/fixed.go (about) 1 // Copyright 2015 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. 4 5 // Package fixed implements fixed-point integer types. 6 package fixed // import "golang.org/x/image/math/fixed" 7 8 import ( 9 "fmt" 10 ) 11 12 // TODO: implement fmt.Formatter for %f and %g. 13 14 // I returns the integer value i as an Int26_6. 15 // 16 // For example, passing the integer value 2 yields Int26_6(128). 17 func I(i int) Int26_6 { 18 return Int26_6(i << 6) 19 } 20 21 // Int26_6 is a signed 26.6 fixed-point number. 22 // 23 // The integer part ranges from -33554432 to 33554431, inclusive. The 24 // fractional part has 6 bits of precision. 25 // 26 // For example, the number one-and-a-quarter is Int26_6(1<<6 + 1<<4). 27 type Int26_6 int32 28 29 // String returns a human-readable representation of a 26.6 fixed-point number. 30 // 31 // For example, the number one-and-a-quarter becomes "1:16". 32 func (x Int26_6) String() string { 33 const shift, mask = 6, 1<<6 - 1 34 if x >= 0 { 35 return fmt.Sprintf("%d:%02d", int32(x>>shift), int32(x&mask)) 36 } 37 x = -x 38 if x >= 0 { 39 return fmt.Sprintf("-%d:%02d", int32(x>>shift), int32(x&mask)) 40 } 41 return "-33554432:00" // The minimum value is -(1<<25). 42 } 43 44 // Int52_12 is a signed 52.12 fixed-point number. 45 // 46 // The integer part ranges from -2251799813685248 to 2251799813685247, 47 // inclusive. The fractional part has 12 bits of precision. 48 // 49 // For example, the number one-and-a-quarter is Int52_12(1<<12 + 1<<10). 50 type Int52_12 int64 51 52 // String returns a human-readable representation of a 52.12 fixed-point 53 // number. 54 // 55 // For example, the number one-and-a-quarter becomes "1:1024". 56 func (x Int52_12) String() string { 57 const shift, mask = 12, 1<<12 - 1 58 if x >= 0 { 59 return fmt.Sprintf("%d:%04d", int64(x>>shift), int64(x&mask)) 60 } 61 x = -x 62 if x >= 0 { 63 return fmt.Sprintf("-%d:%04d", int64(x>>shift), int64(x&mask)) 64 } 65 return "-2251799813685248:0000" // The minimum value is -(1<<51). 66 } 67 68 // P returns the integer values x and y as a Point26_6. 69 // 70 // For example, passing the integer values (2, -3) yields Point26_6{128, -192}. 71 func P(x, y int) Point26_6 { 72 return Point26_6{Int26_6(x << 6), Int26_6(y << 6)} 73 } 74 75 // Point26_6 is a 26.6 fixed-point coordinate pair. 76 // 77 // It is analogous to the image.Point type in the standard library. 78 type Point26_6 struct { 79 X, Y Int26_6 80 } 81 82 // Add returns the vector p+q. 83 func (p Point26_6) Add(q Point26_6) Point26_6 { 84 return Point26_6{p.X + q.X, p.Y + q.Y} 85 } 86 87 // Sub returns the vector p-q. 88 func (p Point26_6) Sub(q Point26_6) Point26_6 { 89 return Point26_6{p.X - q.X, p.Y - q.Y} 90 } 91 92 // Mul returns the vector p*k. 93 func (p Point26_6) Mul(k Int26_6) Point26_6 { 94 return Point26_6{p.X * k / 64, p.Y * k / 64} 95 } 96 97 // Div returns the vector p/k. 98 func (p Point26_6) Div(k Int26_6) Point26_6 { 99 return Point26_6{p.X * 64 / k, p.Y * 64 / k} 100 } 101 102 // Point52_12 is a 52.12 fixed-point coordinate pair. 103 // 104 // It is analogous to the image.Point type in the standard library. 105 type Point52_12 struct { 106 X, Y Int52_12 107 } 108 109 // Add returns the vector p+q. 110 func (p Point52_12) Add(q Point52_12) Point52_12 { 111 return Point52_12{p.X + q.X, p.Y + q.Y} 112 } 113 114 // Sub returns the vector p-q. 115 func (p Point52_12) Sub(q Point52_12) Point52_12 { 116 return Point52_12{p.X - q.X, p.Y - q.Y} 117 } 118 119 // Mul returns the vector p*k. 120 func (p Point52_12) Mul(k Int52_12) Point52_12 { 121 return Point52_12{p.X * k / 4096, p.Y * k / 4096} 122 } 123 124 // Div returns the vector p/k. 125 func (p Point52_12) Div(k Int52_12) Point52_12 { 126 return Point52_12{p.X * 4096 / k, p.Y * 4096 / k} 127 } 128 129 // R returns the integer values minX, minY, maxX, maxY as a Rectangle26_6. 130 // 131 // For example, passing the integer values (0, 1, 2, 3) yields 132 // Rectangle26_6{Point26_6{0, 64}, Point26_6{128, 192}}. 133 // 134 // Like the image.Rect function in the standard library, the returned rectangle 135 // has minimum and maximum coordinates swapped if necessary so that it is 136 // well-formed. 137 func R(minX, minY, maxX, maxY int) Rectangle26_6 { 138 if minX > maxX { 139 minX, maxX = maxX, minX 140 } 141 if minY > maxY { 142 minY, maxY = maxY, minY 143 } 144 return Rectangle26_6{ 145 Point26_6{ 146 Int26_6(minX << 6), 147 Int26_6(minY << 6), 148 }, 149 Point26_6{ 150 Int26_6(maxX << 6), 151 Int26_6(maxY << 6), 152 }, 153 } 154 } 155 156 // Rectangle26_6 is a 26.6 fixed-point coordinate rectangle. The Min bound is 157 // inclusive and the Max bound is exclusive. It is well-formed if Min.X <= 158 // Max.X and likewise for Y. 159 // 160 // It is analogous to the image.Rectangle type in the standard library. 161 type Rectangle26_6 struct { 162 Min, Max Point26_6 163 } 164 165 // Rectangle52_12 is a 52.12 fixed-point coordinate rectangle. The Min bound is 166 // inclusive and the Max bound is exclusive. It is well-formed if Min.X <= 167 // Max.X and likewise for Y. 168 // 169 // It is analogous to the image.Rectangle type in the standard library. 170 type Rectangle52_12 struct { 171 Min, Max Point52_12 172 }