Development/JavaScript

javascript - Find angle between hour and minute hands in an analog clock

linuxism 2014. 4. 1. 11:00


Find angle between hour and minute hands in an analog clock


I was given this interview question recently:

Given a 12-hour analog clock, compute in degree the smaller angle between the hour and minute hands. Be as precise as you can.

I'm wondering what's the simplest, most readable, most precise algorithm is. Solution in any language is welcome (but do explain it a bit if you think it's necessary).

share|improve this question
8 
simplest? looking it up on wiki! : en.wikipedia.org/wiki/Clock_angle_problem'; –  Mitch Wheat May 1 '10 at 5:12
3 
I think that this is a purely mathematical problem, and also fairly trivial. I do not see how this question can get four up-votes... –  Andreas Rejbrand May 1 '10 at 12:06
add comment

It turns out that Wikipedia does have the best answer.

// h = 1..12, m = 0..59
static double angle(int h, int m) {
    double hAngle = 0.5D * (h * 60 + m);
    double mAngle = 6 * m;
    double angle = Math.abs(hAngle - mAngle);
    angle = Math.min(angle, 360 - angle);
    return angle;
}

Basically:

  • The hour hand moves at the rate of 0.5 degrees per minute
  • The minute hand moves at the rate of of 6 degrees per minute

Problem solved.


And precision isn't a concern because the fractional part is either .0 or .5, and in the range of 0..360, all of these values are exactly representable in double.

share|improve this answer
1 
This is not quite correct for h >= 12. –  starblue May 1 '10 at 11:03
1 
@starblue: I've clarified that it's a 12-hour analog clock. –  polygenelubricants May 1 '10 at 11:34 
7 
@starblue Can analog clock be ever 24hr? –  AksharRoop Sep 3 '12 at 8:20
add comment

For finding the angle between the hands of a clock is ,

30 * [HRS - (MIN/5)] + (MIN/2) 
share|improve this answer
   
This works great. Thanks for the answer!! –  gabhi Nov 3 '13 at 18:46
   
Missing absolute value ? –  Hunter McMillen Dec 17 '13 at 14:27
add comment

The java code that polygenlubricants is similar than mine. Let's assume that the clock is 12 hour instead of 24.

If it's 24 hours, then that's a different story. Also, another assumption, assume if the clock is stopped while we calculate this.

One clock cycle is 360 degree.

  1. How many degree can the minute hand run per minute? 360 / 60 = 6 degree per minute.

  2. How many degree can the hour hand run per hour? 360/12 = 30 degree per hour (since hour hand run slower than minute)

Since it's easier to calculate in the unit, "minute", let's get

"how many degree can the hour hand run per minute"?

30 / 60 = 0.5 degree per minute.

So, if you know how to get those numbers, the problem is pretty much done with solution.

share|improve this answer
add comment

Try this code :

import java.util.Scanner;

class Clock{

    public static void main(String args[]){
        int hours,mins;

    System.out.println("Enter the Time(hours) : ");
        Scanner dx = new Scanner(System.in);
        hours = dx.nextInt();

    System.out.println("Enter the time(mins) : ");
        Scanner fx = new Scanner(System.in);
        mins = fx.nextInt();

    if(hours>=0 && hours<=12){

        if(mins>=0 && mins<=59){
            double hDegrees = (hours * 30) + (mins * 0.5);
                    double mDegrees = mins * 6;
                    double diff  = Math.abs(hDegrees - mDegrees);

        System.out.println("The angle between sticks is (degrees) : "+diff);
                if (diff > 180){ 

                diff = 360 - diff;
        System.out.println("The angle between sticks is (degrees) : "+diff);
                }

        }

    }

    else{
        System.out.println("Wrong input ");
    }


}

}
share|improve this answer
add comment

for finding the angle between the hour hand and the minute hand is

angle=(hour*5-min)*6
share|improve this answer
add comment

I tried to solve this problem but I don't think it can be solved mathematics.

share|improve this answer
add comment

I do not know if it's right, .something like this?

//m*360/60 - (h*360/24)+(m*360/(24*60)) ->
t = abs(25*m - 60*h)/4
t = min(t,360-t)
share|improve this answer
   
Well it can't be right if you're missing an end parenthesis :D –  Wallacoloo May 1 '10 at 5:17
   
For h=3m=0, I get 45. It should be 90. –  polygenelubricants May 1 '10 at 5:37
   
@poly okay, I forgot regular clock is 12, not 24 hours. if you replace division by 24 with 12, you should get correct –  Anycorn May 1 '10 at 5:42




출처 - http://stackoverflow.com/questions/2748965/fnd-angle-between-hour-and-minute-hands-in-an-analog-clock