码迷,mamicode.com
首页 > 其他好文 > 详细

实力开发技巧:点跟圆的测试

时间:2014-12-12 01:28:54      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:实例   开发技巧   点跟圆   

点跟圆的测试

/*

设计一个类Point2D,用来表示二维平台中的某个点

1)         属性

  1. a)      double _x

  2. b)     double _y

2)         方法

  1. a)      属性相应的setget方法

  2. b)     设计一个对象方法同时设置xy

  3. c)      设计一个对象方法计算跟其他点的距离

  4. d)     设计一个类方法计算两个点之间的距离

3)         提示

  1. a)      C语言的masth.h中有个函数:double powdouble ndouble m);计算nm次方

  2. b)     C语言的math.H中有个函数:double sqrtdouble n);计算根号n的值(对n进行开根)

*/

#import<Foundation/Foundation.h>

 

//

@interface Point2D : NSObject

{

       double _x;

       double _y;

}

 

// x值得setergeter方法

-        (void)setX : (double)x;

-        (double)x;

-         

// y值得setergeter方法

-        (void)setYX(double)y;

-        (double)y;

//同时设置xy

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

//计算跟其他点的距离

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

//计算跟其他点的距离

+ (double)distanceBetweenPoint1: (Ponint2D *)p1andPoint2 : (Point2D *)p2;

@end

@implementation Point2D

 

// y值得setergeter方法

-        (void)setX : (double)x

{

        _x = x;

}

-        (double)x

{

return _x;

}

// x值得setergeter方法

-        (void)setY : (double)y

{

        _y = y;

}

-        (double)y

{

return _y;

}

//同时设置xy

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

{

/*

方法一:

              _x=x;

                   _y=y;

方法二:

Self->_x=x;

Self->_y=y;

*/

[self setX:x];

[self setY:y];

}

//计算跟其他点的距离

-        (double)distanceWithOther :(Ponint2D *)other

{

//{(x1-x2)的平方+(y1-y2)的平方}开根

return [Point2D distanceBetweenPoint : self andPoint2D : other];

}

//计算跟其他点的距离

+ (double)distanceBetweenPoint1: (Ponint2D *)p1andPoint2 : (Point2D *)p2

{

       //{(x1-x2)的平方+(y1-y2)的平方}开根

       //x1-x2

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

       //(x1-x2)的平方

       double xDeltaPF = pow([p1 x]-[p2x],2);

       //y1-y2

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

       //(y1-y2)的平方

       double yDeltaPF = pow([p1 y]-[p2y],2);

       return sqrt(xDelta+yDelta);

}

 

@end

/*

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

  1. 1.    属性

  2. a)      Double _radius(半径)

  3. b)     Point2D *_point(圆心)

  4. 2.    方法

  5. a)      属性相应的setget方法

  6. b)     设计一个对象判断跟其他圆是否重叠(重叠放回yes,否则返回NO

  7. c)      设计一个类方法判断两个圆是否重叠(重叠返回yes,否则返回NO

*/

#import<Foundation/Foundation.h>

 

//

@interface Circle : NSObject

{

       //半径

       double       _radius;

       //圆心                                       

       Piont2D    *_point;

}

-         (void)setRadius:(double)radius;

-        (double)radius;

-         (void)setPoint:(Point 2D *)point;

-         (Point 2D *)point;

//返回值是BOOL类型一般都用is开头

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

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

@end

@implementation Circle

-        (void)setRadius:(double)radius

{

        _radius = radius;

}

-        (double)radius

{

return _radius;

}

-        (void)setPoint:(Point 2D *)point

{

        _piont = point;

}

-         (Point 2D *)point;

{

return _point;

}

-        (BOOL)isInteractWithOther:(Circle*)other

{

        //如果两个圆心的距离小于两个圆的半径和的时间重叠

//如果两个圆心的距离大于等于两个圆的半径和的时间不重叠

        Point2D *p1 = [self point];

        Point2D *p2 = [other Point];

double distance = [p1 distanceWithOther:p2];

double radiusSum = [self radius]+[other radius];

return distance<radiusSum

}

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

{

       return [ c1 isInteractWithOther : c2];

}

 

int main()

{

       //c1圆对象

       Circle *c1 = [Circle new];

       //c1圆半径设置

       [c1 setRadius:5];

       //创建c1圆心对象

       Point2D *p1=[Point2D new];

       //设置p1的坐标 10  15

       [p1 setX:10 andY:15];

       //设置圆心

       [c1 setPoint:p1];

       //更改c1圆心的x坐标

       [[c1 point] secX:10];

       //c2 圆对象

       Circle *c2 = [Circle new];

       //设置圆的半径

       [c2 setRadius : 2];

       //创建圆心对象

       Point2D *p2 = [Point2D new];

       [p2 setX:12 andY19];

       //设置圆心

       [c2 setPoint : p2]

       BOOL b1=[c1 isIntractWithOther:c2];

       NSLog(@”%d”,b1);

BOOL b2=[Circle  isIntractBetweenWithOther:c1 and c2];

       NSLog(@”%d”,b2);

 

       /*

       Point2D *p1=[Point2D new];

       [p1 setX:10 andY:15];

       Point2D *p2=[Point2D new];

[p1 setX:13 andY:19];

Double d1 = [p1 distanceWithOrher:p2];

Double d2 = [Point2D distanceBetweenPoint2D:p1 andPoint:2D];

       NSLog(@”%f”,d1);

NSLog(@”%f”,d2);

*/

       return 0;

}

注意:

只有利用黎明调用类方法的时候,不需要在类名后面写*

其他情况下,类名后面统一加上一个*

 

 


本文出自 “我成IT成长之路” 博客,请务必保留此出处http://jeason.blog.51cto.com/9704473/1588927

实力开发技巧:点跟圆的测试

标签:实例   开发技巧   点跟圆   

原文地址:http://jeason.blog.51cto.com/9704473/1588927

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!