Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello. I am implementing a clock in java and I am having a little trouble drawin

ID: 3736512 • Letter: H

Question

Hello. I am implementing a clock in java and I am having a little trouble drawing the hands on the dial. I was given two formulas:

The other method is using this:

double firstAngle = 60/60.0*2.0*Math.PI;
double secondAngle = Math.PI/2.0-firstAngle;

Then we would use:something like:

minuteHand.lineTo((float)cX + Math.cos(secondAngle)*200, (float)cY+Math.sin(secondAngle)*200)

This works for me except that using 60 points to 30 minutes and using 30 points to 60 minutes.

I was hoping someone could show me how to implement these funcitons to draw the clock hands.

Thanks.

90 * (27/360)

Explanation / Answer

Hi Student,

The idea is to take 12:00 (h = 12, m = 0) as a reference.

Following are detailed steps.

1) Calculate the angle made by hour hand with respect to 12:00 in h hours and m minutes.
2) Calculate the angle made by minute hand with respect to 12:00 in h hours and m minutes.
3) The difference between two angles is the angle between two hands.

How to calculate the two angles with respect to 12:00?
The minute hand moves 360 degree in 60 minute(or 6 degree in one minute) and hour hand moves 360 degree in 12 hours(or 0.5 degree in 1 minute). In h hours and m minutes, the minute hand would move (h*60 + m)*6 and hour hand would move (h*60 + m)*0.5.

Here is a java implementation :

import java.io.*;

class GFG

{

    // Function to calculate the angle

    static int calcAngle(double h, double m)

    {

        // validate the input

        if (h <0 || m < 0 || h >12 || m > 60)

            System.out.println("Wrong input");

        if (h == 12)

            h = 0;

        if (m == 60)

            m = 0;

        // Calculate the angles moved by hour and minute hands

        // with reference to 12:00

        int hour_angle = (int)(0.5 * (h*60 + m));

        int minute_angle = (int)(6*m);

        // Find the difference between two angles

        int angle = Math.abs(hour_angle - minute_angle);

        // smaller angle of two possible angles

        angle = Math.min(360-angle, angle);

        return angle;

    }

     

    // Driver program

    public static void main (String[] args)

    {

        System.out.println(calcAngle(9, 60)+" degree");

        System.out.println(calcAngle(3, 30)+" degree");

    }

}

Happy Learning :)