Option Explicit
Function ZeroTo2Pi(Rad) As Double
ZeroTo2Pi = BasePeriodVal(0#, 6.28318530717959, Rad)
End Function
Function ZeroTo360(Deg) As Double
ZeroTo360 = BasePeriodVal(0#, 360#, Deg)
End Function
Function BasePeriodVal(X, Y, Val) As Double
Dim XY As Double
XY = Y - X
BasePeriodVal = Val
TestVal:
If BasePeriodVal < X Then
BasePeriodVal = BasePeriodVal + XY
GoTo TestVal
Else
If BasePeriodVal >= Y Then
BasePeriodVal = BasePeriodVal - XY
GoTo TestVal
End If
End If
End Function
Sub BasePeriod_Test()
Dim Ans As String
Dim I As Integer
Dim Title As String
Dim X As Single
Dim X1 As Single
Dim X2 As Single
Dim Xi As Single
Title = "Test of base period conversion functions"
GetInitial:
Ans = InputBox("enter test #:" & vbCrLf & _
"1 convert degrees to [0 , 360]" & vbCrLf & _
"2 convert radians to [0 , 2PI]" & vbCrLf & _
"3 convert any period value to base period equivalent" & vbCrLf & vbCrLf & _
"[enter nothing or click on Cancel to quit]", Title)
If Ans = "" Then Exit Sub
I = Ans
Select Case I
Case Is = 1
GetCase1: Ans = InputBox("value in degrees ?", "Demo of ZeroTo360")
If Ans = "" Then GoTo GetInitial
X = Ans
MsgBox X & " degrees = " & Format(ZeroTo360(X), "##0.###") & " degrees", _
vbInformation, Title
GoTo GetCase1
Case Is = 2
GetCase2: Ans = InputBox("value in radians ?", "Demo of ZeroTo2Pi")
If Ans = "" Then GoTo GetInitial
X = Ans
MsgBox X & " radians = " & Format(ZeroTo2Pi(X), "##0.###") & " radians", _
vbInformation, Title
GoTo GetCase2
Case Is = 3
GetCase3x1: Ans = InputBox("start of base period ?", "Demo of BasePeriodVal")
If Ans = "" Then GoTo GetInitial
X1 = Ans
GetCase3x2: Ans = InputBox("end of base period ?" & vbCrLf & _
"start of base period = " & X1, "Demo of BasePeriodVal")
If Ans = "" Then GoTo GetCase3x1
X2 = Ans
GetCase3Xi: Ans = InputBox("target value ?" & vbCrLf & _
"base period = [ " & X1 & " , " & X2 & " ]", "Demo of BasePeriodVal")
If Ans = "" Then GoTo GetCase3x2
Xi = Ans
MsgBox "start of base period = " & X1 & vbCrLf & _
"end of base period = " & X2 & vbCrLf & _
"target value = " & Xi & vbCrLf & _
"resulting value in base period = " & _
Format(BasePeriodVal(X1, X2, Xi), "##0.###"), _
vbInformation, Title
GoTo GetCase3Xi
End Select
GoTo GetInitial
End Sub
|