You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2016 Luke Zhao <me@lkzhao.com>
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import QuartzCore
  23. let π = CGFloat.pi
  24. internal struct KeySet<Key: Hashable, Value: Hashable> {
  25. var dict: [Key: Set<Value>] = [:]
  26. internal subscript(key: Key) -> Set<Value> {
  27. mutating get {
  28. if dict[key] == nil {
  29. dict[key] = Set<Value>()
  30. }
  31. return dict[key]!
  32. }
  33. set {
  34. dict[key] = newValue
  35. }
  36. }
  37. }
  38. internal extension CGSize {
  39. var center: CGPoint {
  40. return CGPoint(x: width / 2, y: height / 2)
  41. }
  42. var point: CGPoint {
  43. return CGPoint(x: width, y: height)
  44. }
  45. func transform(_ t: CGAffineTransform) -> CGSize {
  46. return self.applying(t)
  47. }
  48. func transform(_ t: CATransform3D) -> CGSize {
  49. return self.applying(CATransform3DGetAffineTransform(t))
  50. }
  51. }
  52. internal extension CGRect {
  53. var center: CGPoint {
  54. return CGPoint(x: origin.x + width / 2, y: origin.y + height / 2)
  55. }
  56. var bounds: CGRect {
  57. return CGRect(origin: CGPoint.zero, size: size)
  58. }
  59. init(center: CGPoint, size: CGSize) {
  60. self.init(x: center.x - size.width / 2, y: center.y - size.height / 2, width: size.width, height: size.height)
  61. }
  62. }
  63. internal extension CGFloat {
  64. func clamp(_ a: CGFloat, _ b: CGFloat) -> CGFloat {
  65. return self < a ? a : (self > b ? b : self)
  66. }
  67. }
  68. internal extension TimeInterval {
  69. func clamp(_ a: TimeInterval, _ b: TimeInterval) -> TimeInterval {
  70. return self < a ? a : (self > b ? b : self)
  71. }
  72. }
  73. internal extension CGPoint {
  74. func translate(_ dx: CGFloat, dy: CGFloat) -> CGPoint {
  75. return CGPoint(x: self.x + dx, y: self.y + dy)
  76. }
  77. func transform(_ t: CGAffineTransform) -> CGPoint {
  78. return self.applying(t)
  79. }
  80. func transform(_ t: CATransform3D) -> CGPoint {
  81. return self.applying(CATransform3DGetAffineTransform(t))
  82. }
  83. func distance(_ b: CGPoint) -> CGFloat {
  84. return sqrt(pow(self.x - b.x, 2) + pow(self.y - b.y, 2))
  85. }
  86. static func + (left: CGPoint, right: CGPoint) -> CGPoint {
  87. return CGPoint(x: left.x + right.x, y: left.y + right.y)
  88. }
  89. static func - (left: CGPoint, right: CGPoint) -> CGPoint {
  90. return CGPoint(x: left.x - right.x, y: left.y - right.y)
  91. }
  92. static func / (left: CGPoint, right: CGFloat) -> CGPoint {
  93. return CGPoint(x: left.x / right, y: left.y / right)
  94. }
  95. static func / (left: CGPoint, right: CGPoint) -> CGPoint {
  96. return CGPoint(x: left.x / right.x, y: left.y / right.y)
  97. }
  98. static func * (left: CGPoint, right: CGFloat) -> CGPoint {
  99. return CGPoint(x: left.x * right, y: left.y * right)
  100. }
  101. static func * (left: CGPoint, right: CGSize) -> CGPoint {
  102. return CGPoint(x: left.x * right.width, y: left.y * right.height)
  103. }
  104. static func * (left: CGFloat, right: CGPoint) -> CGPoint {
  105. return right * left
  106. }
  107. static func * (left: CGPoint, right: CGPoint) -> CGPoint {
  108. return CGPoint(x: left.x * right.x, y: left.y * right.y)
  109. }
  110. static prefix func - (point: CGPoint) -> CGPoint {
  111. return .zero - point
  112. }
  113. static func abs(_ p: CGPoint) -> CGPoint {
  114. return CGPoint(x: Swift.abs(p.x), y: Swift.abs(p.y))
  115. }
  116. }
  117. internal extension CGSize {
  118. static func * (left: CGSize, right: CGFloat) -> CGSize {
  119. return CGSize(width: left.width * right, height: left.height * right)
  120. }
  121. static func * (left: CGSize, right: CGSize) -> CGSize {
  122. return CGSize(width: left.width * right.width, height: left.height * right.height)
  123. }
  124. static func / (left: CGSize, right: CGSize) -> CGSize {
  125. return CGSize(width: left.width / right.width, height: left.height / right.height)
  126. }
  127. static func / (left: CGPoint, right: CGSize) -> CGPoint {
  128. return CGPoint(x: left.x / right.width, y: left.y / right.height)
  129. }
  130. }
  131. extension CATransform3D: Equatable {
  132. public static func == (lhs: CATransform3D, rhs: CATransform3D) -> Bool {
  133. var lhs = lhs
  134. var rhs = rhs
  135. return memcmp(&lhs, &rhs, MemoryLayout<CATransform3D>.size) == 0
  136. }
  137. }