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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Harvey_ios 中级黑马   /  2015-5-9 18:19  /  751 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

设计Circle类判断两圆是否相交,之前这道题中,我用了继承的方法,其实这里根本就不用继承,Circle不能继承Point2D,因为圆不是点,之前学了继承知道,Cat和Dog可以继承Animal,因为狗和猫都是动物,所以不是看到有相似的地方就用继承。
这里是重新编写之后的代码:
  1. /**
  2. 6.设计一个类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. // 点
  14. @interface Point2D : NSObject
  15. {
  16.     double _x; // x值
  17.     double _y; // y值
  18.    
  19. }
  20. // x值的getter和setter
  21. - (void)setX:(double)x;
  22. - (double)x;

  23. // y值的getter和setter
  24. - (void)setY:(double)y;
  25. - (double)y;

  26. // 同时设置x和y
  27. - (void)setX:(double)x andY:(double)y;

  28. // 计算跟其他点的距离
  29. - (double)distanceWithOther:(Point2D *)other;

  30. // 计算两个点之间的距离
  31. + (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;



  32. @end

  33. @implementation Point2D
  34. // x值的getter和setter
  35. - (void)setX:(double)x
  36. {
  37.     _x = x;
  38. }
  39. - (double)x
  40. {
  41.     return _x;
  42. }

  43. // y值的getter和setter
  44. - (void)setY:(double)y
  45. {
  46.     _y = y;
  47. }
  48. - (double)y
  49. {
  50.     return _y;
  51. }
  52. // 同时设置x和y
  53. - (void)setX:(double)x andY:(double)y
  54. {
  55.     // 第1种思路
  56.     // _x = x;
  57.     // _y = y;
  58.    
  59.     // 第2种思路
  60.         [self setX:x];
  61.         [self setY:y];
  62. }

  63. // 计算跟其他点的距离
  64. - (double)distanceWithOther:(Point2D *)other
  65. {
  66.     // 不要再傻乎乎算一遍了,直接调用类方法即可
  67.     return [Point2D distanceBetweenPoint1:self andPoint2:other];
  68. }

  69. // 计算两个点之间的距离
  70. + (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
  71. {
  72.     // 两点距离公式:( (x1-x2)的平方 + (y1-y2)的平方 )开根
  73.    
  74.     // x1-x2
  75.     double xDelta = [p1 x] - [p2 x];
  76.     // (x1-x2)的平方
  77.     double xDeltaPingFang = pow(xDelta, 2);
  78.    
  79.     // y1-y2
  80.     double yDelta = [p1 y] - [p2 y];
  81.     // (y1-y2)的平方
  82.     double yDeltaPingFang = pow(yDelta, 2);
  83.    
  84.     return sqrt(xDeltaPingFang + yDeltaPingFang);
  85. }
  86. @end


  87. // Circle 类
  88. @interface Circle : NSObject
  89. {
  90.     double _radius;  // 半径
  91.     Point2D *_point;  // 圆心
  92. }
  93. // radius值的setter和getter方法
  94. - (void)setRadius:(double)radius;
  95. - (double)radius;
  96. - (void)setPoint:(Point2D *)point;
  97. - (Point2D *)point;
  98. - (BOOL)xjWithOtherCircle :(Circle *)other;
  99. + (BOOL)xjBetweenCircle1: (Circle *)c1 andCircle2:(Circle *)c2;
  100. @end
  101. @implementation Circle
  102. - (void)setRadius:(double)radius
  103. {
  104.     _radius = radius;
  105. }
  106. - (double)radius
  107. {
  108.     return _radius;
  109. }
  110. - (void)setPoint:(Point2D *)point
  111. {
  112.     _point = point;
  113. }
  114. - (Point2D *)point;
  115. {
  116.     return _point;
  117. }
  118. - (BOOL)xjWithOtherCircle :(Circle *)other
  119. {
  120.     return [Circle xjBetweenCircle1:self andCircle2:other];
  121. }
  122. + (BOOL)xjBetweenCircle1: (Circle *)c1 andCircle2:(Circle *)c2
  123. {
  124.     // 判断两圆是否相交:两圆心的距离和半径之和比较大小
  125.     //1、如果圆心距离大于半径之和,则不想交
  126.     //2、如果圆心半径小于或等于半径之和,则相交
  127.     Point2D *p1 = [c1 point];
  128.     Point2D *p2 = [c2 point];
  129.    
  130.     double distance = [p1 distanceWithOther: p2];
  131.     double sumRadius = [c1 radius]+[c2 radius]  ;  
  132.     return sumRadius > distance;
  133.         }
  134. @end
  135. int main()
  136. {
  137.     Circle *c1 = [Circle new];
  138.     Point2D *p1 = [Point2D new];
  139.     [c1 setPoint:p1];  // 通过set方法使圆心指针指向Point2D类型对象
  140.     [p1 setX:10 andY:10];  // 设置点得坐标为(10,10)
  141.     [c1 setRadius:3];  // 设置c1的半径的值为3
  142.    
  143.     Circle *c2 = [Circle new];
  144.      Point2D *p2 = [Point2D new];
  145.      [c2 setPoint:p2];
  146.     [p2 setX:13 andY:14]; // 设置点得坐标是 (13,14)
  147.     [c2 setRadius:1]; // 设置c2的半径是1
  148.    
  149.    
  150.     // double d1 = [p1 distanceWithOther:p2];
  151.     //
  152.     //double d2 = [Point2D distanceBetweenPoint1:p1 andPoint2:p2];
  153.    
  154.     NSLog(@"%d", [Circle xjBetweenCircle1:c1 andCircle2:c2]); // 不相交就输出0,相交就输出1
  155.    
  156.     return 0;
  157. }
复制代码

这里半径之和是4,圆心距离是5,所以不相交,输出结果是0.
  1. 2015-05-09 17:57:43.446 a.out[899:707] 0
复制代码




0 个回复

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