VB.Net Tutorial/Operator/Mod

Материал из VB Эксперт
Перейти к: навигация, поиск

Use Mod to fix degree

Public Class Tester
    Public Shared Function FixRange(ByVal origValue As Double, _
          ByVal rangeMin As Double, ByVal rangeMax As Double) _
          As Double
        Dim shiftedValue As Double
        Dim delta As Double
        shiftedValue = origValue - rangeMin
        delta = rangeMax - rangeMin
        Return (((shiftedValue Mod delta) + delta) Mod delta) + _
           rangeMin
    End Function
    Public Shared Sub Main
        Dim result As New System.Text.StringBuilder
        Dim formatDegrees As String = "Degrees: {0}  Range: {1},{2}  Value: {3}"
        Dim formatRadians As String = "Radians: {0}  Range: {1},{2}  Value: {3}"
        Dim degrees As Double
        Dim radians As Double
        Dim ranged As Double
        " ----- Degrees over the range.
        degrees = 367.75
        ranged = FixRange(degrees, 0, 360)
        result.AppendLine(String.Format(formatDegrees,degrees, 0, 360, ranged))
        " ----- Radians over the range.
        radians = Math.PI * 3.33
        ranged = FixRange(radians, -Math.PI, Math.PI)
        result.AppendLine(String.Format(formatRadians, _
           radians, -Math.PI, Math.PI, ranged))
        Console.WriteLine(result.ToString())
    End Sub
End Class
Degrees: 367.75  Range: 0,360  Value: 7.75
Radians: 10.461503536454  Range: -3.14159265358979,3.14159265358979  Value: -2.10486707790516