f32.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. /*
  3. Package f32 is an internal version of the public package f32 with
  4. extra types for internal use.
  5. */
  6. package f32
  7. import (
  8. "image"
  9. "math"
  10. "gioui.org/f32"
  11. )
  12. type Point = f32.Point
  13. type Affine2D = f32.Affine2D
  14. var NewAffine2D = f32.NewAffine2D
  15. // A Rectangle contains the points (X, Y) where Min.X <= X < Max.X,
  16. // Min.Y <= Y < Max.Y.
  17. type Rectangle struct {
  18. Min, Max Point
  19. }
  20. // String return a string representation of r.
  21. func (r Rectangle) String() string {
  22. return r.Min.String() + "-" + r.Max.String()
  23. }
  24. // Rect is a shorthand for Rectangle{Point{x0, y0}, Point{x1, y1}}.
  25. // The returned Rectangle has x0 and y0 swapped if necessary so that
  26. // it's correctly formed.
  27. func Rect(x0, y0, x1, y1 float32) Rectangle {
  28. if x0 > x1 {
  29. x0, x1 = x1, x0
  30. }
  31. if y0 > y1 {
  32. y0, y1 = y1, y0
  33. }
  34. return Rectangle{Point{x0, y0}, Point{x1, y1}}
  35. }
  36. // Pt is shorthand for Point{X: x, Y: y}.
  37. var Pt = f32.Pt
  38. // Size returns r's width and height.
  39. func (r Rectangle) Size() Point {
  40. return Point{X: r.Dx(), Y: r.Dy()}
  41. }
  42. // Dx returns r's width.
  43. func (r Rectangle) Dx() float32 {
  44. return r.Max.X - r.Min.X
  45. }
  46. // Dy returns r's Height.
  47. func (r Rectangle) Dy() float32 {
  48. return r.Max.Y - r.Min.Y
  49. }
  50. // Intersect returns the intersection of r and s.
  51. func (r Rectangle) Intersect(s Rectangle) Rectangle {
  52. if r.Min.X < s.Min.X {
  53. r.Min.X = s.Min.X
  54. }
  55. if r.Min.Y < s.Min.Y {
  56. r.Min.Y = s.Min.Y
  57. }
  58. if r.Max.X > s.Max.X {
  59. r.Max.X = s.Max.X
  60. }
  61. if r.Max.Y > s.Max.Y {
  62. r.Max.Y = s.Max.Y
  63. }
  64. if r.Empty() {
  65. return Rectangle{}
  66. }
  67. return r
  68. }
  69. // Union returns the union of r and s.
  70. func (r Rectangle) Union(s Rectangle) Rectangle {
  71. if r.Empty() {
  72. return s
  73. }
  74. if s.Empty() {
  75. return r
  76. }
  77. if r.Min.X > s.Min.X {
  78. r.Min.X = s.Min.X
  79. }
  80. if r.Min.Y > s.Min.Y {
  81. r.Min.Y = s.Min.Y
  82. }
  83. if r.Max.X < s.Max.X {
  84. r.Max.X = s.Max.X
  85. }
  86. if r.Max.Y < s.Max.Y {
  87. r.Max.Y = s.Max.Y
  88. }
  89. return r
  90. }
  91. // Canon returns the canonical version of r, where Min is to
  92. // the upper left of Max.
  93. func (r Rectangle) Canon() Rectangle {
  94. if r.Max.X < r.Min.X {
  95. r.Min.X, r.Max.X = r.Max.X, r.Min.X
  96. }
  97. if r.Max.Y < r.Min.Y {
  98. r.Min.Y, r.Max.Y = r.Max.Y, r.Min.Y
  99. }
  100. return r
  101. }
  102. // Empty reports whether r represents the empty area.
  103. func (r Rectangle) Empty() bool {
  104. return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
  105. }
  106. // Add offsets r with the vector p.
  107. func (r Rectangle) Add(p Point) Rectangle {
  108. return Rectangle{
  109. Point{r.Min.X + p.X, r.Min.Y + p.Y},
  110. Point{r.Max.X + p.X, r.Max.Y + p.Y},
  111. }
  112. }
  113. // Sub offsets r with the vector -p.
  114. func (r Rectangle) Sub(p Point) Rectangle {
  115. return Rectangle{
  116. Point{r.Min.X - p.X, r.Min.Y - p.Y},
  117. Point{r.Max.X - p.X, r.Max.Y - p.Y},
  118. }
  119. }
  120. // Round returns the smallest integer rectangle that
  121. // contains r.
  122. func (r Rectangle) Round() image.Rectangle {
  123. return image.Rectangle{
  124. Min: image.Point{
  125. X: int(floor(r.Min.X)),
  126. Y: int(floor(r.Min.Y)),
  127. },
  128. Max: image.Point{
  129. X: int(ceil(r.Max.X)),
  130. Y: int(ceil(r.Max.Y)),
  131. },
  132. }
  133. }
  134. // fRect converts a rectangle to a f32internal.Rectangle.
  135. func FRect(r image.Rectangle) Rectangle {
  136. return Rectangle{
  137. Min: FPt(r.Min), Max: FPt(r.Max),
  138. }
  139. }
  140. // Fpt converts an point to a f32.Point.
  141. func FPt(p image.Point) Point {
  142. return Point{
  143. X: float32(p.X), Y: float32(p.Y),
  144. }
  145. }
  146. func ceil(v float32) int {
  147. return int(math.Ceil(float64(v)))
  148. }
  149. func floor(v float32) int {
  150. return int(math.Floor(float64(v)))
  151. }