|
| 1 | +// |
| 2 | +// CardIOCGGeometry.h |
| 3 | +// See the file "LICENSE.md" for the full license governing this code. |
| 4 | +// |
| 5 | + |
| 6 | +// Note: These don't really need namespacing, since they are inlined. |
| 7 | + |
| 8 | +// Note: need to use `#ifdef __LP64__` logic, rather than depending on <tgmath.h>, |
| 9 | +// because this header is sometimes included in .mm files (where <tgmath.h> doesn't apply). |
| 10 | + |
| 11 | +#import <Foundation/Foundation.h> |
| 12 | + |
| 13 | +static inline CGRect CGRectZeroWithSize(CGSize size) { |
| 14 | + return CGRectMake(0.0f, 0.0f, size.width, size.height); |
| 15 | +} |
| 16 | + |
| 17 | +static inline CGRect CGRectZeroWithSquareSize(CGFloat size) { |
| 18 | + return CGRectMake(0.0f, 0.0f, size, size); |
| 19 | +} |
| 20 | + |
| 21 | +static inline CGRect CGRectZeroWithWidthAndHeight(CGFloat width, CGFloat height) { |
| 22 | + return CGRectMake(0.0f, 0.0f, width, height); |
| 23 | +} |
| 24 | + |
| 25 | +static inline CGRect CGRectWithOriginAndSize(CGPoint origin, CGSize size) { |
| 26 | + return CGRectMake(origin.x, origin.y, size.width, size.height); |
| 27 | +} |
| 28 | + |
| 29 | +static inline CGRect CGRectWithOriginWidthAndHeight(CGPoint origin, CGFloat width, CGFloat height) { |
| 30 | + return CGRectMake(origin.x, origin.y, width, height); |
| 31 | +} |
| 32 | + |
| 33 | +static inline CGRect CGRectWithOriginAndSquareSize(CGPoint origin, CGFloat size) { |
| 34 | + return CGRectMake(origin.x, origin.y, size, size); |
| 35 | +} |
| 36 | + |
| 37 | +static inline CGRect CGRectWithXYAndSize(CGFloat xOrigin, CGFloat yOrigin, CGSize size) { |
| 38 | + return CGRectMake(xOrigin, yOrigin, size.width, size.height); |
| 39 | +} |
| 40 | + |
| 41 | +static inline CGRect CGRectWithXYAndSquareSize(CGFloat xOrigin, CGFloat yOrigin, CGFloat size) { |
| 42 | + return CGRectMake(xOrigin, yOrigin, size, size); |
| 43 | +} |
| 44 | + |
| 45 | +static inline CGRect CGRectWithCenterAndSize(CGPoint center, CGSize size) { |
| 46 | + return CGRectMake(center.x - size.width / 2.0f, center.y - size.height / 2.0f, size.width, size.height); |
| 47 | +} |
| 48 | + |
| 49 | +static inline CGRect CGRectWithCenterAndSquareSize(CGPoint center, CGFloat size) { |
| 50 | + return CGRectMake(center.x - size / 2.0f, center.y - size / 2.0f, size, size); |
| 51 | +} |
| 52 | + |
| 53 | +static inline CGRect CGRectWithRotatedRect(CGRect rect) { |
| 54 | + return CGRectMake(rect.origin.y, rect.origin.x, rect.size.height, rect.size.width); |
| 55 | +} |
| 56 | + |
| 57 | +static inline CGSize CGSizeMakeSquare(CGFloat widthAndHeight) { |
| 58 | + return CGSizeMake(widthAndHeight, widthAndHeight); |
| 59 | +} |
| 60 | + |
| 61 | +static inline CGRect CGRectByAddingXOffset(CGRect originalRect, CGFloat xOffset) { |
| 62 | + return CGRectWithXYAndSize(originalRect.origin.x + xOffset, originalRect.origin.y, originalRect.size); |
| 63 | +} |
| 64 | + |
| 65 | +static inline CGRect CGRectByAddingYOffset(CGRect originalRect, CGFloat yOffset) { |
| 66 | + return CGRectWithXYAndSize(originalRect.origin.x, originalRect.origin.y + yOffset, originalRect.size); |
| 67 | +} |
| 68 | + |
| 69 | +static inline CGRect CGRectByAddingXAndYOffset(CGRect originalRect, CGFloat xOffset, CGFloat yOffset) { |
| 70 | + return CGRectWithXYAndSize(originalRect.origin.x + xOffset, originalRect.origin.y + yOffset, originalRect.size); |
| 71 | +} |
| 72 | + |
| 73 | +static inline CGRect CGRectByAddingWidth(CGRect originalRect, CGFloat additionalWidth) { |
| 74 | + return CGRectMake(originalRect.origin.x, originalRect.origin.y, originalRect.size.width + additionalWidth, originalRect.size.height); |
| 75 | +} |
| 76 | + |
| 77 | +static inline CGRect CGRectByAddingHeight(CGRect originalRect, CGFloat additionalHeight) { |
| 78 | + return CGRectMake(originalRect.origin.x, originalRect.origin.y, originalRect.size.width, originalRect.size.height + additionalHeight); |
| 79 | +} |
| 80 | + |
| 81 | +static inline CGRect CGRectBySettingWidth(CGRect originalRect, CGFloat newWidth) { |
| 82 | + return CGRectMake(originalRect.origin.x, originalRect.origin.y, newWidth, originalRect.size.height); |
| 83 | +} |
| 84 | + |
| 85 | +static inline CGRect CGRectBySettingHeight(CGRect originalRect, CGFloat newHeight) { |
| 86 | + return CGRectMake(originalRect.origin.x, originalRect.origin.y, originalRect.size.width, newHeight); |
| 87 | +} |
| 88 | + |
| 89 | +static inline CGRect CGRectByAddingWidthAndHeight(CGRect originalRect, CGFloat additionalWidth, CGFloat additionalHeight) { |
| 90 | + return CGRectMake(originalRect.origin.x, originalRect.origin.y, originalRect.size.width + additionalWidth, originalRect.size.height + additionalHeight); |
| 91 | +} |
| 92 | + |
| 93 | +static inline CGSize CGSizeByAddingHeight(CGSize originalSize, CGFloat extraHeight) { |
| 94 | + return CGSizeMake(originalSize.width, originalSize.height + extraHeight); |
| 95 | +} |
| 96 | + |
| 97 | +static inline CGSize CGSizeByAddingWidth(CGSize originalSize, CGFloat extraWidth) { |
| 98 | + return CGSizeMake(originalSize.width + extraWidth, originalSize.height); |
| 99 | +} |
| 100 | + |
| 101 | +static inline CGSize CGSizeByAddingWidthAndHeight(CGSize originalSize, CGFloat extraWidth, CGFloat extraHeight) { |
| 102 | + return CGSizeMake(originalSize.width + extraWidth, originalSize.height + extraHeight); |
| 103 | +} |
| 104 | + |
| 105 | +static inline CGSize CGSizeByScaling(CGSize originalSize, CGFloat scale) { |
| 106 | + return CGSizeMake(originalSize.width * scale, originalSize.height * scale); |
| 107 | +} |
| 108 | + |
| 109 | +static inline CGPoint CGPointByAddingXOffset(CGPoint originalPoint, CGFloat xOffset) { |
| 110 | + return CGPointMake(originalPoint.x + xOffset, originalPoint.y); |
| 111 | +} |
| 112 | + |
| 113 | +static inline CGPoint CGPointByAddingYOffset(CGPoint originalPoint, CGFloat yOffset) { |
| 114 | + return CGPointMake(originalPoint.x, originalPoint.y + yOffset); |
| 115 | +} |
| 116 | + |
| 117 | +static inline CGFloat SquaredDistanceBetweenPoints(CGPoint p1, CGPoint p2) { |
| 118 | + CGFloat deltaX = p1.x - p2.x; |
| 119 | + CGFloat deltaY = p1.y - p2.y; |
| 120 | + return (deltaX * deltaX) + (deltaY * deltaY); |
| 121 | +} |
| 122 | + |
| 123 | +static inline CGFloat DistanceBetweenPoints(CGPoint p1, CGPoint p2) { |
| 124 | +#ifdef __LP64__ |
| 125 | + return sqrt(SquaredDistanceBetweenPoints(p1, p2)); |
| 126 | +#else |
| 127 | + return sqrtf(SquaredDistanceBetweenPoints(p1, p2)); |
| 128 | +#endif |
| 129 | +} |
| 130 | + |
| 131 | +static inline CGPoint CenterOfRect(CGRect rect) { |
| 132 | + return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)); |
| 133 | +} |
| 134 | + |
| 135 | +static inline double AngleBetweenPoints(CGPoint p, CGPoint q) { |
| 136 | + CGPoint deltaVector = CGPointMake(p.x - q.x, p.y - q.y); |
| 137 | + double angle = atan(deltaVector.y / deltaVector.x) + (deltaVector.x < 0 ? M_PI : 0); |
| 138 | + return angle; |
| 139 | +} |
| 140 | + |
| 141 | +static inline CGPoint MidpointBetweenPoints(CGPoint p, CGPoint q) { |
| 142 | + return CGPointMake((p.x + q.x) / 2.0f, (p.y + q.y) / 2.0f); |
| 143 | +} |
| 144 | + |
| 145 | +static inline CGPoint PointMinusPoint(CGPoint p, CGPoint q) { |
| 146 | + return CGPointMake(p.x - q.x, p.y - q.y); |
| 147 | +} |
| 148 | + |
| 149 | +static inline CGPoint PointPlusPoint(CGPoint p, CGPoint q) { |
| 150 | + return CGPointMake(p.x + q.x, p.y + q.y); |
| 151 | +} |
| 152 | + |
| 153 | +static inline CGFloat OffsetToCenterFloatInFloat(CGFloat smallerValue, CGFloat largerValue) { |
| 154 | + return (largerValue - smallerValue) / 2.0f; |
| 155 | +} |
| 156 | + |
| 157 | +static inline CGRect CenteredRectInRectWithSize(CGRect rectToCenterIn, CGSize sizeOfCenteredRect) { |
| 158 | + return CGRectWithXYAndSize(rectToCenterIn.origin.x + OffsetToCenterFloatInFloat(sizeOfCenteredRect.width, rectToCenterIn.size.width), |
| 159 | + rectToCenterIn.origin.y + OffsetToCenterFloatInFloat(sizeOfCenteredRect.height, rectToCenterIn.size.height), |
| 160 | + sizeOfCenteredRect); |
| 161 | +} |
| 162 | + |
| 163 | +static inline CGSize ScaledSize(CGSize originalSize, CGFloat scalingFactor) { |
| 164 | + return CGSizeMake(originalSize.width * scalingFactor, originalSize.height * scalingFactor); |
| 165 | +} |
| 166 | + |
| 167 | +static inline CGRect CGRectRoundedToNearestPixel(CGRect rect) { |
| 168 | +#ifdef __LP64__ |
| 169 | + return CGRectMake(round(rect.origin.x), |
| 170 | + round(rect.origin.y), |
| 171 | + round(rect.size.width), |
| 172 | + round(rect.size.height)); |
| 173 | +#else |
| 174 | + return CGRectMake(roundf(rect.origin.x), |
| 175 | + roundf(rect.origin.y), |
| 176 | + roundf(rect.size.width), |
| 177 | + roundf(rect.size.height)); |
| 178 | +#endif |
| 179 | +} |
| 180 | + |
| 181 | +static inline CGRect CGRectFlooredToNearestPixel(CGRect rect) { |
| 182 | +#ifdef __LP64__ |
| 183 | + return CGRectMake(floor(rect.origin.x), |
| 184 | + floor(rect.origin.y), |
| 185 | + floor(rect.size.width), |
| 186 | + floor(rect.size.height)); |
| 187 | +#else |
| 188 | + return CGRectMake(floorf(rect.origin.x), |
| 189 | + floorf(rect.origin.y), |
| 190 | + floorf(rect.size.width), |
| 191 | + floorf(rect.size.height)); |
| 192 | +#endif |
| 193 | +} |
| 194 | + |
| 195 | +static inline CGFloat CGFloatMean(CGFloat a, CGFloat b) { |
| 196 | + return (a + b) / 2.0f; |
| 197 | +} |
| 198 | + |
| 199 | +static inline CGFloat orientationToRotation(UIInterfaceOrientation orientation) { |
| 200 | + CGFloat rotation = 0.0f; |
| 201 | + switch(orientation) { |
| 202 | + case UIInterfaceOrientationPortrait: { |
| 203 | + rotation = 0.0f; |
| 204 | + break; |
| 205 | + } |
| 206 | + case UIInterfaceOrientationLandscapeRight: { |
| 207 | + rotation = (CGFloat)M_PI_2; |
| 208 | + break; |
| 209 | + } |
| 210 | + case UIInterfaceOrientationPortraitUpsideDown: { |
| 211 | + rotation = (CGFloat)M_PI; |
| 212 | + break; |
| 213 | + } |
| 214 | + case UIInterfaceOrientationLandscapeLeft: { |
| 215 | + rotation = (CGFloat)(3.0 * M_PI_2); |
| 216 | + break; |
| 217 | + } |
| 218 | + default: |
| 219 | + break; |
| 220 | + } |
| 221 | + return rotation; |
| 222 | +} |
| 223 | + |
| 224 | +static inline CGRect aspectFit(CGSize contents, CGSize container) { |
| 225 | + // Aspect fit is not well defined when contents or container is degenerate. Rather than crashing, |
| 226 | + // return a zero-ish value. |
| 227 | + if(contents.height == 0 || contents.width == 0 || container.width == 0 || container.height == 0) { |
| 228 | + return CGRectZero; |
| 229 | + } |
| 230 | + |
| 231 | + CGFloat contentsAspectRatio = contents.width / contents.height; |
| 232 | + |
| 233 | + CGSize contained; |
| 234 | + if (contentsAspectRatio * container.height < container.width) { |
| 235 | + contained = CGSizeMake(contentsAspectRatio * container.height, container.height); |
| 236 | + } else if (contentsAspectRatio * container.height > container.width) { |
| 237 | + contained = CGSizeMake(container.width, container.width / contentsAspectRatio); |
| 238 | + } else { |
| 239 | + contained = container; |
| 240 | + } |
| 241 | + |
| 242 | + CGPoint origin = CGPointMake((container.width - contained.width) / 2.0f, (container.height - contained.height) / 2.0f); |
| 243 | + |
| 244 | + return CGRectWithOriginAndSize(origin, contained); |
| 245 | +} |
| 246 | + |
0 commit comments