/
CPoint.m
89 lines (61 loc) · 1.9 KB
/
CPoint.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
//
// Point.m
// DBScan
//
// Created by Christian Vogel on 02.03.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "CPoint.h"
@implementation CPoint
@synthesize coordinates = _coordinates;
- (id)init {
self = [super init];
if (self)
_coordinates = [NSMutableArray array];
return self;
}
- (void)addCoordinate:(float)coordinate {
[_coordinates addObject:[NSNumber numberWithFloat:coordinate]];
}
- (float)coordinateAtPosition:(int)position {
if ((position < 0) || (position > [_coordinates count]))
[NSException raise:@"Invalid input." format:@"position %i is invalid", position];
return [[_coordinates objectAtIndex:position] floatValue];
}
- (NSUInteger)dimension {
return _coordinates.count;
}
- (NSString *)description {
NSString *result = @"";
for (int i = 0; i < _coordinates.count; i++) {
result = [result stringByAppendingString:[[_coordinates objectAtIndex:i] description]];
if (i < (_coordinates.count - 1))
result = [result stringByAppendingString:@","];
}
return result;
}
+ (id)pointWithCoordinates:(NSArray *)coordinates {
CPoint *copy = [CPoint new];
for (int i = 0; i < coordinates.count; i++) {
float coordinate = [[coordinates objectAtIndex:i] floatValue];
[copy addCoordinate:coordinate];
}
return copy;
}
- (BOOL)isEqual:(id)object {
if (object == self)
return YES;
if (!object || ![object isKindOfClass:[self class]])
return NO;
CPoint *other = object;
if (_coordinates.count != other.coordinates.count)
return NO;
for (int i = 0; i < _coordinates.count; i++) {
float selfAttr = [[_coordinates objectAtIndex:i] floatValue];
float otherAttr = [[[other coordinates] objectAtIndex:i] floatValue];
if (selfAttr != otherAttr)
return NO;
}
return YES;
}
@end