这是我跟着老师的代码敲的,6.设计一个类Circle,用来表示二维平面中的圆
1> 属性
* double radius (半径)
* Point2D *point (圆心)
2> 方法
* 属性相应的set和get方法
* 设计一个对象方法判断跟其他圆是否相交(重叠返回YES,否则返回NO)
* 设计一个类方法判断两个圆是否相交(重叠返回YES,否则返回NO)
然后敲完之后就5个警告,看不出来在哪。。。谁帮帮忙~~~~我只是稍微按自己的理解改动了点成员变量和方法中的变量名。。。
#import <Foundation/Foundation.h>
#import <math.h>
@interface Point2D : NSObject
{
double _x;
double _y;
}
- (void)setX:(double)x;
- (double)x;
- (void)setY:(double)y;
- (double)y;
- (void)setX:(double)x andY:(double)y;
- (double)distanceWithOther :(Point2D *)other;
+ (double)distanceWithPonit1:(Point2D *)point1 andPoint2:(Point2D *)point2;
@end
@implementation Point2D
- (void)setX:(double)x
{
_x = x;
}
- (double)x
{
return _x;
}
- (void)setY:(double)y
{
_y = y;
}
- (double)y
{
return _y;
}
- (void)setX:(double)x andY:(double)y
{
_x = x;
_y = y;
}
- (double)distanceWithOther :(Point2D *)other
{
return [Point2D distanceWithPonit1:self andPoint2:other];
}
+ (double)distanceWithPonit1:(Point2D *)point1 andPoint2:(Point2D *)point2
{
double xDelta = [point1 x] - [point2 x];
double xDeltaPingFang = pow(xDelta, 2);
double yDelta = [point1 y] - [point2 y];
double yDeltaPingFang = pow(yDelta, 2);
return sqrt(xDeltaPingFang + yDeltaPingFang);
}
@end
@interface Circle : NSObject
{
double _radius;
Point2D *_point;
}
- (void)setRadius:(double)radius;
- (double)radius;
- (void)setPoint:(Point2D *)point;
- (Point2D *)point;
- (BOOL)isInteraceWithOther:(Circle *)other;
+ (BOOL)isInteraceBetweenCircle1:(Circle *)circle1 andCircle2:(Circle *)circle2;
@end
@implementation Circle
- (void)setRadius:(double)radius
{
_radius = radius;
}
- (double)radius
{
return _radius;
}
- (void)setPoint:(Point2D *)point
{
_point = point;
}
- (Point2D *)point
{
return _point;
}
- (BOOL)isInteraceWithOther:(Circle *)other
{
return [Circle isInteraceBetweenCircle1:self andCircle2:other];
}
+ (BOOL)isInteraceBetweenCircle1:(Circle *)circle1 andCircle2:(Circle *)circle2
{
Point2D *point1 = [circle1 point];
Point2D *point2 = [circle2 point];
double distance = [point1 distanceWithOther:point2];
double radiusSum = [circle1 radius] + [circle2 radius];
return distance < radiusSum;
}
@end
int main ()
{
Circle *c1 = [Circle new];
[c1 setRadius:2];
Point2D *p1 = [Point2D new];
[p1 setX:10 andY:10];
[c1 setPoint :p1];
Circle *c2 = [Circle new];
[c2 setRadius:2];
Point2D *p2 = [Point2D new];
[p2 setX:13 andY:14];
[c2 Point2D :p2];
BOOL b1 = [c1 isInteractWithOther:c2];
BOOL b2 = [Circle isInteractBetweenCircle1:c1 andCircle2:c2];
NSLog(@"%d %d", b1, b2);
return 0 ;
} |