计算2点之间的距离

计算2点之间的距离:
#import "Point2D.h"
#import <math.h>

@interface Point2D ()
{
    double _x;
    double _y;
}

//set和get方法

-(void)setX:(double)x;
-(double)x;

-(void)setY:(double)y;
-(double)y;

-(void)setX:(double)x andsetY:(double)y;


//计算距离方法

-(double)distanceWithOtherPoint:(Point2D *)point;

+(double)distanceWithPoint1:(Point2D *)point1 andPoint2:(Point2D *)point2;

@end


@implementation Point2D

//x
-(void)setX:(double)x{
    _x = x;
}

-(double)x{
    return _x;
}

//y
-(void)setY:(double)y{
    _y = y;
}

-(double)y{
    return _y;
}

-(void)setX:(double)x andsetY:(double)y{
    [self setX:x];
    [self setY:y];
}

-(double)distanceWithOtherPoint:(Point2D *)point{
    //X^2
    double xDelta = [self x]-[point x];
    double xDeltaPF  = pow(xDelta, 2);
    
    //Y^2
    double yDelta = [self y]-[point y];
    double yDeltaPF = pow(yDelta, 2);
    
    return sqrt(xDeltaPF+yDeltaPF);//开根号
}


+(double)distanceWithPoint1:(Point2D *)point1 andPoint2:(Point2D *)point2{
    return [point2 distanceWithOtherPoint:point1];
}



int main(int argc, const char * argv[]) {
    
    Point2D * p1 = [Point2D new];
    Point2D * p2 = [Point2D new];
    
    
    [p1 setX:10 andsetY:90];
    [p2 setX:80 andsetY:67];
    
    double  distance = [Point2D distanceWithPoint1:p1 andPoint2:p2];
    NSLog(@"distance=%f",distance);
    
    return 0;
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值