UnitPoint.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /// A normalized 2D point in a view's coordinate space.
  2. ///
  3. /// The point's coordinates start from the view's top leading corner, and
  4. /// are relative to the views size.
  5. ///
  6. /// Coordinates between 0 and 1 are inside the bounds of the view, and coordinates
  7. /// outside of that range are outside of the view.
  8. public struct UnitPoint: Hashable, Sendable {
  9. /// The normalized distance from the origin to the point in the horizontal direction.
  10. public var x: Double
  11. /// The normalized distance from the origin to the point in the vertical dimension.
  12. public var y: Double
  13. /// Creates a unit point with the specified horizontal and vertical offsets.
  14. public init(x: Double, y: Double) {
  15. self.x = x
  16. self.y = y
  17. }
  18. /// Creates a unit point at the origin.
  19. public init() {
  20. self.x = 0
  21. self.y = 0
  22. }
  23. }
  24. extension UnitPoint {
  25. /// The origin of a view, in the top, leading corner.
  26. public static let zero = UnitPoint()
  27. /// A point that's in the top, leading corner of a view.
  28. public static let topLeading = UnitPoint(x: 0, y: 0)
  29. /// A point that's centered horizontally on the top edge of a view.
  30. public static let top = UnitPoint(x: 0.5, y: 0)
  31. /// A point that's in the top, trailing corner of a view.
  32. public static let topTrailing = UnitPoint(x: 1, y: 0)
  33. /// A point that's centered vertically on the leading edge of a view.
  34. public static let leading = UnitPoint(x: 0, y: 0.5)
  35. /// A point that's centered vertically on the trailing edge of a view.
  36. public static let trailing = UnitPoint(x: 1, y: 0.5)
  37. /// A point that's centered in a view.
  38. public static let center = UnitPoint(x: 0.5, y: 0.5)
  39. /// A point that's in the bottom, leading corner of a view.
  40. public static let bottomLeading = UnitPoint(x: 0, y: 1)
  41. /// A point that's centered horizontally on the bottom edge of a view.
  42. public static let bottom = UnitPoint(x: 0.5, y: 1)
  43. /// A point that's in the bottom, trailing corner of a view.
  44. public static let bottomTrailing = UnitPoint(x: 1, y: 1)
  45. }