Position.swift 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /// A positon.
  2. struct Position: Hashable, Sendable {
  3. /// The zero position (aka the origin).
  4. static let zero = Self(0, 0)
  5. /// The position's x component.
  6. var x: Double
  7. /// The position's y component.
  8. var y: Double
  9. var vector: SIMD2<Int> {
  10. SIMD2(
  11. LayoutSystem.roundSize(x),
  12. LayoutSystem.roundSize(y)
  13. )
  14. }
  15. /// Creates a new position.
  16. init(_ x: Double, _ y: Double) {
  17. self.x = x
  18. self.y = y
  19. }
  20. /// The position component associated with the given orientation's main axis.
  21. public subscript(component orientation: Orientation) -> Double {
  22. get {
  23. switch orientation {
  24. case .horizontal:
  25. x
  26. case .vertical:
  27. y
  28. }
  29. }
  30. set {
  31. switch orientation {
  32. case .horizontal:
  33. x = newValue
  34. case .vertical:
  35. y = newValue
  36. }
  37. }
  38. }
  39. /// The position component associated with the given axis.
  40. public subscript(component axis: Axis) -> Double {
  41. get {
  42. self[component: axis.orientation]
  43. }
  44. set {
  45. self[component: axis.orientation] = newValue
  46. }
  47. }
  48. }