Golang image.Rectangle.Sub and Union functions example.

package image

Golang image.Rectangle.Sub and Union 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)

 p := image.Pt(20, 100)

 // Sub returns the rectangle r translated by -p
 subRect := rect.Sub(p)

 fmt.Println(subRect)

 // Union returns the smallest rectangle that contains both r and s.
 unionRect := rect.Union(subRect)

 fmt.Println(unionRect)
 }

Output :

Min X : 0

Max X : 500

(-20,-100)-(480,400)

(-20,-100)-(500,500)

References :

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

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

Advertisement