计算两点之间的距离(笔记)

这个是看一个视频上的,所的记录下来,供以后学习
//
//  main.m
//  CH00-练习
//
//  Created by Alfie on 15/3/10.
//  Copyright (c) 2015年 Alfie. All rights reserved.
//

#import <Foundation/Foundation.h>


// 点
@interface Point2D : NSObject
{
    double _x; // x值
    double _y; // y值
}

// x值的getter方法和setter方法
- (void)setX:(double)x;
- (double)x;

// y值的getter方法和setter方法
- (void)setY:(double)y;
- (double)y;

// 同时设置x和y
- (void) setX:(double)x andY:(double)y;

// 计算跟其它点的距离
- (double)distanceWithOther:(Point2D *)other;
// 计算两点之间的距离
+ (double) distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;

@end

@implementation Point2D

//x值的getter方法和setter
- (void)setX:(double)x
{
    _x = x;
}
- (double)x
{
    return_x;
}

// y值的getter方法和setter
- (void)setY:(double)y
{
    _y = y;
}
- (double)y
{
    return_y;
}

// 同时设置x和y
- (void) setX:(double)x andY:(double)y
{
//    _x = x;
//    _y = y;
    
//    self -> _x = x;
//    self -> _y = y;
    
    [selfsetX:x];
    [selfsetY:y];
}

// 计算跟其它点的距离
- (double)distanceWithOther:(Point2D *)other
{
    // (x1 - x2)的平方 +(y1 - y2)的平方平方根
    
   // return  [Point2D distanceBetweenPoint1:self andPoint2:other];
    
    // x1 - x2
    double xDelta = [selfx] - [other x];
    // (x1 - x2)的平方
    double xDeltaPF =pow(xDelta, 2);
    
    // y1 - y2
    double yDelta = [selfy] - [other y];
    // (y1 - y2)的平方
    double yDeltaPF =pow(yDelta,2);
    
    // 返回距离
    returnsqrt(xDeltaPF + yDeltaPF);
}

// 计算两点之间的距离
+ (double) distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
{
    
    return [p1distanceWithOther:p2];
   
    /*
    // x1 - x2
    double xDelta = [p1 x] - [p2 x];
    // (x1 - x2)的平方
    double xDeltaPF = pow(xDelta, 2);
    
    // y1 - y2
    double yDelta = [p1 y] - [p2 y];
    // (y1 - y2)的平方
    double yDeltaPF = pow(yDelta, 2);
    
    //返回距离
    return sqrt(xDeltaPF + yDeltaPF);
     */
}

@end

int main(int argc,const char * argv[]) {
    @autoreleasepool {
        Point2D *p1 = [Point2Dnew];
        // (10,15)
        [p1 setX:10andY:15];
        
        Point2D *p2 = [Point2Dnew];
        // (13,19)
        [p2 setX:13andY:19];
        
        double d1 = [p1distanceWithOther:p2];
        
        NSLog(@"%f",d1);
    }
    return0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值