//
// main.m
// 06作业
//
// Created by apple on 15/8/9.
// Copyright (c) 2015年 金诗妍. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <math.h>
// Point2D类
@interface Point2D : NSObject
{
double _x; //x值
double _y; //y值
}
// x的setter和getter
- (void)setX:(double)x;
- (double)x;
// y的setter和getter
- (void)setY:(double)y;
- (double)y;
// 一个类对象方法同时设置x和y
- (void)setX:(double)x andY:(double)y;
// 计算跟其他点的距离
- (double)distanceWithOther:(Point2D *)other;
// 计算两点间距离
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;
@end
/**********方法**********/
@implementation Point2D
// x的setter和getter
- (void)setX:(double)x
{
_x = x;
}
- (double)x
{
return _x;
}
//y的setter和getter
- (void)setY:(double)y
{
_y = y;
}
- (double)y
{
return _y;
}
// 同时设置x和y
- (void)setX:(double)x andY:(double)y
{
[self setX:x];
[self setY:y];
}
// 计算与另外一个点的距离
- (double)distanceWithOther:(Point2D *)other
{
double xDelta = _x - [other x];
double xDeltaPF = pow(xDelta,2);
double yDelta = _y - [other y];
double yDeltaPF = pow(yDelta,2);
return sqrt(xDeltaPF+yDeltaPF);
}
// 计算两点间距离
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
{
return [p1 distanceWithOther:p2]; // 类方法调用对象方法
}
@end
// 圆类
@interface Circle : NSObject
{
double _radius;
Point2D *_point;
}
//半径的setter和getter
- (void)setRadius:(double)radius;
- (double)radius;
//Point2D的setter and getter
- (void)setPoint:(Point2D *)point;
- (Point2D *)point;
// 判断与其他圆是否重叠,是则返回YES否则返回NO
- (BOOL)isInteractWithOther:(Circle *)other;
// 判断两个圆是否重叠,是则返回YES否则返回NO
+ (BOOL)isInteractBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2;
@end
@implementation Circle
// 半径的setter和getter
- (void)setRadius:(double)radius
{
_radius = radius;
}
- (double)radius
{
return _radius;
}
//Point2D的setter and getter
- (void)setPoint:(Point2D *)point
{
_point = point;
}
- (Point2D *)point
{
return _point;
}
// 判断与其他圆是否重叠,是则返回YES否则返回NO
- (BOOL)isInteractWithOther:(Circle *)other
{
Point2D *p1 = [self point];
Point2D *p2 = [other point];
// 两点间距离
double distance = [p1 distanceWithOther:p2];
// 半径和
double radiusSum = [self radius] + [other radius];
if(distance < radiusSum)
{
return 1;
}
else
{
return 0;
}
return distance < radiusSum;
}
// 判断两个圆是否重叠,是则返回YES否则返回NO
+ (BOOL)isInteractBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2
{
return [c1 isInteractWithOther:c2];
}
@end
int main()
{
// 圆对象
Circle *c1 = [Circle new];
[c1 setRadius:5];
// 创建圆心对象
Point2D *p1 = [Point2D new];
[p1 setX:10 andY:15];
// 设置圆心
[c1 setPoint:p1];
[[c1 point] setX:15]; // 圆心变更为(15,15)
// 圆对象
Circle *c2 = [Circle new];
[c2 setRadius:2];
// 创建圆心对象
Point2D *p2 = [Point2D new];
[p2 setX:12 andY:19];
// 设置圆心
[c2 setPoint:p2];
/*对象方法
BOOL juage = [c1 isInteractWithOther:c2];
NSLog(@"%d",juage);
return 0;
*/
// 类方法
BOOL juage = [Circle isInteractBetweenCircle1:c1 andCircle2:c2];
NSLog(@"%d",juage);
}
此次debug心塞死了。