aboutsummaryrefslogtreecommitdiff
path: root/challenge-120/eric-cheung/excel-vba/ch-2.bas
blob: 2fad9a79eacd7c2b87d95d9d80c174dbf5ae0800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Attribute VB_Name = "ModTask_02"
Option Explicit
'https://www.varsitytutors.com/basic_geometry-help/how-to-find-the-angle-of-clock-hands
'https://www.omnicalculator.com/math/clock-angle#method-2-use-a-clock-angle-formula

Function ClockAngle(strTime) As Double

    Const nSectorToDegree As Integer = 30
    Const nDayToHour As Integer = 12
    Const nDiv As Integer = 5

    Dim nHour As Integer, dMinute As Double, dFactor As Double
    
    nHour = Val(Left(strTime, 2)) Mod nDayToHour
    dMinute = Val(Right(strTime, 2)) / nDiv
    
    ClockAngle = Abs(nHour - dMinute) * nSectorToDegree
    
    If dMinute > 0 Then
        dFactor = dMinute * nSectorToDegree / nDayToHour
        If nHour >= dMinute Then
            ClockAngle = ClockAngle + dFactor
        Else
            ClockAngle = ClockAngle - dFactor
        End If
    End If
    
    ClockAngle = Abs(Round(ClockAngle, 2))
    
    If ClockAngle > 180 Then
        ClockAngle = 360 - ClockAngle
    End If

End Function

Sub Task_02()

    Dim varInputNum
    
    Do
        varInputNum = InputBox("Please enter the time", strMyTitle, "04:00")
        If varInputNum = "" Then Exit Sub
    Loop Until varInputNum <> ""
    
    MsgBox "The smallest angle spanned by the time " & varInputNum & " is: " & ClockAngle(varInputNum) & " degrees"
    
End Sub