一、练习题两点间距离,和判断两个圆是否相交
/*
5.设计一个类Point2D,用来表示二维平面中某个点
1> 属性
* double _x
* double _y
2> 方法
* 属性相应的set和get方法
* 设计一个对象方法同时设置x和y
* 设计一个对象方法计算跟其他点的距离
* 设计一个类方法计算两个点之间的距离
3> 提示
* C语言的math.h中有个函数:double pow(double n, double m); 计算n的m次方
* C语言的math.h中有个函数:double sqrt(double n); 计算根号n的值(对n进行开根)
*/
#import <Foundation/Foundation.h>
#import <math.h>
//点
@interface Point2D : NSObject
{
double _x; // x值
double _y; // y值
}
//x值的getter和setter
- (void)setX:(double)x;
- (double)x;
//y值的getter和setter
- (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值的getter和setter
- (void)setX:(double)x
{
_x = x;
}
- (double)x
{
return _x;
}
//y值的getter和setter
- (void)setY:(double)y
{
_y = y;
}
- (double)y
{
return _y;
}
//同时设置x值和y的值
-(void)setX:(double)x andY:(double)y
{
/*
_x = x;
_y = y;
*/
/*
self->_x = x;
self->_y = y;
*/
//建议下面做法,可以在set的时候进行过滤
[self setX:x];
[self setY:y];
}
//计算跟其他点的距离
-(double)distanceWithOther:(Point2D *)other
{
//((x1-x2)的平方+(y1-y2)的平方) 开根
//return [Point2D distanceBetweenPoint1:self andPoint2:other];
//x1-x2
double xDelta = _x - [other x];
//(x1-x2)的平方
double xDeltaPF = pow(xDelta, 2);
//y1-y2
double yDelta = _y - [other y];
//(y1-y2)的平方
double yDeltaPF = pow(yDelta, 2);
//返回距离
return sqrt(xDeltaPF + yDeltaPF);
}
//计算两个点之间的距离
+(double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
{
return [p1 distanceWithOther:p2];
/*
//x1-x2
double xDelta = [p1 x] -[p2 x];
//(x1-x2)的平方
double xDeltaPF = pow(xDelta 2);
//y1-y2
double yDelta = [p1 y] -[p2 y];
//(y1-y2)的平方
double yDeltaPF = pow(yDelta 2);
//返回距离
return sqrt(xDeltaPF + yDeltaPF);
*/
}
@end
/**
6.设计一个类Circle,用来表示二维平面中的圆
1> 属性
* double radius (半径)
* Point2D *point (圆心)
2> 方法
* 属性相应的set和get方法
* 设计一个对象方法判断跟其他圆是否相交(重叠返回YES,否则返回NO)
* 设计一个类方法判断两个圆是否相交(重叠返回YES,否则返回NO)
*/
@interface Circle :NSObject
{
//半径
double _radius;
//圆心
Point2D *_point; //默认_point=0,即为空值,所以不能直接使用。需要先传个对象过来
}
//半径的getter和setter
- (void)setRadius:(double)radius;
- (double)radius;
//圆心的getter和setter
- (void)setPoint:(Point2D *)point;
- (Point2D *)point;
//对象方法判断跟其他圆是否相交(重叠返回YES,否则返回NO)
//返回值是BOOL类型的,方法名一般以is开头
- (BOOL)isInteractWithOther:(Circle *)other;
//类方法判断两个圆是否相交(重叠返回YES,否则返回NO)
+ (BOOL)isInteractBetweenWithCircle1:(Circle *)c1 andCircle2:(Circle *)c2;
@end
@implementation Circle
//半径的getter和setter
- (void)setRadius:(double)radius
{
_radius = radius;
}
- (double)radius
{
return _radius;
}
//圆心的getter和setter
- (void)setPoint:(Point2D *)point
{
_point = point;
}
- (Point2D *)point
{
return _point;
}
//对象方法判断跟其他圆是否相交(重叠返回YES,否则返回NO)
//返回值是BOOL类型的,方法名一般以is开头
- (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)isInteractBetweenWithCircle1:(Circle *)c1 andCircle2:(Circle *)c2
{
return [c1 isInteractWithOther:c2];
}
@end
int main()
{
//每一个圆对象
Circle *c1 = [Circle new];
//设置半径
[c1 setRadius:5];
//必须先创建圆心对象(因为[c1 point]默认是空)
Point2D *p1 = [Point2D new];
[p1 setX:10 andY:15];
//先设置圆心
[c1 setPoint:p1];
[[c1 point] setX:15];
//c1 半径:5 圆心:(15,15)
//第二个圆对象
Circle *c2 = [Circle new];
[c2 setRadius:2];
//创建圆心对象
Point2D *p2 = [Point2D new];
[p2 setX:12 andY:19];
//设置圆心
[c2 setPoint:p2];
//c2 半径:2 圆心:(12 ,19)
//圆心距离是5 半径和是7
BOOL b1 =[c1 isInteractWithOther:c2];
NSLog(@"%d",b1);
/*
Point2D *p1 = [Point2D new];
[p1 setX:10 andY:15];
Point2D *p2 = [Point2D new];
[p2 setX:13 andY:19];
double d1 = [p1 distanceWithOther:p2];
NSLog(@"%f", d1);
*/
return 0;
}
二、多文件开发
1.习惯把类的声明和实现分开存放:
.h成员变量、方法的声明
.m方法的实现
如果想使用某个类,只需要加入.h文件即可如下:
#import <Point2D.h>
#import <Circle.h>
2.多文件开发,编译可以分开编译,
但链接的时候要多个文件一起,否则是出错的
cc Circle.o Point2D.o 00-第6题作业.o -framework Foundation
3.在xcode 中点run会把所有文件编译,链接运行
三、xcode 技巧
1.command +q 退出xcode所有打开文件。
2.xcode中在.m文件中 写方法实现,
可以先敲- setAge 这样的,就会出现下方自动提示
- (void)setAge(int)age
3.xcode特有的注释方法
#pragma mark - 姓名的set方法
- (void)setName:(NSString *)name
{
_name = name;
}
其中的 - 可产生横线方便区分,查找。4.如果代码没有错,就看链接的错
点左上角!号图标