#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);
}
谢谢阅读!
网友评论