forked from btrask/Sequential
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PGGeometry.m
257 lines (238 loc) · 9.04 KB
/
PGGeometry.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* Copyright © 2007-2009, The Sequential Project
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the the Sequential Project nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE SEQUENTIAL PROJECT ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE SEQUENTIAL PROJECT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#import "PGGeometry.h"
#include <tgmath.h>
#pragma mark NSPoint
NSPoint PGIntegralPoint(NSPoint aPoint)
{
return NSMakePoint(round(aPoint.x), round(aPoint.y));
}
NSPoint PGOffsetPointBySize(NSPoint aPoint, NSSize aSize)
{
return NSMakePoint(aPoint.x + aSize.width, aPoint.y + aSize.height);
}
NSPoint PGOffsetPointByXY(NSPoint aPoint, CGFloat x, CGFloat y)
{
return NSMakePoint(aPoint.x + x, aPoint.y + y);
}
NSSize PGPointDiff(NSPoint p1, NSPoint p2)
{
return NSMakeSize(p1.x - p2.x, p1.y - p2.y);
}
#pragma mark NSSize
NSSize PGScaleSizeByXY(NSSize size, CGFloat scaleX, CGFloat scaleY)
{
return NSMakeSize(size.width * scaleX, size.height * scaleY);
}
NSSize PGScaleSizeByFloat(NSSize size, CGFloat scale)
{
return PGScaleSizeByXY(size, scale, scale);
}
NSSize PGIntegralSize(NSSize s)
{
return NSMakeSize(round(s.width), round(s.height));
}
#pragma mark NSRect
NSRect PGCenteredSizeInRect(NSSize s, NSRect r)
{
return NSMakeRect(NSMidX(r) - s.width / 2, NSMidY(r) - s.height / 2, s.width, s.height);
}
BOOL PGIntersectsRectList(NSRect rect, NSRect const *list, NSUInteger count)
{
NSUInteger i = count;
while(i--) if(NSIntersectsRect(rect, list[i])) return YES;
return NO;
}
NSRect PGIntegralRect(NSRect r)
{
return NSMakeRect(round(NSMinX(r)), round(NSMinY(r)), round(NSWidth(r)), round(NSHeight(r)));
}
void PGGetRectDifference(NSRect diff[4], NSUInteger *count, NSRect minuend, NSRect subtrahend)
{
if(NSIsEmptyRect(subtrahend)) {
diff[0] = minuend;
*count = 1;
return;
}
NSUInteger i = 0;
diff[i] = NSMakeRect(NSMinX(minuend), NSMaxY(subtrahend), NSWidth(minuend), MAX(NSMaxY(minuend) - NSMaxY(subtrahend), 0.0f));
if(!NSIsEmptyRect(diff[i])) i++;
diff[i] = NSMakeRect(NSMinX(minuend), NSMinY(minuend), NSWidth(minuend), MAX(NSMinY(subtrahend) - NSMinY(minuend), 0.0f));
if(!NSIsEmptyRect(diff[i])) i++;
CGFloat const sidesMinY = MAX(NSMinY(minuend), NSMinY(subtrahend));
CGFloat const sidesHeight = NSMaxY(subtrahend) - MAX(NSMinY(minuend), NSMinY(subtrahend));
diff[i] = NSMakeRect(NSMinX(minuend), sidesMinY, MAX(NSMinX(subtrahend) - NSMinX(minuend), 0.0f), sidesHeight);
if(!NSIsEmptyRect(diff[i])) i++;
diff[i] = NSMakeRect(NSMaxX(subtrahend), sidesMinY, MAX(NSMaxX(minuend) - NSMaxX(subtrahend), 0.0f), sidesHeight);
if(!NSIsEmptyRect(diff[i])) i++;
*count = i;
}
NSRect PGScaleRect(NSRect r, CGFloat scaleX, CGFloat scaleY)
{
return NSMakeRect(NSMinX(r) * scaleX, NSMinY(r) * scaleY, NSWidth(r) * scaleX, NSHeight(r) * scaleY);
}
#pragma mark PGRectEdgeMask
NSSize PGRectEdgeMaskToSizeWithMagnitude(PGRectEdgeMask mask, CGFloat magnitude)
{
NSCParameterAssert(!PGHasContradictoryRectEdges(mask));
NSSize size = NSZeroSize;
if(mask & PGMinXEdgeMask) size.width = -magnitude;
else if(mask & PGMaxXEdgeMask) size.width = magnitude;
if(mask & PGMinYEdgeMask) size.height = -magnitude;
else if(mask & PGMaxYEdgeMask) size.height = magnitude;
return size;
}
NSPoint PGRectEdgeMaskToPointWithMagnitude(PGRectEdgeMask mask, CGFloat magnitude)
{
NSSize const s = PGRectEdgeMaskToSizeWithMagnitude(mask, magnitude);
return NSMakePoint(s.width, s.height);
}
NSPoint PGPointOfPartOfRect(NSRect r, PGRectEdgeMask mask)
{
NSPoint p;
switch(PGHorzEdgesMask & mask) {
case PGHorzEdgesMask:
case PGNoEdges: p.x = NSMidX(r); break;
case PGMinXEdgeMask: p.x = NSMinX(r); break;
case PGMaxXEdgeMask: p.x = NSMaxX(r); break;
}
switch(PGVertEdgesMask & mask) {
case PGVertEdgesMask:
case PGNoEdges: p.y = NSMidY(r); break;
case PGMinYEdgeMask: p.y = NSMinY(r); break;
case PGMaxYEdgeMask: p.y = NSMaxY(r); break;
}
return p;
}
PGRectEdgeMask PGPointToRectEdgeMaskWithThreshhold(NSPoint p, CGFloat threshhold)
{
CGFloat const t = fabs(threshhold);
PGRectEdgeMask direction = PGNoEdges;
if(p.x <= -t) direction |= PGMinXEdgeMask;
else if(p.x >= t) direction |= PGMaxXEdgeMask;
if(p.y <= -t) direction |= PGMinYEdgeMask;
else if(p.y >= t) direction |= PGMaxYEdgeMask;
return direction;
}
PGRectEdgeMask PGNonContradictoryRectEdges(PGRectEdgeMask mask)
{
PGRectEdgeMask r = mask;
if((r & PGHorzEdgesMask) == PGHorzEdgesMask) r &= ~PGHorzEdgesMask;
if((r & PGVertEdgesMask) == PGVertEdgesMask) r &= ~PGVertEdgesMask;
return r;
}
BOOL PGHasContradictoryRectEdges(PGRectEdgeMask mask)
{
return PGNonContradictoryRectEdges(mask) != mask;
}
#pragma mark PGPageLocation
PGRectEdgeMask PGReadingDirectionAndLocationToRectEdgeMask(PGPageLocation loc, PGReadingDirection dir)
{
NSCParameterAssert(PGPreserveLocation != loc);
BOOL const ltr = dir == PGReadingDirectionLeftToRight;
switch(loc) {
case PGHomeLocation: return PGMaxYEdgeMask | (ltr ? PGMinXEdgeMask : PGMaxXEdgeMask);
case PGEndLocation: return PGMinYEdgeMask | (ltr ? PGMaxXEdgeMask : PGMinXEdgeMask);
case PGEndTopLocation: return PGMaxYEdgeMask | (ltr ? PGMaxXEdgeMask : PGMinXEdgeMask);
}
return PGNoEdges;
}
#pragma mark PGOrientation
PGOrientation PGOrientationWithTIFFOrientation(NSUInteger orientation)
{
switch(orientation) {
case 2: return PGFlippedHorz;
case 3: return PGUpsideDown;
case 4: return PGFlippedVert;
case 5: return PGRotated90CCW | PGFlippedHorz;
case 6: return PGRotated90CW;
case 7: return PGRotated90CCW | PGFlippedVert;
case 8: return PGRotated90CCW;
default: return PGUpright;
}
}
PGOrientation PGAddOrientation(PGOrientation o1, PGOrientation o2)
{
PGOrientation n1 = o1, n2 = o2;
if(o1 & PGRotated90CCW && !(o2 & PGRotated90CCW)) n2 = ((o2 & PGFlippedHorz) >> 1) | ((o2 & PGFlippedVert) << 1);
PGOrientation r = n1 ^ n2;
if(o1 & PGRotated90CCW && o2 & PGRotated90CCW) r ^= PGUpsideDown;
return r;
}
NSString *PGLocalizedStringWithOrientation(PGOrientation orientation)
{
// TODO: Add these to the Localizable.strings files.
switch(orientation) {
case PGFlippedHorz: return NSLocalizedString(@"Flipped Horizontally", nil);
case PGUpsideDown: return NSLocalizedString(@"Upside Down", nil);
case PGFlippedVert: return NSLocalizedString(@"Flipped Vertically", nil);
case PGRotated90CCW | PGFlippedHorz: return NSLocalizedString(@"Rotated CCW & Flipped Horizontally", nil);
case PGRotated90CW: return NSLocalizedString(@"Rotated CW", nil);
case PGRotated90CCW | PGFlippedVert: return NSLocalizedString(@"Rotated CCW & Flipped Vertically", nil);
case PGRotated90CCW: return NSLocalizedString(@"Rotated CCW", nil);
default: return NSLocalizedString(@"Upright", nil);
}
}
#pragma mark PGInset
PGInset const PGZeroInset = {0.0f, 0.0f, 0.0f, 0.0f};
PGInset PGMakeInset(CGFloat minX, CGFloat minY, CGFloat maxX, CGFloat maxY)
{
return (PGInset){minX, minY, maxX, maxY};
}
PGInset PGScaleInset(PGInset i, CGFloat s)
{
return PGMakeInset(i.minX * s, i.minY * s, i.maxX * s, i.maxY * s);
}
PGInset PGInvertInset(PGInset inset)
{
return PGScaleInset(inset, -1);
}
NSPoint PGInsetPoint(NSPoint p, PGInset i)
{
return NSMakePoint(p.x + i.minX, p.y + i.minY);
}
NSSize PGInsetSize(NSSize s, PGInset i)
{
return NSMakeSize(MAX(0.0f, s.width - i.minX - i.maxX), MAX(0.0f, s.height - i.minY - i.maxY));
}
NSRect PGInsetRect(NSRect r, PGInset i)
{
return (NSRect){PGInsetPoint(r.origin, i), PGInsetSize(r.size, i)};
}
PGInset PGAddInsets(PGInset a, PGInset b)
{
return PGMakeInset(a.minX + b.minX, a.minY + b.minY, a.maxX + b.maxX, a.maxY + b.maxY);
}
#pragma mark Animation
NSTimeInterval PGUptime(void)
{
return (NSTimeInterval)UnsignedWideToUInt64(AbsoluteToNanoseconds(UpTime())) * 1e-9f;
}
CGFloat PGLagCounteractionSpeedup(NSTimeInterval *timeOfFrame, CGFloat desiredFramerate)
{
NSCParameterAssert(timeOfFrame);
NSTimeInterval const currentTime = PGUptime();
CGFloat const speedup = (CGFloat)(*timeOfFrame ? (currentTime - *timeOfFrame) / desiredFramerate : 1.0f);
*timeOfFrame = currentTime;
return speedup;
}