geometry.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package fyne
  2. var _ Vector2 = (*Delta)(nil)
  3. var _ Vector2 = (*Position)(nil)
  4. var _ Vector2 = (*Size)(nil)
  5. // Vector2 marks geometry types that can operate as a coordinate vector.
  6. type Vector2 interface {
  7. Components() (float32, float32)
  8. IsZero() bool
  9. }
  10. // Delta is a generic X, Y coordinate, size or movement representation.
  11. type Delta struct {
  12. DX, DY float32
  13. }
  14. // NewDelta returns a newly allocated Delta representing a movement in the X and Y axis.
  15. func NewDelta(dx float32, dy float32) Delta {
  16. return Delta{DX: dx, DY: dy}
  17. }
  18. // Components returns the X and Y elements of this Delta.
  19. func (v Delta) Components() (float32, float32) {
  20. return v.DX, v.DY
  21. }
  22. // IsZero returns whether the Position is at the zero-point.
  23. func (v Delta) IsZero() bool {
  24. return v.DX == 0.0 && v.DY == 0.0
  25. }
  26. // Position describes a generic X, Y coordinate relative to a parent Canvas
  27. // or CanvasObject.
  28. type Position struct {
  29. X float32 // The position from the parent's left edge
  30. Y float32 // The position from the parent's top edge
  31. }
  32. // NewPos returns a newly allocated Position representing the specified coordinates.
  33. func NewPos(x float32, y float32) Position {
  34. return Position{x, y}
  35. }
  36. // NewSquareOffsetPos returns a newly allocated Position with the same x and y position.
  37. //
  38. // Since: 2.4
  39. func NewSquareOffsetPos(length float32) Position {
  40. return Position{length, length}
  41. }
  42. // Add returns a new Position that is the result of offsetting the current
  43. // position by p2 X and Y.
  44. func (p Position) Add(v Vector2) Position {
  45. // NOTE: Do not simplify to `return p.AddXY(v.Components())`, it prevents inlining.
  46. x, y := v.Components()
  47. return Position{p.X + x, p.Y + y}
  48. }
  49. // AddXY returns a new Position by adding x and y to the current one.
  50. func (p Position) AddXY(x, y float32) Position {
  51. return Position{p.X + x, p.Y + y}
  52. }
  53. // Components returns the X and Y elements of this Position
  54. func (p Position) Components() (float32, float32) {
  55. return p.X, p.Y
  56. }
  57. // IsZero returns whether the Position is at the zero-point.
  58. func (p Position) IsZero() bool {
  59. return p.X == 0.0 && p.Y == 0.0
  60. }
  61. // Subtract returns a new Position that is the result of offsetting the current
  62. // position by p2 -X and -Y.
  63. func (p Position) Subtract(v Vector2) Position {
  64. // NOTE: Do not simplify to `return p.SubtractXY(v.Components())`, it prevents inlining.
  65. x, y := v.Components()
  66. return Position{p.X - x, p.Y - y}
  67. }
  68. // SubtractXY returns a new Position by subtracting x and y from the current one.
  69. func (p Position) SubtractXY(x, y float32) Position {
  70. return Position{p.X - x, p.Y - y}
  71. }
  72. // Size describes something with width and height.
  73. type Size struct {
  74. Width float32 // The number of units along the X axis.
  75. Height float32 // The number of units along the Y axis.
  76. }
  77. // NewSize returns a newly allocated Size of the specified dimensions.
  78. func NewSize(w float32, h float32) Size {
  79. return Size{w, h}
  80. }
  81. // NewSquareSize returns a newly allocated Size with the same width and height.
  82. //
  83. // Since: 2.4
  84. func NewSquareSize(side float32) Size {
  85. return Size{side, side}
  86. }
  87. // Add returns a new Size that is the result of increasing the current size by
  88. // s2 Width and Height.
  89. func (s Size) Add(v Vector2) Size {
  90. // NOTE: Do not simplify to `return s.AddXY(v.Components())`, it prevents inlining.
  91. w, h := v.Components()
  92. return Size{s.Width + w, s.Height + h}
  93. }
  94. // AddWidthHeight returns a new Size by adding width and height to the current one.
  95. func (s Size) AddWidthHeight(width, height float32) Size {
  96. return Size{s.Width + width, s.Height + height}
  97. }
  98. // IsZero returns whether the Size has zero width and zero height.
  99. func (s Size) IsZero() bool {
  100. return s.Width == 0.0 && s.Height == 0.0
  101. }
  102. // Max returns a new Size that is the maximum of the current Size and s2.
  103. func (s Size) Max(v Vector2) Size {
  104. x, y := v.Components()
  105. maxW := Max(s.Width, x)
  106. maxH := Max(s.Height, y)
  107. return NewSize(maxW, maxH)
  108. }
  109. // Min returns a new Size that is the minimum of the current Size and s2.
  110. func (s Size) Min(v Vector2) Size {
  111. x, y := v.Components()
  112. minW := Min(s.Width, x)
  113. minH := Min(s.Height, y)
  114. return NewSize(minW, minH)
  115. }
  116. // Components returns the Width and Height elements of this Size
  117. func (s Size) Components() (float32, float32) {
  118. return s.Width, s.Height
  119. }
  120. // Subtract returns a new Size that is the result of decreasing the current size
  121. // by s2 Width and Height.
  122. func (s Size) Subtract(v Vector2) Size {
  123. // NOTE: Do not simplify to `return s.SubtractXY(v.Components())`, it prevents inlining.
  124. w, h := v.Components()
  125. return Size{s.Width - w, s.Height - h}
  126. }
  127. // SubtractWidthHeight returns a new Size by subtracting width and height from the current one.
  128. func (s Size) SubtractWidthHeight(width, height float32) Size {
  129. return Size{s.Width - width, s.Height - height}
  130. }