## ProblemYou want to shift intermediate angular calculation results into a range such as 0° to 360°, -180° to 180°, 0 to 2π radians, or π to π radians. ## SolutionSample code folder: Chapter 06\AngleRange Create a function that handles all these range conversions efficiently. ## DiscussionSome scientific calculations produce angular results that are beyond normal ranges, requiring adjustment to bring them into the standard range of values. For example, in astronomical calculations a variety of polynomials are used to compute highly accurate positions of the planets and stars, but the polynomials often return angles representing many revolutions of the various orbs. You might say the angles are astronomical in size before they are adjusted into a normalized range such as 0° to 360°. The following function handles these range adjustments efficiently, bringing the values back down to earth: Public Function FixRange(ByVal origValue As Double, _ ByVal rangeMin As Double, ByVal rangeMax As Double) _ As Double ' ----- Adjust a value to within a specified range. ' Use the range size as the adjustment factor. Dim shiftedValue As Double Dim delta As Double shiftedValue = origValue - rangeMin delta = rangeMax - rangeMin Return (((shiftedValue Mod delta) + delta) Mod delta) + _ rangeMin End Function The The function uses a double application of the The following code demonstrates the use of the 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)) ' ----- Degress under the range. degrees = -97.5 ranged = FixRange(degrees, 0, 360) result.AppendLine(String.Format(formatDegrees, _ degrees, 0, 360, ranged)) ' ----- Degrees in range. degrees = -97.5 ranged = FixRange(degrees, -180, 180) result.AppendLine(String.Format(formatDegrees, _ degrees, -180, 180, 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)) MsgBox(result.ToString()) Figure 6-11 shows the results produced by this sample code. ## Figure 6-11. Using the Range( ) function to normalize angles in degrees or radians## See AlsoSearch for information on the |

Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))

ISBN: 0596101775

EAN: 2147483647

EAN: 2147483647

Year: 2006

Pages: 400

Pages: 400

Authors: Tim Patrick, John Craig

Similar book on Amazon

- Case Study-Hub and Spoke MPLS VPN Network Using BGP PE-CE Routing for Sites Using Unique AS Numbers
- Implementing MPLS QoS for Layer 2 VPN Implementations
- Case Study 4: Implementing Layer 3 VPNs over Layer 2 VPN Topologies and Providing L2 VPN Redundancy
- Case Study 6: Implementing Class-Based Tunnel Selection with MPLS Traffic Engineering
- Case Study 10: BGP Site of Origin

flylib.com © 2008-2017.

If you may any questions please contact us: flylib@qtcs.net

If you may any questions please contact us: flylib@qtcs.net