码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-06-08 16:52:50      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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

 

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

标签:

原文地址:http://www.cnblogs.com/sixindev/p/4561193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!