0

我的圈子上有两个点,(x1y1)和(x2y2)。如何计算它们之间在逆时针和顺时针方向上的距离。目前,我正在使用以下公式来计算圆上两点之间的距离。请建议。

- (CGFloat)calculateDistanceBetween:(CGPoint)point1 point2:(CGPoint)point2
{
    CGFloat dx = point2.x - point1.x;
    CGFloat dy = point2.y - point1.y;
    return sqrt(dx*dx + dy*dy );
}
4

1 回答 1

2

这真的是一道数学题。您正在寻找弧长(等于弧度乘以半径的角度)

你的函数不能计算弧长,因为它不知道圆在哪里(定义一个圆需要 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 的值,所以在得到两个角度之间的差异后,我们必须重新归一化它们)

于 2013-05-04T15:32:09.680 回答