RectUtil.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "RectUtil.h"
  21. @implementation RectUtil
  22. +(CGRect) setPositionOf: (CGRect) rect x:(CGFloat) x y:(CGFloat) y
  23. {
  24. return CGRectMake(x, y, rect.size.width, rect.size.height);
  25. }
  26. +(CGRect) setPositionOf: (CGRect) rect point:(CGPoint) point
  27. {
  28. return CGRectMake(point.x, point.y, rect.size.width, rect.size.height);
  29. }
  30. +(CGRect) setXPositionOf: (CGRect) rect x:(CGFloat) x
  31. {
  32. return CGRectMake(x, rect.origin.y, rect.size.width, rect.size.height);
  33. }
  34. +(CGRect) setYPositionOf: (CGRect) rect y:(CGFloat) y
  35. {
  36. return CGRectMake(rect.origin.x, y, rect.size.width, rect.size.height);
  37. }
  38. +(CGRect) changeSizeOf: (CGRect) rect deltaX:(CGFloat) deltaX deltaY:(CGFloat) deltaY
  39. {
  40. return CGRectMake(rect.origin.x, rect.origin.y, rect.size.width + deltaX, rect.size.height + deltaY);
  41. }
  42. +(CGRect) setSizeOf: (CGRect) rect width:(CGFloat) width height:(CGFloat) height
  43. {
  44. return CGRectMake(rect.origin.x, rect.origin.y, width, height);
  45. }
  46. +(CGRect) setWidthOf: (CGRect) rect width:(CGFloat) width
  47. {
  48. return CGRectMake(rect.origin.x, rect.origin.y, width, rect.size.height);
  49. }
  50. +(CGRect) setHeightOf: (CGRect) rect height:(CGFloat) height
  51. {
  52. return CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, height);
  53. }
  54. +(BOOL) doRectOverlapXExcludingEdgesLeft: (CGRect) left right: (CGRect) right
  55. {
  56. CGFloat leftMinX = CGRectGetMinX(left);
  57. CGFloat leftMaxX = CGRectGetMaxX(left);
  58. CGFloat rightMinX = CGRectGetMinX(right);
  59. CGFloat rightMaxX = CGRectGetMaxX(right);
  60. if (leftMinX > rightMinX && leftMinX < rightMaxX) {
  61. return TRUE;
  62. }
  63. if (leftMaxX > rightMinX && leftMaxX < rightMaxX) {
  64. return TRUE;
  65. }
  66. if (rightMinX > leftMinX && rightMinX < leftMaxX) {
  67. return TRUE;
  68. }
  69. if (rightMaxX > leftMinX && rightMaxX < leftMaxX) {
  70. return TRUE;
  71. }
  72. if (leftMinX == rightMinX && leftMaxX == rightMaxX) {
  73. return TRUE;
  74. }
  75. return FALSE;
  76. }
  77. + (CGRect)offsetRect: (CGRect) rect byX: (CGFloat) dx byY: (CGFloat) dy
  78. {
  79. CGRect newRect = CGRectMake(rect.origin.x + dx, rect.origin.y + dy, rect.size.width, rect.size.height);
  80. return newRect;
  81. }
  82. + (CGRect)offsetAndResizeRect: (CGRect) rect byX: (CGFloat) dx byY: (CGFloat) dy
  83. {
  84. CGRect newRect = CGRectMake(rect.origin.x + dx, rect.origin.y + dy, rect.size.width - dx, rect.size.height - dy);
  85. return newRect;
  86. }
  87. + (CGRect) rectZeroAtCenterOf: (CGRect) rect
  88. {
  89. CGFloat x = rect.origin.x + rect.size.width/2.0;
  90. CGFloat y = rect.origin.y + rect.size.height/2.0;
  91. return CGRectMake(x, y, 0.0, 0.0);
  92. }
  93. + (CGRect) moveRect: (CGRect) rect to: (CGPoint) point keepingOffset: (CGPoint) offset
  94. {
  95. CGFloat x = point.x + offset.x;
  96. CGFloat y = point.y + offset.y;
  97. return [RectUtil setPositionOf:rect x: x y: y ];
  98. }
  99. + (CGRect) growRect: (CGRect) rect byDx: (CGFloat) dX byDy: (CGFloat) dY
  100. {
  101. CGFloat x = rect.origin.x - dX / 2.0;
  102. CGFloat y = rect.origin.y - dY / 2.0;
  103. CGFloat width = rect.size.width + dX;
  104. CGFloat height = rect.size.height + dY;
  105. return CGRectMake(x, y, width, height);
  106. }
  107. + (CGRect) growRectBaseline: (CGRect) rect byDx: (CGFloat) dX byDy: (CGFloat) dY
  108. {
  109. CGFloat x = rect.origin.x - dX / 2.0;
  110. CGFloat y = rect.origin.y - dY;
  111. CGFloat width = rect.size.width + dX;
  112. CGFloat height = rect.size.height + dY;
  113. return CGRectMake(x, y, width, height);
  114. }
  115. + (CGRect) rect: (CGRect) rect centerIn: (CGRect) outerRect {
  116. return [RectUtil rect: rect centerIn: outerRect round: NO];
  117. }
  118. + (CGRect) rect: (CGRect) rect centerIn: (CGRect) outerRect round: (BOOL) round
  119. {
  120. CGFloat innerWidth = rect.size.width;
  121. CGFloat outerWidth = outerRect.size.width;
  122. CGFloat innerHeight = rect.size.height;
  123. CGFloat outerHeight = outerRect.size.height;
  124. CGFloat x = (outerWidth - innerWidth) / 2.0;
  125. CGFloat y = (outerHeight - innerHeight) / 2.0;
  126. if (round) {
  127. x = roundf(x);
  128. y = roundf(y);
  129. }
  130. return [RectUtil setPositionOf: rect x: x y: y];
  131. }
  132. + (CGRect) rect: (CGRect) rect centerVerticalIn: (CGRect) outerRect
  133. {
  134. return [RectUtil rect: rect centerVerticalIn: outerRect round:NO];
  135. }
  136. + (CGRect) rect: (CGRect) rect centerVerticalIn: (CGRect) outerRect round: (BOOL) round
  137. {
  138. CGFloat innerHeight = rect.size.height;
  139. CGFloat outerHeight = outerRect.size.height;
  140. CGFloat x = rect.origin.x;
  141. CGFloat y = (outerHeight - innerHeight) / 2.0;
  142. if (round)
  143. y = roundf(y);
  144. return [RectUtil setPositionOf: rect x: x y: y];
  145. }
  146. + (CGRect) rect: (CGRect) rect centerHorizontalIn: (CGRect) outerRect
  147. {
  148. return [RectUtil rect: rect centerHorizontalIn: outerRect round:NO];
  149. }
  150. + (CGRect) rect: (CGRect) rect centerHorizontalIn: (CGRect) outerRect round: (BOOL) round
  151. {
  152. CGFloat innerWidth = rect.size.width;
  153. CGFloat outerWidth = outerRect.size.width;
  154. CGFloat x = (outerWidth - innerWidth) / 2.0;
  155. if (round)
  156. x = roundf(x);
  157. CGFloat y = rect.origin.y;
  158. return [RectUtil setPositionOf: rect x: x y: y];
  159. }
  160. + (CGPoint) centerOf: (CGRect) rect
  161. {
  162. CGFloat x = rect.origin.x + rect.size.width/2.0;
  163. CGFloat y = rect.origin.y + rect.size.height/2.0;
  164. return CGPointMake(x, y);
  165. }
  166. + (CGFloat) distancePoint: (CGPoint) p1 toPoint: (CGPoint) p2
  167. {
  168. CGFloat xDist = (p2.x - p1.x);
  169. CGFloat yDist = (p2.y - p1.y);
  170. CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));
  171. return distance;
  172. }
  173. + (CGRect) rect: (CGRect) rect alignVerticalWith: (CGRect) outerRect round: (BOOL) round
  174. {
  175. CGFloat innerHeight = rect.size.height;
  176. CGFloat outerHeight = outerRect.size.height;
  177. CGFloat x = rect.origin.x;
  178. CGFloat y = outerRect.origin.y + (outerHeight - innerHeight) / 2.0;
  179. if (round)
  180. y = roundf(y);
  181. return [RectUtil setPositionOf: rect x: x y: y];
  182. }
  183. + (CGRect) rect: (CGRect) rect centerAlignWith: (CGRect) outerRect round: (BOOL) round
  184. {
  185. CGFloat innerHeight = rect.size.height;
  186. CGFloat outerHeight = outerRect.size.height;
  187. CGFloat innerWidth = rect.size.width;
  188. CGFloat outerWidth = outerRect.size.width;
  189. CGFloat x = outerRect.origin.x + (outerWidth - innerWidth) / 2.0;
  190. CGFloat y = outerRect.origin.y + (outerHeight - innerHeight) / 2.0;
  191. if (round) {
  192. x = roundf(x);
  193. y = roundf(y);
  194. }
  195. return [RectUtil setPositionOf: rect x: x y: y];
  196. }
  197. @end