判断两个圆是否重叠

判断两个圆是否重叠,代码如下:

//
//  main.m
//  CH00-练习
//
//  Created by Alfie on 15/3/10.
//  Copyright (c) 2015年 Alfie. All rights reserved.
//

#import <Foundation/Foundation.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;
    
    [self setX:x];
    [self setY:y];
}

// 计算跟其它点的距离
- (double)distanceWithOther:(Point2D *)other
{
    // (x1 - x2)的平方 + (y1 - y2)的平方 平方根
    
   // return  [Point2D distanceBetweenPoint1:self andPoint2:other];
    
    // x1 - x2
    double xDelta = [self x] - [other x];
    // (x1 - x2)的平方
    double xDeltaPF = pow(xDelta, 2);
    
    // y1 - y2
    double yDelta = [self 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

/*
设计一个类 Circle,用来表示二维平面中的圆
1> 属性
 * double _radian  (半径)
 * Point2D *_point (圆心)
                    
2> 方法
 * 属性相应的 set 和 get 方法
 * 设计一个对象判断跟其他圆是否重叠 (重叠返回YES,否则返回NO)
 * 设计一个类方法判断两上圆是否重叠 (重叠返回YES, 否则返回NO)
*/

// 圆
@interface Circle : NSObject
{
    // 半径
    double _radius;
    // 圆心
//    double _x;
//    double _y;
    Point2D *_point;
}

// 半径的 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) isInteractBetweenCircle1:(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 YES;
//    }
//    else
//    {
//        return NO;
//    }
    
//    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(int argc, const char * argv[]) {
    @autoreleasepool {
        // 圆对象
        Circle *c1 = [Circle new];
        // 设置半径
        [c1 setRadius:5];
        // 创建圆心对象
        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);
        
        BOOL b1 = [c1 isInteractWithOther:c2];
        
        NSLog(@"%d",b1);
        
        
        /**
        Point2D *p1 = [Point2D new];
        // (10,15)
        [p1 setX:10 andY:15];
        
        Point2D *p2 = [Point2D new];
        // (13,19)
        [p2 setX:13 andY:19];
        
        double d1 = [p1 distanceWithOther:p2];
        
        NSLog(@"%f",d1);
         **/
         
    }
    return 0;
}
只有利用类名调用类方法的时候,不需要在类名后面写 *
其它情况下,类名后面必须加上 *



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值