这真的是一道数学题。您正在寻找弧长(等于弧度乘以半径的角度)
你的函数不能计算弧长,因为它不知道圆在哪里(定义一个圆需要 3 个点)。
- (CGFloat)calculateShortestArcDistanceBetween:(CGPoint)point1 point2:(CGPoint)point2 center:(CGPoint)center
{
CGFloat dx1 = point1.x - center.x;
CGFloat dy1 = point1.y - center.y;
CGFloat dx2 = point2.x - center.x;
CGFloat dy2 = point2.y - center.y;
CGFloat angle1 = atan2f( dy1, dx1 );
CGFloat angle2 = atan2f( dy2, dx2 );
CGFloat angle = angle1 - angle2;
if( angle > M_PI ) angle -= M_PI * 2;
else if( angle < -M_PI ) angle += M_PI * 2;
return fabs( angle ) * sqrtf( dx1 * dx1 + dy1 * dy1 );
}
- (CGFloat)calculateDirectedArcDistanceBetween:(CGPoint)point1 point2:(CGPoint)point2 center:(CGPoint)center
{
CGFloat dx1 = point1.x - center.x;
CGFloat dy1 = point1.y - center.y;
CGFloat dx2 = point2.x - center.x;
CGFloat dy2 = point2.y - center.y;
CGFloat angle1 = atan2f( dy1, dx1 );
CGFloat angle2 = atan2f( dy2, dx2 );
CGFloat angle = angle1 - angle2;
if( angle < 0 ) angle += M_PI * 2;
return angle * sqrtf( dx1 * dx1 + dy1 * dy1 );
}
大多数棘手的问题是确保范围正确(atan2
给出从 -pi 到 +pi 的值,所以在得到两个角度之间的差异后,我们必须重新归一化它们)