geometry.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // Add returns a new Position that is the result of offsetting the current
  37. // position by p2 X and Y.
  38. func (p Position) Add(v Vector2) Position {
  39. x, y := v.Components()
  40. return Position{p.X + x, p.Y + y}
  41. }
  42. // AddXY returns a new Position by adding x and y to the current one.
  43. func (p Position) AddXY(x, y float32) Position {
  44. return Position{p.X + x, p.Y + y}
  45. }
  46. // Components returns the X and Y elements of this Position
  47. func (p Position) Components() (float32, float32) {
  48. return p.X, p.Y
  49. }
  50. // IsZero returns whether the Position is at the zero-point.
  51. func (p Position) IsZero() bool {
  52. return p.X == 0.0 && p.Y == 0.0
  53. }
  54. // Subtract returns a new Position that is the result of offsetting the current
  55. // position by p2 -X and -Y.
  56. func (p Position) Subtract(v Vector2) Position {
  57. x, y := v.Components()
  58. return Position{p.X - x, p.Y - y}
  59. }
  60. // SubtractXY returns a new Position by subtracting x and y from the current one.
  61. func (p Position) SubtractXY(x, y float32) Position {
  62. return Position{p.X - x, p.Y - y}
  63. }
  64. // Size describes something with width and height.
  65. type Size struct {
  66. Width float32 // The number of units along the X axis.
  67. Height float32 // The number of units along the Y axis.
  68. }
  69. // NewSize returns a newly allocated Size of the specified dimensions.
  70. func NewSize(w float32, h float32) Size {
  71. return Size{w, h}
  72. }
  73. // Add returns a new Size that is the result of increasing the current size by
  74. // s2 Width and Height.
  75. func (s Size) Add(v Vector2) Size {
  76. w, h := v.Components()
  77. return Size{s.Width + w, s.Height + h}
  78. }
  79. // AddWidthHeight returns a new Size by adding width and height to the current one.
  80. func (s Size) AddWidthHeight(width, height float32) Size {
  81. return Size{s.Width + width, s.Height + height}
  82. }
  83. // IsZero returns whether the Size has zero width and zero height.
  84. func (s Size) IsZero() bool {
  85. return s.Width == 0.0 && s.Height == 0.0
  86. }
  87. // Max returns a new Size that is the maximum of the current Size and s2.
  88. func (s Size) Max(v Vector2) Size {
  89. x, y := v.Components()
  90. maxW := Max(s.Width, x)
  91. maxH := Max(s.Height, y)
  92. return NewSize(maxW, maxH)
  93. }
  94. // Min returns a new Size that is the minimum of the current Size and s2.
  95. func (s Size) Min(v Vector2) Size {
  96. x, y := v.Components()
  97. minW := Min(s.Width, x)
  98. minH := Min(s.Height, y)
  99. return NewSize(minW, minH)
  100. }
  101. // Components returns the Width and Height elements of this Size
  102. func (s Size) Components() (float32, float32) {
  103. return s.Width, s.Height
  104. }
  105. // Subtract returns a new Size that is the result of decreasing the current size
  106. // by s2 Width and Height.
  107. func (s Size) Subtract(v Vector2) Size {
  108. w, h := v.Components()
  109. return Size{s.Width - w, s.Height - h}
  110. }
  111. // SubtractWidthHeight returns a new Size by subtracting width and height from the current one.
  112. func (s Size) SubtractWidthHeight(width, height float32) Size {
  113. return Size{s.Width - width, s.Height - height}
  114. }