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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. 6.设计一个类Circle,用来表示二维平面中的圆
  2. 1> 属性
  3. * double radius (半径)
  4. * Point2D *point (圆心)

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

  9. #import <Foundation/Foundation.h>
  10. #import <math.h>

  11. @interface Point2D : NSObject
  12. {
  13.     double _x;
  14.     double _y;
  15.    
  16. }
  17. - (void)setX:(double)x;
  18. - (double)x;

  19. - (void)setY:(double)y;
  20. - (double)y;

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

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

  23. + (double)distanceWithPonit1:(Point2D *)point1 andPoint2:(Point2D *)point2;
  24. @end

  25. @implementation Point2D
  26. - (void)setX:(double)x
  27. {
  28.     _x = x;
  29. }
  30. - (double)x
  31. {
  32.     return _x;
  33. }

  34. - (void)setY:(double)y
  35. {
  36.     _y = y;
  37. }
  38. - (double)y
  39. {
  40.     return _y;
  41. }

  42. - (void)setX:(double)x andY:(double)y
  43. {
  44.     _x = x;
  45.     _y = y;
  46. }

  47. - (double)distanceWithOther :(Point2D *)other
  48. {
  49.     return  [Point2D distanceWithPonit1:self andPoint2:other];
  50. }


  51. + (double)distanceWithPonit1:(Point2D *)point1 andPoint2:(Point2D *)point2
  52. {
  53.     double xDelta = [point1 x] - [point2 x];
  54.     double xDeltaPingFang = pow(xDelta, 2);
  55.    
  56.     double yDelta = [point1 y] - [point2 y];
  57.     double yDeltaPingFang = pow(yDelta, 2);
  58.    
  59.     return sqrt(xDeltaPingFang + yDeltaPingFang);

  60. }
  61. @end

  62. @interface Circle : NSObject
  63. {
  64.     double _radius;
  65.     Point2D *_point;
  66. }
  67. - (void)setRadius:(double)radius;
  68. - (double)radius;

  69. - (void)setPoint:(Point2D *)point;
  70. - (Point2D *)point;

  71. - (BOOL)isInteraceWithOther:(Circle *)other;
  72. + (BOOL)isInteraceBetweenCircle1:(Circle *)circle1 andCircle2:(Circle *)circle2;
  73. @end

  74. @implementation Circle
  75. - (void)setRadius:(double)radius
  76. {
  77.     _radius = radius;
  78. }
  79. - (double)radius
  80. {
  81.     return _radius;
  82. }

  83. - (void)setPoint:(Point2D *)point
  84. {
  85.     _point = point;
  86. }
  87. - (Point2D *)point
  88. {
  89.     return _point;
  90. }
  91. - (BOOL)isInteraceWithOther:(Circle *)other
  92. {
  93.     return [Circle isInteraceBetweenCircle1:self andCircle2:other];
  94. }
  95. + (BOOL)isInteraceBetweenCircle1:(Circle *)circle1 andCircle2:(Circle *)circle2
  96. {
  97.     Point2D *point1 = [circle1 point];
  98.     Point2D *point2 = [circle2 point];
  99.    
  100.     double distance = [point1 distanceWithOther:point2];
  101.     double radiusSum = [circle1 radius] + [circle2 radius];
  102.    
  103.     return distance < radiusSum;
  104. }
  105. @end
  106. int main ()
  107. {
  108.     Circle *c1 = [Circle new];
  109.     [c1 setRadius:2];
  110.    
  111.     Point2D *p1 = [Point2D new];
  112.     [p1 setX:10 andY:10];
  113.     [c1 setPoint :p1];
  114.    
  115.     Circle *c2 = [Circle new];
  116.     [c2 setRadius:2];
  117.    
  118.     Point2D *p2 = [Point2D new];
  119.     [p2 setX:13 andY:14];
  120.     [c2 Point2D :p2];
  121.    
  122.     BOOL b1 = [c1 isInteractWithOther:c2];
  123.    
  124.     BOOL b2 = [Circle isInteractBetweenCircle1:c1 andCircle2:c2];
  125.    
  126.     NSLog(@"%d %d", b1, b2);
  127.    
  128.     return 0 ;

  129. }

复制代码

2 个回复

倒序浏览
第141 和 143  你那个单词isInteract  你在定义的时候写的是isInterace  你把两个t改成e
139行  Point2D改成 setPoint
我这边看就没错误和警告了啊
回复 使用道具 举报
陌生爱人 发表于 2014-10-10 15:38
第141 和 143  你那个单词isInteract  你在定义的时候写的是isInterace  你把两个t改成e
139行  Point2D改 ...

好的,谢谢、还是我比较粗心了。太感谢了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马