Golang image.Rectangle.Add, Canon, Dx and Dy functions example.

package image

Golang image.Rectangle.Add, Canon, Dx and Dy 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)

 // Add returns the rectangle r translated by p.
 pt := image.Pt(100, 100)
 newRect := rect.Add(pt)

 fmt.Println("Before Add() : ", rect)
 fmt.Println("After Add() : ", newRect)

 // Canon returns the canonical version of r. The returned rectangle has minimum and maximum coordinates swapped if necessary so that it is well-formed.
 canonRect := newRect.Canon()

 fmt.Println("Canonical Version : ", canonRect)

 fmt.Println("canonRect's width : ", canonRect.Dx())
 fmt.Println("canonRect's height : ", canonRect.Dy())

 }

Output :

Min X : 0

Max X : 500

Before Add() : (0,0)-(500,500)

After Add() : (100,100)-(600,600)

Canonical Version : (100,100)-(600,600)

canonRect's width : 500

canonRect's height : 500

References :

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

Advertisement