Golang image.Rectangle.Intersect, Overlaps, Size and String functions.

package image

Golang image.Rectangle.Intersect, Overlaps, Size and String 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)

 s := image.Rect(-100, -100, 550, 550)

 // 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.
 interRect := rect.Intersect(s)
 fmt.Println(interRect)

 // Overlaps reports whether r and s have a non-empty intersection.
 fmt.Println("Did rect overlaps s ? ", rect.Overlaps(s))

 // Size returns r's width and height.
 fmt.Println(rect.Size())

 // String returns a string representation of r like "(3,4)-(6,5)".
 fmt.Println(rect.String())
 }

Output :

Min X : 0

Max X : 500

(0,0)-(500,500)

Did rect overlaps s ? true

(500,500)

(0,0)-(500,500)

References :

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

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

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

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

Advertisement