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

来源:互联网 发布:软件试玩赚钱 编辑:程序博客网 时间:2024/05/16 01:17
这个是看一个视频上的,所的记录下来,供以后学习
////  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);     */}@endint 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
原创粉丝点击