Golang image.Rectangle.Empty, Eq, In and Inset functions.

package image

Golang image.Rectangle.Empty, Eq, In and Inset functions usage example.

 package main

 import (
 "fmt"
 "image"
 "runtime"
 )

 func main() {

 // maximize CPU usage for maximum performance
 runtime.GOMAXPROCS(runtime.NumCPU())

 // define a rectangle
 rect := image.Rect(0, 0, 500, 500)
 fmt.Println("Min X : ", rect.Min.X)
 fmt.Println("Max X : ", rect.Max.X)

 // Empty reports whether the rectangle contains no points.
 isempty := rect.Empty()
 fmt.Println("Is rect empty ? ", isempty) // should be false

 var ZR image.Rectangle
 isempty = ZR.Empty()
 fmt.Println("Is ZR empty ? ", isempty) // should be true

 // Eq reports whether r and s are equal.
 equal := ZR.Eq(rect)
 fmt.Println("Is ZR equals to rect ? ", equal) // should be false

 //In reports whether every point in r is in s.
 s := image.Rect(-100, -100, 600, 600)
 inside := rect.In(s)

 fmt.Println("Is rect inside s ? ", inside) // should be true

 // Inset returns the rectangle r inset by n, which may be negative.
 // If either of r's dimensions is less than 2*n then an empty rectangle near the center of r will be returned.

 fmt.Println("Inset by 1 : ", s.Inset(1))
 fmt.Println("Inset by 20 : ", s.Inset(20)) // see the diff ?

 }

Output :

Min X : 0

Max X : 500

Is rect empty ? false

Is ZR empty ? true

Is ZR equals to rect ? false

Is rect inside s ? true

Inset by 1 : (-99,-99)-(599,599)

Inset by 20 : (-80,-80)-(580,580)

References :

http://golang.org/pkg/image/#Rectangle.Empty

http://golang.org/pkg/image/#Rectangle.Eq

http://golang.org/pkg/image/#Rectangle.In

http://golang.org/pkg/image/#Rectangle.Inset

Advertisement