// Copyright 2015 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 fixed // import "golang.org/x/image/math/fixed"import ()// TODO: implement fmt.Formatter for %f and %g.// I returns the integer value i as an Int26_6.//// For example, passing the integer value 2 yields Int26_6(128).func ( int) Int26_6 {returnInt26_6( << 6)}// Int26_6 is a signed 26.6 fixed-point number.//// The integer part ranges from -33554432 to 33554431, inclusive. The// fractional part has 6 bits of precision.//// For example, the number one-and-a-quarter is Int26_6(1<<6 + 1<<4).typeInt26_6int32// String returns a human-readable representation of a 26.6 fixed-point number.//// For example, the number one-and-a-quarter becomes "1:16".func ( Int26_6) () string {const , = 6, 1<<6 - 1if >= 0 {returnfmt.Sprintf("%d:%02d", int32(>>), int32(&)) } = -if >= 0 {returnfmt.Sprintf("-%d:%02d", int32(>>), int32(&)) }return"-33554432:00"// The minimum value is -(1<<25).}// Floor returns the greatest integer value less than or equal to x.//// Its return type is int, not Int26_6.func ( Int26_6) () int { returnint(( + 0x00) >> 6) }// Round returns the nearest integer value to x. Ties are rounded up.//// Its return type is int, not Int26_6.func ( Int26_6) () int { returnint(( + 0x20) >> 6) }// Ceil returns the least integer value greater than or equal to x.//// Its return type is int, not Int26_6.func ( Int26_6) () int { returnint(( + 0x3f) >> 6) }// Mul returns x*y in 26.6 fixed-point arithmetic.func ( Int26_6) ( Int26_6) Int26_6 {returnInt26_6((int64()*int64() + 1<<5) >> 6)}// Int52_12 is a signed 52.12 fixed-point number.//// The integer part ranges from -2251799813685248 to 2251799813685247,// inclusive. The fractional part has 12 bits of precision.//// For example, the number one-and-a-quarter is Int52_12(1<<12 + 1<<10).typeInt52_12int64// String returns a human-readable representation of a 52.12 fixed-point// number.//// For example, the number one-and-a-quarter becomes "1:1024".func ( Int52_12) () string {const , = 12, 1<<12 - 1if >= 0 {returnfmt.Sprintf("%d:%04d", int64(>>), int64(&)) } = -if >= 0 {returnfmt.Sprintf("-%d:%04d", int64(>>), int64(&)) }return"-2251799813685248:0000"// The minimum value is -(1<<51).}// Floor returns the greatest integer value less than or equal to x.//// Its return type is int, not Int52_12.func ( Int52_12) () int { returnint(( + 0x000) >> 12) }// Round returns the nearest integer value to x. Ties are rounded up.//// Its return type is int, not Int52_12.func ( Int52_12) () int { returnint(( + 0x800) >> 12) }// Ceil returns the least integer value greater than or equal to x.//// Its return type is int, not Int52_12.func ( Int52_12) () int { returnint(( + 0xfff) >> 12) }// Mul returns x*y in 52.12 fixed-point arithmetic.func ( Int52_12) ( Int52_12) Int52_12 {const , = 52, 12 , := muli64(int64(), int64()) := Int52_12(<< | >>) += Int52_12(( >> ( - 1)) & 1) // Round to nearest, instead of rounding down.return}// muli64 multiplies two int64 values, returning the 128-bit signed integer// result as two uint64 values.//// This implementation is similar to $GOROOT/src/runtime/softfloat64.go's mullu// function, which is in turn adapted from Hacker's Delight.func muli64(, int64) (, uint64) {const ( = 32 = 1<< - 1 ) := uint64( >> ) := uint64( & ) := uint64( >> ) := uint64( & ) := * := * + >> := & := uint64(int64() >> ) += * returnuint64() * uint64(), * + + uint64(int64()>>)}// P returns the integer values x and y as a Point26_6.//// For example, passing the integer values (2, -3) yields Point26_6{128, -192}.func (, int) Point26_6 {returnPoint26_6{Int26_6( << 6), Int26_6( << 6)}}// Point26_6 is a 26.6 fixed-point coordinate pair.//// It is analogous to the image.Point type in the standard library.typePoint26_6struct { X, Y Int26_6}// Add returns the vector p+q.func ( Point26_6) ( Point26_6) Point26_6 {returnPoint26_6{.X + .X, .Y + .Y}}// Sub returns the vector p-q.func ( Point26_6) ( Point26_6) Point26_6 {returnPoint26_6{.X - .X, .Y - .Y}}// Mul returns the vector p*k.func ( Point26_6) ( Int26_6) Point26_6 {returnPoint26_6{.X * / 64, .Y * / 64}}// Div returns the vector p/k.func ( Point26_6) ( Int26_6) Point26_6 {returnPoint26_6{.X * 64 / , .Y * 64 / }}// In returns whether p is in r.func ( Point26_6) ( Rectangle26_6) bool {return .Min.X <= .X && .X < .Max.X && .Min.Y <= .Y && .Y < .Max.Y}// Point52_12 is a 52.12 fixed-point coordinate pair.//// It is analogous to the image.Point type in the standard library.typePoint52_12struct { X, Y Int52_12}// Add returns the vector p+q.func ( Point52_12) ( Point52_12) Point52_12 {returnPoint52_12{.X + .X, .Y + .Y}}// Sub returns the vector p-q.func ( Point52_12) ( Point52_12) Point52_12 {returnPoint52_12{.X - .X, .Y - .Y}}// Mul returns the vector p*k.func ( Point52_12) ( Int52_12) Point52_12 {returnPoint52_12{.X * / 4096, .Y * / 4096}}// Div returns the vector p/k.func ( Point52_12) ( Int52_12) Point52_12 {returnPoint52_12{.X * 4096 / , .Y * 4096 / }}// In returns whether p is in r.func ( Point52_12) ( Rectangle52_12) bool {return .Min.X <= .X && .X < .Max.X && .Min.Y <= .Y && .Y < .Max.Y}// 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 (, , , int) Rectangle26_6 {if > { , = , }if > { , = , }returnRectangle26_6{Point26_6{Int26_6( << 6),Int26_6( << 6), },Point26_6{Int26_6( << 6),Int26_6( << 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.typeRectangle26_6struct { Min, Max Point26_6}// Add returns the rectangle r translated by p.func ( Rectangle26_6) ( Point26_6) Rectangle26_6 {returnRectangle26_6{Point26_6{.Min.X + .X, .Min.Y + .Y},Point26_6{.Max.X + .X, .Max.Y + .Y}, }}// Sub returns the rectangle r translated by -p.func ( Rectangle26_6) ( Point26_6) Rectangle26_6 {returnRectangle26_6{Point26_6{.Min.X - .X, .Min.Y - .Y},Point26_6{.Max.X - .X, .Max.Y - .Y}, }}// 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 ( Rectangle26_6) ( Rectangle26_6) Rectangle26_6 {if .Min.X < .Min.X { .Min.X = .Min.X }if .Min.Y < .Min.Y { .Min.Y = .Min.Y }if .Max.X > .Max.X { .Max.X = .Max.X }if .Max.Y > .Max.Y { .Max.Y = .Max.Y }// 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 .Empty() {returnRectangle26_6{} }return}// Union returns the smallest rectangle that contains both r and s.func ( Rectangle26_6) ( Rectangle26_6) Rectangle26_6 {if .Empty() {return }if .Empty() {return }if .Min.X > .Min.X { .Min.X = .Min.X }if .Min.Y > .Min.Y { .Min.Y = .Min.Y }if .Max.X < .Max.X { .Max.X = .Max.X }if .Max.Y < .Max.Y { .Max.Y = .Max.Y }return}// Empty returns whether the rectangle contains no points.func ( Rectangle26_6) () bool {return .Min.X >= .Max.X || .Min.Y >= .Max.Y}// In returns whether every point in r is in s.func ( Rectangle26_6) ( Rectangle26_6) bool {if .Empty() {returntrue }// Note that r.Max is an exclusive bound for r, so that r.In(s) // does not require that r.Max.In(s).return .Min.X <= .Min.X && .Max.X <= .Max.X && .Min.Y <= .Min.Y && .Max.Y <= .Max.Y}// 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.typeRectangle52_12struct { Min, Max Point52_12}// Add returns the rectangle r translated by p.func ( Rectangle52_12) ( Point52_12) Rectangle52_12 {returnRectangle52_12{Point52_12{.Min.X + .X, .Min.Y + .Y},Point52_12{.Max.X + .X, .Max.Y + .Y}, }}// Sub returns the rectangle r translated by -p.func ( Rectangle52_12) ( Point52_12) Rectangle52_12 {returnRectangle52_12{Point52_12{.Min.X - .X, .Min.Y - .Y},Point52_12{.Max.X - .X, .Max.Y - .Y}, }}// 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 ( Rectangle52_12) ( Rectangle52_12) Rectangle52_12 {if .Min.X < .Min.X { .Min.X = .Min.X }if .Min.Y < .Min.Y { .Min.Y = .Min.Y }if .Max.X > .Max.X { .Max.X = .Max.X }if .Max.Y > .Max.Y { .Max.Y = .Max.Y }// 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 .Empty() {returnRectangle52_12{} }return}// Union returns the smallest rectangle that contains both r and s.func ( Rectangle52_12) ( Rectangle52_12) Rectangle52_12 {if .Empty() {return }if .Empty() {return }if .Min.X > .Min.X { .Min.X = .Min.X }if .Min.Y > .Min.Y { .Min.Y = .Min.Y }if .Max.X < .Max.X { .Max.X = .Max.X }if .Max.Y < .Max.Y { .Max.Y = .Max.Y }return}// Empty returns whether the rectangle contains no points.func ( Rectangle52_12) () bool {return .Min.X >= .Max.X || .Min.Y >= .Max.Y}// In returns whether every point in r is in s.func ( Rectangle52_12) ( Rectangle52_12) bool {if .Empty() {returntrue }// Note that r.Max is an exclusive bound for r, so that r.In(s) // does not require that r.Max.In(s).return .Min.X <= .Min.X && .Max.X <= .Max.X && .Min.Y <= .Min.Y && .Max.Y <= .Max.Y}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.