Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting angle of circle to time in minutes

I have been trying to develop a clock control with a help of a circle. So if the circle is at 360 degree its 3'O clock, if its 90 degree then it will be 12'o clock. I can't figure out what formula can be used to find the time in hh:mm format if I have the angle of the circle.

eg.

9'o clock is 180 degree. 3'o clock is 360 degree 12'o clock is 90 degree and so on.

like image 900
Vinita Avatar asked Dec 03 '25 21:12

Vinita


2 Answers

There's probably a more concise way to do this, but here's what you need to do.

  1. Calculate the decimal value of the time, using 3 - (1/30) * (angle mod 360)
  2. If the result is less than zero, add 12.
  3. Take the integer portion to use as the hours, replacing a value of zero with 12.
  4. Take the remainder (the decimal portion of the calculation from step 1) and convert to minutes by multiplying by 60.
  5. Format your hours and minutes, padding with leading zeros if less than 10, and combine them with the colon.

An example implementation in java:

public static String convertAngleToTimeString(float angle) {
    String time = "";
    float decimalValue = 3.0f - (1.0f/30.0f) * (angle % 360);
    if (decimalValue < 0)
        decimalValue += 12.0f;

    int hours = (int)decimalValue;
    if (hours == 0)
        hours = 12;
    time += (hours < 10 ? "0" + hours: hours) + ":";
    int minutes = (int)(decimalValue * 60) % 60; 
    time += minutes < 10 ? "0" + minutes: minutes;
    return time;
}
like image 136
Tap Avatar answered Dec 05 '25 19:12

Tap


You could check out these formula's on Wikipedia, they might help: http://en.wikipedia.org/wiki/Clock_angle_problem

But other than that, you could do something simple like, degrees/360 * 12. + offset.

For example, if we set that 360 degrees is 12 o'clock, and you had 90 degrees. You would get. 90/360 * 12 = 3. So that would be 3'o clock. Then you could add your offset, which in your case is 3 so it would be 6 o'clock.

like image 25
user1416564 Avatar answered Dec 05 '25 17:12

user1416564



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!