黑马程序员——点与圆的方法设计

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net
培训</a>、期待与您交流! -------



通过oc基本知识的学习,在看懂老师点与圆习题编程方法的基础上,通过自己的理解重写程序,并在编程的过程中不断总结与回顾学过的知识点,达到温故而知新的目的。

xcode编程的过程中,碰到不少的问题,因为xcode有实时编译的功能,因此能及时作出改正,主要代码都作了注释,方便自己与别人日后查看。

 

习题:圆的设计。

1设计一个Poing2D类,包含double xdouble y两个成员变量。

2、设计xygettersetter,设计一个对象方法同时设置xy,设计一个对象方法计算跟其他点的距离,设计一个对象方法计算两点之间的距离。

3math.h中的两个函数,double powdouble n, double m,计算nm次方,double sqrtdouble n),计算根号n的值。

4、设计一个Circle类,用来表示二维平面中的圆。包含 _radius, *point两个成员变量。

5、设置_radiu*pointsettergetter,设计一个对象方法判断跟其他圆是否重叠,设计一个对象方法判断两个圆是否重叠。

 

程序如下:

 

#import<Foundation/Foundation.h>

#import<math.h>

 

@interface Point2D : NSObject  //创建一个二维点对象,类的生命

{

    double _x;  // x

    double _y; // y

}

 

// xysetter

- (void)setX:(double)x;

- (void)setY:(double)y;

 

- (void)setX:(double)x andSetY:(double)y; // 同时设置xy

 

// xygetter

- (double)x;

- (double)y;

 

- (double)distanceWithOtherPoint:(Point2D *)other;   //  该点与另一点的距离

- (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2; // 两点间的距离

 

@end

 

 

 

@implementation Point2D    // 类的实现

 

- (void)setX:(double)x

{

    _x  = x;

}

- (void)setY:(double)y

{

    _y = y;

}

 

- (double)x

{

    return _x;

}

- (double)y

{

    return _y;

}

 

- (double)distanceWithOtherPoint:(Point2D *)other

{

//    double xDistance = [self x] - [other x];

//    double yDistance = [self y] - [other y];

//    double pfh = pow(xDistance, 2) + pow(yDistance,2);

//    return sqrt(pfh);

    return [self distanceBetweenPoint1:self andPoint2:other];

}

 

 

- (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;

{

    double xDistance = [p1 x] - [p2 x];  // 两点x方向的距离

    double yDistance = [p1 y] - [p2 y];  // 两点y方向多距离

    double pfh = pow(xDistance, 2) + pow(yDistance,2);  // 两点距离差的平方

    return sqrt(pfh); // 距离平方开根号即距离作为返回值

}

 

- (void)setX:(double)x andSetY:(double)y

{

    [self setX:x];  //等同于 self->_x = x;   _x = x;

    [self setY:y];

}

 

@end

 

 

 

@interface Circle: NSObject  // circle类的声明

{

    double _radius;

    Point2D *_point;  //组合

}

 

 

- (void)setRadius:(double)radius;

- (double)radius;

 

- (void)setPiont:(Point2D *)point;

- (Point2D *)point;

 

- (BOOL)isCircleCompleWithOtherCircle:(Circle *)circle;

- (BOOL)isCircle1:(Circle *)c1 compleWithCircle2:(Circle *)c2;

@end

 

 

@implementation Circle  // circle类的实现

 

- (void)setRadius:(double)radius

{

    _radius = radius;

}

- (double)radius

{

    return _radius;

}

 

- (void)setPiont:(Point2D *)point

{

    _point = point;

}

- (Point2D *)point

{

    return _point;

}

 

- (BOOL)isCircleCompleWithOtherCircle:(Circle *)circle

{

//    Point2D *p1 = [self point];

//    Point2D *p2 = [circle point];

//    double distance = [p1 distanceWithOtherPoint:p2];

//    double pointDistance = [self radius] + [circle radius];

//    return distance < pointDistance;

    return  [self isCircle1:self compleWithCircle2:circle];

    

}

 

- (BOOL)isCircle1:(Circle *)c1 compleWithCircle2:(Circle *)c2

{

    Point2D *p1 = [c1 point];

    Point2D *p2 = [c2 point];

    double distance = [p1 distanceWithOtherPoint:p2];

    double pointDistance = [c1 radius]+[c2 radius];

    return distance < pointDistance;

    }

@end

 

 

int main()

{

    Point2D *p1 = [Point2D new];

    Point2D *p2 = [Point2D new];

    [p1 setX:5 andSetY:5];

    [p2 setX:5 andSetY:5];

    

    Circle *c1 = [Circle new];

    Circle *c2 = [Circle new];

    

   [c1 setPiont:p1];

   [c2 setPiont:p2];

   

    [c1 setRadius:3];

    [c2 setRadius:4];

    

    BOOL i = [c1 isCircleCompleWithOtherCircle:c2]; // 重叠返回1,不重叠返回0

    NSLog(@"%d",i);

    return 0;

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值