【问题标题】:Distance between two points on circle in specific direction特定方向圆上两点之间的距离
【发布时间】:2013-05-04 23:43:54
【问题描述】:

我的圆圈上有两个点,(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 );
}

【问题讨论】:

  • 你是指沿弧线的距离吗?您的函数看起来不像那样做。你所拥有的只是两点之间的线性距离 - 不涉及圆。顺时针/逆时针没有区别。

标签: objective-c math distance geometry


【解决方案1】:

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

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

【讨论】:

  • 小备注:hypotf 是一个方便计算向量长度的函数。
  • @MartinR 对此一无所知。它是便携式的吗?快速的互联网搜索并没有告诉我。
  • 我在圆上的两个点是 {180.109, 155.487} 和 {79.2966, 18.2268} 我在两个方向上都得到了弧长 221.778336 -221.778336。这是不对的!我错过了什么吗?
  • 修改功能如下。 - (CGFloat)calculateDistanceBetween:(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); CGF浮动角度=角度1-角度2;返回角度 * sqrtf( dx1 * dx1 + dy1 * dy1 ); }
  • 你还没有说你的中心点在哪里。另请参阅我的更新答案,其中包含 if( angle &lt; 0 )... 行(定向角度需要)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 2011-03-14
  • 1970-01-01
  • 2019-01-23
  • 2012-07-17
  • 1970-01-01
相关资源
最近更新 更多