我有一个有趣的数学问题,我无法弄清楚。
我正在建造一个用于安装磨损的表盘,需要根据时间计算出手的旋转角度。
通常情况下这很简单,但这里是踢球者:手不是时钟的核心。 让我们说我的钟面尺寸为10,10 我的分针支点位于6,6(左下角为0,0),我的时针位于4,4。
如何在任何给定的分钟内计算出角度,使得该点始终指向正确的分钟?
由于
答案 0 :(得分:1)
好的,在Nico的帮助下,我已经设法做出调整并得到一个有效的例子。
需要纳入的主要变化是改变输入的顺序以进行atan计算以及进行调整,因为android坚持将坐标系统颠倒过来。
请参阅下面的代码。
//minutes hand rotation calculation
int minute = mCalendar.get(Calendar.MINUTE);
float minutePivotX = mCenterX+minuteOffsetX;
//because of flipped coord system we take the y remainder of the full width instead
float minutePivotY = mWidth - mCenterY - minuteOffsetY;
//calculate target position
double minuteTargetX = mCenterX + mRadius * Math.cos(ConvertToRadians(minute * 6));
double minuteTargetY = mCenterY + mRadius * Math.sin(ConvertToRadians(minute * 6));
//calculate the direction vector from the hand's pivot to the target
double minuteDirectionX = minuteTargetX - minutePivotX;
double minuteDirectionY = minuteTargetY - minutePivotY;
//calculate the angle
float minutesRotation = (float)Math.atan2(minuteDirectionY,minuteDirectionX );
minutesRotation = (float)(minutesRotation * 360 / (2 * Math.PI));
//do this because of flipped coord system
minutesRotation = minutesRotation-180;
//if less than 0 add 360 so the rotation is clockwise
if (minutesRotation < 0)
{
minutesRotation = (minutesRotation+360);
}
//hours rotation calculations
float hour = mCalendar.get(Calendar.HOUR);
float minutePercentOfHour = (minute/60.0f);
hour = hour+minutePercentOfHour;
float hourPivotX = mCenterX+hourOffsetX;
//because of flipped coord system we take the y remainder of the full width instead
float hourPivotY = mWidth - mCenterY - hourOffsetY;
//calculate target position
double hourTargetX = mCenterX + mRadius * Math.cos(ConvertToRadians(hour * 30));
double hourTargetY = mCenterY + mRadius * Math.sin(ConvertToRadians(hour * 30));
//calculate the direction vector from the hand's pivot to the target
double hourDirectionX = hourTargetX - hourPivotX;
double hourDirectionY = hourTargetY - hourPivotY;
//calculate the angle
float hoursRotation = (float)Math.atan2(hourDirectionY,hourDirectionX );
hoursRotation = (float)(hoursRotation * 360 / (2 * Math.PI));
//do this because of flipped coord system
hoursRotation = hoursRotation-180;
//if less than 0 add 360 so the rotation is clockwise
if (hoursRotation < 0)
{
hoursRotation = (hoursRotation+360);
}
这还包括一个小辅助函数:
public double ConvertToRadians(double angle)
{
return (Math.PI / 180) * angle;
}
感谢您的帮助
答案 1 :(得分:0)
只需根据方向向量计算角度。
首先,计算目标位置。对于分针,这可能是:
targetX = radius * sin(2 * Pi / 60 * minutes)
targetY = radius * cos(2 * Pi / 60 * minutes)
然后计算从手的枢轴到目标的方向向量:
directionX = targetX - pivotX
directionY = targetY - pivotY
计算角度:
angle = atan2(directionX, directionY)