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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 流风124 中级黑马   /  2015-4-6 22:02  /  1081 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

今天学习OC视频时遇到的一道题,感觉挺好的,对于面向对象的理解很有帮助,跟大家分享一下
6.设计一个类Circle,用来表示二维平面中的圆
1> 属性
* double _radius (半径)
* Point2D *_point (圆心)

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

下面是我自己练习的代码
  1. #import <Foundation/Foundation.h>
  2. #import <math.h>

  3. //计算距离之差函数
  4. double Delta(double d1,double d2)
  5. {
  6.     return d1 - d2;
  7. }
  8. // 类:点的声明
  9. @interface Point2D : NSObject
  10. {
  11.     // 点的二维坐标
  12.     double _x;
  13.     double _y;
  14. }
  15. // x坐标的set方法声明
  16. - (void)setX:(double)x;
  17. // x坐标的get方法声明
  18. - (double)x;

  19. // y坐标的set方法声明
  20. - (void)setY:(double)y;
  21. // y坐标的get方法声明
  22. - (double)y;

  23. //计算跟其他点的距离
  24. - (double)distanceWithOther:(Point2D *)otherpoint;

  25. //计算两个点之间的距离
  26. + (double)distanceBetweenPoint1:(Point2D *)p1 AndPoint2:(Point2D *)p2;
  27. @end
  28. // 类:点的实现
  29. @implementation Point2D
  30. // x坐标的set方法实现
  31. - (void)setX:(double)x
  32. {
  33.     _x = x;
  34. }
  35. // x坐标的get方法实现
  36. - (double)x
  37. {
  38.     return _x;
  39. }

  40. // y坐标的set方法实现
  41. - (void)setY:(double)y
  42. {
  43.     _y = y;
  44. }
  45. // y坐标的get方法实现
  46. - (double)y
  47. {
  48.     return _y;
  49. }

  50. //计算跟其他点的距离
  51. - (double)distanceWithOther:(Point2D *)otherpoint
  52. {
  53.     return [Point2D distanceBetweenPoint1:self AndPoint2:otherpoint];
  54. }

  55. //计算两个点之间的距离
  56. + (double)distanceBetweenPoint1:(Point2D *)p1 AndPoint2:(Point2D *)p2
  57. {
  58.    
  59.     double delta_x = Delta([p1 x],[p2 x]);
  60.     double delta_y = Delta([p1 y],[p2 y]);
  61.     double d = sqrt( pow(delta_x,2) + pow(delta_y,2) );
  62.     return d;
  63. }
  64.         
  65.         
  66. @end
  67.         

  68. // 类:圆的声明
  69. @interface Circle : NSObject
  70. {
  71.     double _radius; // 半径
  72.     Point2D *_point; // 圆心
  73. }
  74. // 半径的set方法声明
  75. - (void)setRadius:(double)radius;
  76. // 半径的get方法声明
  77. - (double)radius;

  78. // 圆心的set方法声明
  79. - (void)setPoint2DX:(double)x AndY:(double)y;
  80. // 圆心的get方法声明
  81. - (Point2D *)point;

  82. //对象方法:判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
  83. - (BOOL)overlapWithOtherCircle:(Circle *)otherCircle;

  84. //类方法判断两个圆是否重叠(重叠返回YES,否则返回NO)
  85. + (BOOL)overlapCircle1:(Circle *)c1 WithCircle2:(Circle *)c2;

  86. @end
  87. // 类:圆的实现
  88. @implementation Circle
  89. // 半径的set方法实现
  90. - (void)setRadius:(double)radius
  91. {
  92.     _radius = radius;
  93. }
  94. // 半径的get方法实现
  95. - (double)radius
  96. {
  97.     return _radius;
  98. }

  99. // 圆心的set方法实现
  100. - (void)setPoint2DX:(double)x AndY:(double)y
  101. {
  102.     Point2D *p = [Point2D new];
  103.     _point = p;
  104.     [_point setX:x];
  105.     [_point setY:y];
  106. }
  107. // 圆心的get方法实现
  108. - (Point2D *)point
  109. {
  110.     [_point x];
  111.     [_point y];
  112.     return _point;
  113. }

  114. //对象方法:判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
  115. - (BOOL)overlapWithOtherCircle:(Circle *)otherCircle
  116. {
  117.     return [Circle overlapCircle1:self WithCircle2:otherCircle];
  118. }

  119. //类方法判断两个圆是否重叠(重叠返回YES,否则返回NO)
  120. + (BOOL)overlapCircle1:(Circle *)c1 WithCircle2:(Circle *)c2
  121. {
  122.     double sumRadiusDistance = [c1 radius] + [c2 radius];//半径之和
  123.     double deltaCenterDistance = [[c1 point] distanceWithOther:[c2 point]];//圆心之间的距离
  124.     if (deltaCenterDistance >= 0 && deltaCenterDistance < sumRadiusDistance)
  125.     {
  126.         return YES;
  127.     }
  128.     return NO;
  129. }


  130. @end


  131. int main()
  132. {
  133.     Circle *circle1 = [Circle new];
  134.     [circle1 setRadius:1];
  135.     [circle1 setPoint2DX:0 AndY:0];
  136.    
  137.     Circle *circle2 = [Circle new];
  138.     [circle2 setRadius:1];
  139.     [circle2 setPoint2DX:1.9 AndY:0];
  140.    
  141.     if ([circle1 overlapWithOtherCircle:circle2] == YES)
  142.     {
  143.         NSLog(@"两圆重叠");
  144.     }else
  145.     {
  146.         NSLog(@"不重叠");
  147.     }
  148.    
  149.     return 0;
  150. }
复制代码

2 个回复

倒序浏览
厉害!代码写的很工整,格式也很好!
回复 使用道具 举报
弥风冻雪 发表于 2015-4-6 22:04
厉害!代码写的很工整,格式也很好!

谢谢,完全是按照老师的要求来的,希望能从现在开始就养成良好的编程习惯啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马