常用距离计算函数

PointsCalculation.h文件

#import <Foundation/Foundation.h>

#import <CoreGraphics/CoreGraphics.h>

#ifndef max

#define max(a,b)            (((a) > (b)) ? (a) : (b))

#endif



#ifndef min

#define min(a,b)            (((a) < (b)) ? (a) : (b))

#endif


#define degreesToRadian(x) (M_PI * x / 180.0)

#define radiansToDegrees(x) (180.0 * x / M_PI)


@interface PointsCalculation : NSObject {

    

}

+(CGFloat)widthBetweenPoints:(CGPoint) first:(CGPoint) second;

+(CGFloat)heightBetweenPoints:(CGPoint) first:(CGPoint) second;


+(CGFloat)distanceBetweenPoints:(CGPoint) first:(CGPoint) second;

+(CGFloat)angleBetweenPoints:(CGPoint)left:(CGPoint) right;

+(CGFloat)angleBetweenThreePoints:(CGPoint)first:(CGPoint)second:(CGPoint)third;

+(CGFloat)angleBetweenLines:(CGPoint)line1Start:(CGPoint)line1End:(CGPoint)line2Start:(CGPoint)line2End;

+ (NSInteger)createRandomsizeValueInt:(NSInteger)fromInt toInt:(NSInteger)toInt;

+ (double)createRandomsizeValueFloat:(double)fromFloat toFloat:(double)toFloat;

@end

PointsCalculation.m文件

#import "PointsCalculation.h"

#include <math.h>



@implementation PointsCalculation

//!随机数的最大

#define ARC4RANDOM_MAX      0x100000000


+ (NSInteger)createRandomsizeValueInt:(NSInteger)fromInt toInt:(NSInteger)toInt

{

    if (toInt < fromInt)

    {

        return toInt;

    }

    if (toInt == fromInt)

    {

        return fromInt;

    }

    NSInteger randVal = arc4random() % (toInt - fromInt + 1) + fromInt;

    return randVal;

}


+ (double)createRandomsizeValueFloat:(double)fromFloat toFloat:(double)toFloat

{

    if (toFloat < fromFloat)

    {

        return toFloat;

    }

    if (toFloat == fromFloat)

    {

        return fromFloat;

    }

    double randVal = ((double)arc4random() / ARC4RANDOM_MAX) * (toFloat - fromFloat) + fromFloat;

    return randVal;

}

+(CGFloat)widthBetweenPoints:(CGPoint) first:(CGPoint) second

{

CGFloat deltaX = second.x - first.x;

if (deltaX > 0) {

return deltaX;

}

else {

return deltaX*(-1);

}

}

+(CGFloat)heightBetweenPoints:(CGPoint) first:(CGPoint) second

{

CGFloat deltaY = second.y - first.y;

if (deltaY > 0) {

return deltaY;

}

else {

return deltaY*(-1);

}

}

+(CGFloat)distanceBetweenPoints:(CGPoint) first:(CGPoint) second

{

CGFloat deltaX = second.x - first.x;

CGFloat deltaY = second.y - first.y;

return sqrt(deltaX*deltaX + deltaY*deltaY );

}



+(CGFloat)angleBetweenPoints:(CGPoint)left:(CGPoint) right{

CGFloat height = right.y - left.y;

CGFloat width = left.x - right.x;

CGFloat rads =0;

if(width!=0){

        if (width < 0) {

            rads = -atan(height/width);

        }

        else

        {

            rads = -atan(height/width) + 3.1415926;

        }

}

    else

    {

        if (height < 0) {

            rads = -1.5708;

        }

        else

        {

            rads = 1.5708;

        }

    }

    

    //NSLog(@"angle is: %.2f",radiansToDegrees(rads));

//return radiansToDegrees(rads);

return rads;

//degs = degrees(atan((top - bottom)/(right - left)))

}



+(CGFloat)angleBetweenThreePoints:(CGPoint)first:(CGPoint)second:(CGPoint)third{

CGFloat height1 = second.y - first.y;

CGFloat width1 = first.x - second.x;

CGFloat rads1=0;

if(width1!=0){

rads1 = atan(height1/width1);

}

CGFloat height2 = third.y - first.y;

CGFloat width2 = third.x - second.x;

CGFloat rads2 =0;

if(width2!=0){

rads2 = atan(height2/width2);

}

CGFloat rads=rads2-rads1;

return radiansToDegrees(rads);

}


+(CGFloat)angleBetweenLines:(CGPoint)line1Start:(CGPoint)line1End:(CGPoint)line2Start:(CGPoint)line2End {

CGFloat a = line1End.x - line1Start.x;

CGFloat b = line1End.y - line1Start.y;

CGFloat c = line2End.x - line2Start.x;

CGFloat d = line2End.y - line2Start.y;

CGFloat rads = acos(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));

return radiansToDegrees(rads);

}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值