Introduction
In this article, we will learn how to calculate the angle between the two hands of the clock in C#.
Step 1. Let's try to understand the clock's behavior.
The clock has two different measurement problems.
- Angles (calculated in degree from number 12 clockwise)
- Times (calculated usually based on a 12-hour clock.)
Let’s take an example. The time is 1:20 PM.
From the above screen shot let’s calculate the angle between the hour hand and the minute hand.
We can divide the complete clock into 12 gaps, and each gap with 300.
- Angle of Seg0 =| (Angle of Seg1) – (Angle of Seg2) |
- Since Seg1 has 4 gaps, then the total angle will be =4*300=1200
- Since Seg2 has 1 gap and one sub gap g1 then total angle will be =1*300 +g1
To get the angle of seg0, we have to find out the sub-gap g1 angle, so let's try to use the above formula to calculate the value of sub-gap g1.
Since we know that the angle covered by the hour hand in one minute =0.50 and the hour hand covered the g1 segment in 20 minutes, we can say that the angle covered by sub gap g1 will be =20*0.50 =100.
so Seg2 has 1 gap and one sub gap g1 then total angle will be =1*300 +100=400
Now Angle of Seg0 =|120-40|=800.
Similarly, we can calculate the angle of a given point in time.
Let’s try to build a formula to calculate the angle. if you see from the above example, then you can build the formula.
Time is H: M
Let's consider the rate of change of the angle in degrees per minute.
- Angle covered by hour hand in 12 hours = 3600
- 01 hour = 300
- 01 min= (30/60) ==0.50 = (1/2)0
- Angle covered by minute Hand in 05 minutes =300
Angle covered by hour hand qh= (H*60*0.5+M*0.5) = 0.5(h*60+M)0
Angle covered by minute hand qm=(M*6)0
Total angle between hour & minute hand will be ∆q= | qh - qm |
∆θ = | (0.5(h*60+M)0) - (M*6)0 |=|30H-5.5M=5(6H-1.1M) |0=|5(6H-11/10M) |0
∆θ=|5(6H-1.1M) |0 Clockwise
Where H is an hour, and M is a minute, we are ignoring seconds here.
Let’s try to validate this formula. The time is 1:20 PM
As per the formula angle between the hour and minute hand will be = |5(6*1-1.1*20) |0
=|5(6-22) |0=|5*(-16) |0=800. This is the same angle we have calculated previously in an example.
Now let’s try to write a method to calculate the angle between the hour and minute hand.
Step 2. Create a test project and create a cs file with the name “Smartclock. cs” and create a function “GetAngleBetweenHourAndMinuteHand” and add the below lines of code into it.
using System;
namespace CalculateAngleHMhands
{
class SmartClock
{
public double GetAngleBetweenHourAndMinuteHand(int hour, int minute)
{
// ∆θ = |5(6H - 11/10M)|
// hour => H
// minute => M
return Math.Abs(5 * ((6 * hour) - (1.1 * minute)));
}
}
}
Step 3. In the test class, write a few tests to validate this formula.
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CalculateAngleHMhands
{
[TestClass]
public class UnitTest1
{
private readonly SmartClock smartClock;
public UnitTest1()
{
smartClock = new SmartClock();
}
[TestMethod]
public void TestMethod_0_00_PM()
{
double angle = smartClock.GetAngleBetweenHourAndMinuteHand(0, 00);
Assert.AreEqual(0, angle);
}
[TestMethod]
public void TestMethod_12_30_PM()
{
double angle = smartClock.GetAngleBetweenHourAndMinuteHand(12, 30);
Assert.AreEqual(195, angle);
}
[TestMethod]
public void TestMethod_1_20_PM()
{
double angle = smartClock.GetAngleBetweenHourAndMinuteHand(1, 20);
Assert.AreEqual(80, angle);
}
[TestMethod]
public void TestMethod_2_20_PM()
{
double angle = smartClock.GetAngleBetweenHourAndMinuteHand(2, 20);
Assert.AreEqual(50, angle);
}
}
}
Now, you can see you developed a formula, and it is working perfectly fine.
Congratulations, you have successfully created and calculated the angle between the clock's hour and minute hand. If you have any queries or concerns, just let me know or put them in the comment box, and I will respond as soon as possible. I am open to discussing anything, even silly questions as well. If you have any suggestions related to this article, please let me know, and I promise I will improve this article.
That's all for this tutorial.
Summary
In this article, we have learned how to calculate the angle between a clock's hour and minute hand in C#.