-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPPoint.m
363 lines (297 loc) · 8.36 KB
/
RPPoint.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
// RPPoint.m
// RasterPathCodingTask
//
// Created by Peter Megyesi on 2015. 06. 26..
// Copyright (c) 2015. petya. All rights reserved.
//
#import "RPPoint.h"
@implementation RPPoint
#pragma mark - member methods
#pragma mark -- init etc
- (instancetype)initWithX:(int)x withY:(int)y
{
self = [super init];
if (self) {
_x = x;
_y = y;
_weight = 1.0;
}
return self;
}
- (instancetype)initWithX:(int)x withY:(int)y withWeight:(float)w
{
self = [super init];
if (self) {
_x = x;
_y = y;
_weight = w;
}
return self;
}
- (BOOL)isEqualToRPPoint:(RPPoint *)object
{
if (!object) {
return NO;
}
if (object.x == self.x && object.y == self.y) {
return YES;
}
else {
return NO;
}
}
- (NSString *)toStringWithWeight:(BOOL)weightON
{
if (weightON){
return [[NSString alloc] initWithFormat:@"(%d,%d - %.1f)", self.x, self.y, self.weight];
}
else {
return [[NSString alloc] initWithFormat:@"(%d,%d)", self.x, self.y];
}
}
#pragma mark -- overrides
//override isEqual and hash for the comparison in Sets
- (BOOL)isEqual:(id)object
{
if (self == object) {
return YES;
}
if (!object || ![object isKindOfClass:[RPPoint class]]) {
return NO;
}
return [self isEqualToRPPoint:object];
}
- (NSUInteger)hash
{
return self.x ^ self.y;
}
#pragma mark -- transfer own coordinates between octants
- (void)transferPointToFirstOctantFromOctant:(Octant_t)fromOctant
{
//make copies first, weight stays the same
int x = self.x;
int y = self.y;
switch (fromOctant) {
//x,y
case octant_1:
break;
case octant_2:
self.x = y;
self.y = x;
break;
case octant_3:
self.x = y;
self.y = -x;
break;
case octant_4:
self.x = -x;
self.y = y;
break;
case octant_5:
self.x = -x;
self.y = -y;
break;
case octant_6:
self.x = -y;
self.y = -x;
break;
case octant_7:
self.x = -y;
self.y = x;
break;
case octant_8:
self.x = x;
self.y = -y;
break;
default:
break;
}
}
- (void)transferPointBackFromFirstOctantToOctant:(Octant_t)toOctant
{
//make copies first, weight stays the same
int x = self.x;
int y = self.y;
switch (toOctant) {
//x,y
case octant_1:
break;
case octant_2:
self.x = y;
self.y = x;
break;
//-y,x !differs from toFirstOctant
case octant_3:
self.x = -y;
self.y = x;
break;
case octant_4:
self.x = -x;
self.y = y;
break;
case octant_5:
self.x = -x;
self.y = -y;
break;
case octant_6:
self.x = -y;
self.y = -x;
break;
//y,-x !differs from toFirstOctant
case octant_7:
self.x = y;
self.y = -x;
break;
case octant_8:
self.x = x;
self.y = -y;
break;
default:
break;
}
}
#pragma mark - Class methods
#pragma mark -- calculation with Octants
+ (Octant_t)getOctantForVectorWithStartPoint:(RPPoint *)p0 endPoint:(RPPoint *)p1
{
/* octants:
*
* \3|2/
* 4\|/1
* ---+---
* 5/|\8
* /6|7\
*/
int nx = p1.x - p0.x;
int ny = p1.y - p0.y;
//first half +y
if (0 < ny) {
//first quadrant +x/+y
if (0 < nx) {
//first octant
if (ny <= nx) {
return octant_1;
}
//second octant
else {
return octant_2;
}
}
//second quadrant -x/+y
else {
//third octant
if (abs(nx) <= ny) {
return octant_3;
}
//fourth octant
else {
return octant_4;
}
}
}
//second half -y
else {
//third quadrant -x/-y
if (0 > nx) {
//fifth octant
if (abs(ny) <= abs(nx)) {
return octant_5;
}
//sixth octant
else {
return octant_6;
}
}
//fourth quadrant +x/-y
else {
//seventh octant
if (nx <= abs(ny)) {
return octant_7;
}
//eighth octant
else {
return octant_8;
}
}
}
}
#pragma mark -- I/O
+ (void)printAllRPPointsInSetToConsole:(NSSet *)set withWeight:(BOOL)weightON
{
NSArray *unsortedArr = [set allObjects];
NSSortDescriptor *sdX = [[NSSortDescriptor alloc] initWithKey:@"x" ascending:YES];
NSSortDescriptor *sdY = [[NSSortDescriptor alloc] initWithKey:@"y" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sdX, sdY, nil];
NSArray *sortedArray = [unsortedArr sortedArrayUsingDescriptors:sortDescriptors];
for (RPPoint *p in sortedArray) {
if (weightON) {
NSLog(@"(%d,%d - %.1f) ", p.x, p.y, p.weight);
}
else {
NSLog(@"(%d,%d) ", p.x, p.y);
}
}
}
+ (void)writeAllRPPointsInSet:(NSSet *)set withWeight:(BOOL)weightON intoFile:(NSString *)filePath withInputDataFirst:(NSString *)inputData
{
//try to open directory
NSFileManager *fileManager = [[NSFileManager alloc] init];
BOOL isDir;
BOOL exists = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
if (!exists) {
NSLog(@"Error, path to output file is not valid!");
}
else {
//sort
NSArray *unsortedArr = [set allObjects];
NSSortDescriptor *sdX = [[NSSortDescriptor alloc] initWithKey:@"x" ascending:YES];
NSSortDescriptor *sdY = [[NSSortDescriptor alloc] initWithKey:@"y" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sdX, sdY, nil];
NSArray *sortedArray = [unsortedArr sortedArrayUsingDescriptors:sortDescriptors];
if (isDir) {
NSMutableString *filePathExtended = [[NSMutableString alloc] initWithString:filePath];
//append out.txt
char lastChar = [filePath characterAtIndex:[filePath length] - 1];
if (lastChar == '/'){
[filePathExtended appendString:@"out.txt"];
}
else {
[filePathExtended appendString:@"/out.txt"];
}
//create file
[fileManager createFileAtPath:filePathExtended contents:nil attributes:nil];
//create contentstring to flush at once - @param:input data + calculated points
NSMutableString *allCalculatedPoints = [[NSMutableString alloc] init];
for (RPPoint *p in sortedArray) {
[allCalculatedPoints appendFormat:@"%@\n", [p toStringWithWeight:weightON]];
}
NSString *contentsToWrite = [[NSString alloc] initWithFormat:
@"%@%@",
inputData,
allCalculatedPoints];
// write into the new file
[contentsToWrite writeToFile:filePathExtended atomically:YES encoding:NSUTF8StringEncoding error:NULL];
}
}
}
///deprecated
/*
+ (FixedPoint)unwrapFixedPointFromRPP:(RPPoint *)p
{
FixedPoint fp;
fp.x = p.x;
fp.y = p.y;
return fp;
}
+ (RPPoint *)wrapFixedPointInRPP:(FixedPoint)p
{
return [[RPPoint alloc] initWithX:p.x withY:p.y];
}
+ (void)addFixedPoint:(FixedPoint)p toMutableSet:(NSMutableSet *)set
{
RPPoint *wrappedPoint = [RPPoint wrapFixedPointInRPP:p];
[set addObject:wrappedPoint];
}
*/
@end