f32.go 1.3 KB

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