计算两点之间的距离,两点之间的斜率(角度)--秀清

时间:2021-10-13 14:38:13
//
//  ViewController.m
//  勾股定理
//
//  Created by 张秀清 on 15/6/8.
//  Copyright (c) 2015年 张秀清. All rights reserved.
//

#import "ViewController.h"

//角度转弧度
#define degreesToradian(x) (M_PI*x/180.0)

//弧度转角度
#define radiansToDegrees(x) (180.0*x/M_PI)

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGPoint point1 = CGPointMake(100, 100);
    CGPoint point2 = CGPointMake(200, 200);
    
    CGFloat distance = [self distanceBetweenPoints:point1 point2:point2];
    NSLog(@"%f",distance);

    CGFloat angle    = [self angleBetweenPoints:point1 point2:point2];
    NSLog(@"%f",angle);
    
//    CGFloat x = (point1.x + distance) * cos(angle);
//    CGFloat y = (point1.y + distance) * sin(angle);
//    
//    NSLog(@"%f---%f",x,y);
}

#pragma mark - 计算两点间的距离
-(CGFloat)distanceBetweenPoints:(CGPoint)point1 point2:(CGPoint)point2
{
    CGFloat distanceX = point2.x - point1.x;
    CGFloat distanceY = point2.y - point1.y;
    
    return sqrt(distanceX*distanceX + distanceY*distanceY);
}

#pragma mark - 计算两点间的角度
-(CGFloat)angleBetweenPoints:(CGPoint)point1 point2:(CGPoint)point2
{
    CGFloat height = point2.y - point1.y;
    CGFloat width  = point1.x - point2.x;
    
    CGFloat rads   = atan(height/width);
    
    return radiansToDegrees(rads);
    
}


@end