A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 诺微_ 中级黑马   /  2014-12-20 23:35  /  1654 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*
  2. 要求:设计一个类Circle,用来表示二维平面中的圆
  3. 1> 属性
  4. * double _radius (半径)
  5. * Point2D *_point (圆心)

  6. 2> 方法
  7. * 属性相应的set和get方法
  8. * 设计一个对象方法判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
  9. * 设计一个类方法判断两个圆是否重叠(重叠返回YES,否则返回NO)
  10. */

  11. #import <Foundation/Foundation.h>
  12. #import <math.h>

  13. @interface Point2D : NSObject
  14. {
  15.     double _x;
  16.     double _y;
  17. }

  18. // x值的setter和getter的声明
  19. - (void)setX:(double)x;
  20. - (double)x;

  21. // y值的setter和getter的声明
  22. - (void)setY:(double)y;
  23. - (double)y;

  24. // 1.对象方法:计算跟其他点的距离的声明
  25. - (double)distanceOfOther:(Point2D *)other;

  26. // 2.类方法:计算两个点间的距离的声明
  27. + (double)distanceWithP1:(Point2D *)p1 andP2:(Point2D *)p2;
  28. @end

  29. @implementation Point2D
  30. // x值的setter和getter的实现
  31. - (void)setX:(double)x
  32. {
  33.     _x = x;
  34. }
  35. - (double)x
  36. {
  37.     return _x;
  38. }

  39. // y值的setter和getter的实现
  40. - (void)setY:(double)y
  41. {
  42.     _y = y;
  43. }
  44. - (double)y
  45. {
  46.     return _y;
  47. }

  48. // 1.对象方法:计算跟其他点的距离的实现
  49. - (double)distanceOfOther:(Point2D *)other
  50. {
  51.     double xPingFang = pow(([self x] - [other x]), 2.0); // x差值的平方
  52.     double yPingFang = pow(([self y] - [other y]), 2.0); // y差值的平方
  53.    
  54.     return sqrt(xPingFang + yPingFang); // x差值的平方 + y差值的平方 再开方
  55. }

  56. // 2.类方法:计算两个点间的距离的实现
  57. + (double)distanceWithP1:(Point2D *)p1 andP2:(Point2D *)p2
  58. {
  59.     return [p1 distanceOfOther:p2];
  60. }
  61. @end

  62. @interface Circle : NSObject
  63. {
  64.     double _radius; // (半径)
  65.     Point2D *_point; //(圆心)
  66. }

  67. // 半径的setter和getter的声明
  68. - (void)setRadius:(double)radius;
  69. - (double)radius;

  70. // 圆心的setter和getter的声明
  71. - (void)setPoint:(Point2D *)point;
  72. - (Point2D *)point;

  73. /*
  74. 返回值是BOOL类型的,方法名一般以is开头
  75. */
  76. // 对象方法判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
  77. - (BOOL)isInteractOfOther:(Circle *)other;

  78. // 类方法判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
  79. + (BOOL)isInteractCircle1:(Circle *)circle1 WithCircle2:(Circle *)circle2;
  80. @end

  81. @implementation Circle
  82. // 半径的setter和getter的实现
  83. - (void)setRadius:(double)radius
  84. {
  85.     _radius = radius;
  86. }
  87. - (double)radius
  88. {
  89.     return _radius;
  90. }

  91. // 圆心的setter和getter的实现
  92. - (void)setPoint:(Point2D *)point
  93. {
  94.     _point = point;
  95. }
  96. - (Point2D *)point
  97. {
  98.     return _point;
  99. }


  100. // 对象方法判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
  101. - (BOOL)isInteractOfOther:(Circle *)other
  102. {
  103.     return [Point2D distanceWithP1:[self point] andP2:[other point]] < [self radius] + [other radius];
  104. }
  105.             
  106. // 类方法判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
  107. + (BOOL)isInteractCircle1:(Circle *)circle1 WithCircle2:(Circle *)circle2
  108. {
  109.     return [circle1 isInteractOfOther:circle2];
  110. }
  111. @end

  112. int main()
  113. {
  114.     Circle *c1 = [Circle new];
  115.     [c1 setRadius:1]; // 1.调用圆的setRadius方法设置半径
  116.    
  117.     Point2D *p1 = [Point2D new]; // 先创建一个点对象
  118.     [p1 setX:10];
  119.     [p1 setY:15];
  120.    
  121.     [c1 setPoint:p1]; // 2.调用圆的setPoint方法设置圆心(点对象),注意设置圆心(点对象)时,必须的先新建一个点对象,再调用圆的setPoint方法将已经建好的点对象赋值进去
  122.    
  123.     Circle *c2 = [Circle new];
  124.     [c2 setRadius:3];
  125.    
  126.     Point2D *p2 = [Point2D new];
  127.     [p2 setX:13];
  128.     [p2 setY:19];
  129.    
  130.     [c2 setPoint:p2];
  131.    
  132.     BOOL b1 = [c1 isInteractOfOther:c2];
  133.    
  134.     NSLog(@"%d", b1);
  135.    
  136.     return 0;
  137. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马