@interface CPoint : NSObject
@property double x,y;
-(id)initWithX: (double) x Y: (double) y;
@end
@implementation CPoint
-(id)initWithX: (double) x Y: (double) y {
self = [super init];
if (self) {
self.x = x;
self.y = y;
}
return self;
}
@end
@interface CPoint (CPointExtension)
- (double)distanceTo: (CPoint *) point;
@end
@implementation CPoint (CPointExtension)
- (double)distanceTo: (CPoint *) point {
double d1 = pow(self.x - point.x, 2);
double d2 = pow(self.y - point.y, 2);
return sqrt(d1 + d2);
}
@end
CPoint * p1 = [[CPoint alloc] initWithX:1 Y:2];
CPoint * p2 = [[CPoint alloc] initWithX:2 Y:3];
double distance = [p1 distanceTo:p2];
//distance is 1.4142