美文网首页
计算两点距离、点间角度、线间角度

计算两点距离、点间角度、线间角度

作者: CHADHEA | 来源:发表于2017-03-10 11:27 被阅读0次

    #define kRadians(RADIAN) (180.0 * RADIAN / M_PI)

    //.h

    //点间间距

    + (CGFloat)distanceFromPoint:(CGPoint)starPoint to:(CGPoint)endPoint;

    //点间夹角

    + (CGFloat)angleFromPoint:(CGPoint)starPoint to:(CGPoint)endPoint;

    //线间夹角

    + (CGFloat)angleBetweenLinesFrom:(CGPoint)firstLineStarPoint endPoint:(CGPoint)firstLineEndPoint to:(CGPoint)secondLineStarPoint endPoint:(CGPoint)secondLineEndPoint;

    //.m

    + (CGFloat)distanceFromPoint:(CGPoint)starPoint to:(CGPoint)endPoint {

    CGFloat deltaX = endPoint.x - starPoint.x;

    CGFloat deltaY = endPoint.y - starPoint.y;

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

    }

    + (CGFloat)angleFromPoint:(CGPoint)starPoint to:(CGPoint)endPoint {

    CGFloat height = endPoint.y - starPoint.y;

    CGFloat width = starPoint.x - endPoint.x;

    CGFloat rads = atan(height/width);

    return kRadians(rads);

    }

    + (CGFloat)angleBetweenLinesFrom:(CGPoint)firstLineStarPoint endPoint:(CGPoint)firstLineEndPoint to:(CGPoint)secondLineStarPoint endPoint:(CGPoint)secondLineEndPoint {

    CGFloat a = firstLineEndPoint.x - firstLineStarPoint.x;

    CGFloat b = firstLineEndPoint.y - firstLineStarPoint.y;

    CGFloat c = secondLineEndPoint.x - secondLineStarPoint.x;

    CGFloat d = secondLineEndPoint.y - secondLineStarPoint.y;

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

    return kRadians(rads);

    }

    谢谢阅读!

    相关文章

      网友评论

          本文标题:计算两点距离、点间角度、线间角度

          本文链接:https://www.haomeiwen.com/subject/orpngttx.html