加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

设计一个类Circle,用来表示二维平面中的圆

(2014-04-27 06:07:28)
标签:

it

分类: OC作业

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------


 6.设计一个类Circle,用来表示二维平面中的圆

 1> 属性

 * double radius (半径)

 * Point2D *point (圆心)

 

 2> 方法

 * 属性相应的setget方法

 * 设计一个对象方法判断跟其他圆是否相交(重叠返回YES,否则返回NO

 * 设计一个类方法判断两个圆是否相交(重叠返回YES,否则返回NO


#import

#import


//

@interface Point2D : NSObject

{

    double _x; // x

    double _y; // y

}

// x值的gettersetter

- (void)setX:(double)x;

- (double)x;


// y值的gettersetter

- (void)setY:(double)y;

- (double)y;


// 同时设置xy

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


// 计算跟其他点的距离

- (double)distanceWithOther:(Point2D *)other;


// 计算两个点之间的距离

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

@end


@implementation Point2D

// x值的gettersetter

- (void)setX:(double)x

{

    _x = x;

}

- (double)x

{

    return _x;

}


// y值的gettersetter

- (void)setY:(double)y

{

    _y = y;

}

- (double)y

{

    return _y;

}


// 同时设置xy

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

{

    // 1种思路

    // _x = x;

    // _y = y;

    

    // 2种思路

[self setX:x];

[self setY:y];

}


// 计算跟其他点的距离

- (double)distanceWithOther:(Point2D *)other

{

    // 不要再傻乎乎算一遍了,直接调用类方法即可

    return [Point2D distanceBetweenPoint1:self andPoint2:other];

}


// 计算两个点之间的距离

+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2

{

    // 两点距离公式:( (x1-x2)的平方 + (y1-y2)的平方 )开根

    

    // x1-x2

    double xDelta = [p1 x] - [p2 x];

    // (x1-x2)的平方

    double xDeltaPingFang = pow(xDelta, 2);

    

    // y1-y2

    double yDelta = [p1 y] - [p2 y];

    // (y1-y2)的平方

    double yDeltaPingFang = pow(yDelta, 2);

    

    return sqrt(xDeltaPingFang + yDeltaPingFang);

}

@end


//

@interface Circle : NSObject

{

    double _radius; // 半径

    Point2D *_point; // 圆心

}


// 半径的gettersetter

- (void)setRadius:(double)radius;

- (double)radius;


// 圆心的gettersetter

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

- (Point2D *)point;



// 跟其他圆是否重叠(重叠返回YES,否则返回NO

- (BOOL)isInteractWithOther:(Circle *)other;

// 判断两个圆是否重叠(重叠返回YES,否则返回NO

+ (BOOL)isInteractBetweenCircle1:(Circle *)circle1 andCircle2:(Circle *)circle2;


@end


@implementation Circle

// 半径的gettersetter

- (void)setRadius:(double)radius

{

    _radius = radius;

}

- (double)radius

{

    return _radius;

}


// 圆心的gettersetter

- (void)setPoint:(Point2D *)point

{

    _point = point;

}

- (Point2D *)point

{

    return _point;

}


// 跟其他圆是否重叠(重叠返回YES,否则返回NO

- (BOOL)isInteractWithOther:(Circle *)other

{

    return [Circle isInteractBetweenCircle1:self andCircle2:other];

}


// 判断两个圆是否重叠(重叠返回YES,否则返回NO

+ (BOOL)isInteractBetweenCircle1:(Circle *)circle1 andCircle2:(Circle *)circle2

{

    // 如果两个圆心的距离 >= 两个圆的半径和,就不重叠

    // 如果两个圆心的距离 <</span> 两个圆的半径和,就重叠

    

    // 两个圆心

    Point2D *point1 = [circle1 point];

    Point2D *point2 = [circle2 point];

    // 两个圆心的距离

    double distance = [point1 distanceWithOther:point2];

    

    // 半径和

    double radiusSum = [circle1 radius] + [circle2 radius];

    

    return distance < radiusSum;

}

@end


int main()

{

    Circle *c1 = [Circle new];

    // 设置半径

    [c1 setRadius:2];

    // 设置圆心

    Point2D *p1 = [Point2D new];

    [p1 setX:10 andY:10];

    [c1 setPoint:p1];

    

    Circle *c2 = [Circle new];

    // 设置半径

    [c2 setRadius:2];

    // 设置圆心

    Point2D *p2 = [Point2D new];

    [p2 setX:13 andY:14];

    [c2 setPoint:p2];

    

    // 圆心距离是 半径和是 所以不重叠

    BOOL b1 = [c1 isInteractWithOther:c2];

    

    BOOL b2 = [Circle isInteractBetweenCircle1:c1 andCircle2:c2];

    

    NSLog(@"%d %d", b1, b2);

    

    return 0;

}

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有