objective c - Distance between two points on circle in specific direction -
i have 2 points on circle, (x1y1) , (x2y2). how calculate distance between them in counter , clock wise direction. currently, using following formula calculate distance between 2 points on circle. please suggest.
- (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 ); }
this maths question really. you're looking arc length (which equals angle in radians multiplied radius)
your function stands can't calculate arc length, because doesn't know circle (it takes 3 points define circle).
- (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 ); }
most of tricky bit making sure ranges correct (atan2
gives values -pi +pi, after getting difference between 2 angles must re-normalise them)
Comments
Post a Comment