util.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. // RectBounds specifies the size of the boundaries of a rectangle.
  6. // It can represent the thickness of the borders, the margins, or the padding of a rectangle.
  7. type RectBounds struct {
  8. Top float32
  9. Right float32
  10. Bottom float32
  11. Left float32
  12. }
  13. // Set sets the values of the RectBounds.
  14. func (bs *RectBounds) Set(top, right, bottom, left float32) {
  15. if top >= 0 {
  16. bs.Top = top
  17. }
  18. if right >= 0 {
  19. bs.Right = right
  20. }
  21. if bottom >= 0 {
  22. bs.Bottom = bottom
  23. }
  24. if left >= 0 {
  25. bs.Left = left
  26. }
  27. }
  28. // Rect represents a rectangle.
  29. type Rect struct {
  30. X float32
  31. Y float32
  32. Width float32
  33. Height float32
  34. }
  35. // Contains determines whether a 2D point is inside the Rect.
  36. func (r *Rect) Contains(x, y float32) bool {
  37. if x < r.X || x > r.X+r.Width {
  38. return false
  39. }
  40. if y < r.Y || y > r.Y+r.Height {
  41. return false
  42. }
  43. return true
  44. }