github.com/utopiagio/gio@v0.0.8/f32/f32.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  /*
     4  Package f32 is a float32 implementation of package image's
     5  Point and affine transformations.
     6  
     7  The coordinate space has the origin in the top left
     8  corner with the axes extending right and down.
     9  */
    10  package f32
    11  
    12  import (
    13  	"image"
    14  	"math"
    15  	"strconv"
    16  )
    17  
    18  // A Point is a two dimensional point.
    19  type Point struct {
    20  	X, Y float32
    21  }
    22  
    23  // String return a string representation of p.
    24  func (p Point) String() string {
    25  	return "(" + strconv.FormatFloat(float64(p.X), 'f', -1, 32) +
    26  		"," + strconv.FormatFloat(float64(p.Y), 'f', -1, 32) + ")"
    27  }
    28  
    29  // Pt is shorthand for Point{X: x, Y: y}.
    30  func Pt(x, y float32) Point {
    31  	return Point{X: x, Y: y}
    32  }
    33  
    34  // Add return the point p+p2.
    35  func (p Point) Add(p2 Point) Point {
    36  	return Point{X: p.X + p2.X, Y: p.Y + p2.Y}
    37  }
    38  
    39  // Sub returns the vector p-p2.
    40  func (p Point) Sub(p2 Point) Point {
    41  	return Point{X: p.X - p2.X, Y: p.Y - p2.Y}
    42  }
    43  
    44  // Mul returns p scaled by s.
    45  func (p Point) Mul(s float32) Point {
    46  	return Point{X: p.X * s, Y: p.Y * s}
    47  }
    48  
    49  // Div returns the vector p/s.
    50  func (p Point) Div(s float32) Point {
    51  	return Point{X: p.X / s, Y: p.Y / s}
    52  }
    53  
    54  // Round returns the integer point closest to p.
    55  func (p Point) Round() image.Point {
    56  	return image.Point{
    57  		X: int(math.Round(float64(p.X))),
    58  		Y: int(math.Round(float64(p.Y))),
    59  	}
    60  }