## ProblemYou want to convert between two-dimensional coordinates expressed in either rectangular or polar notation. ## SolutionSample code folder: Chapter 06\ConvertPolar Create two functions for the two conversions: ## DiscussionThe Public Function ToPolar(ByVal sourcePoint As PointF) _ As PointF ' ----- Convert rectangular coordinates to polar. Dim magnitude As Single Dim radians As Single magnitude = CSng(Math.Sqrt(sourcePoint.X ^ 2 + _ sourcePoint.Y ^ 2)) radians = CSng(Math.Atan2(sourcePoint.Y, sourcePoint.X)) Return New PointF(magnitude, radians) End Function Public Function ToRectangular(ByVal sourcePoint As PointF) _ As PointF ' ----- Convert polar coordinates to rectangular. Dim X As Single Dim Y As Single X = CSng(sourcePoint.X * Math.Cos(sourcePoint.Y)) Y = CSng(sourcePoint.X * Math.Sin(sourcePoint.Y)) Return New PointF(X, Y) End Function Both functions assume angles will be expressed in radians, which is consistent with the way angles are expressed in Visual Basic. You can convert angles to and from degrees using the constants presented in Recipe 6.10. The following block of code demonstrates the use of the Dim result As New System.Text.StringBuilder Dim pointA As PointF Dim pointB As PointF Dim pointC As PointF pointA = New PointF(3, 4) pointB = ToPolar(pointA) pointC = ToRectangular(pointB) result.AppendLine("Rectangular: " & pointA.ToString()) result.AppendLine("Polar: " & pointB.ToString()) result.AppendLine("Rectangular: " & pointC.ToString()) MsgBox(result.ToString()) The Figure 6-13 shows the formatted string results of the ## Figure 6-13. Rectangular and polar two-dimensional coordinate conversions using PointF variables## See AlsoSearching for " polar rectangular" on the Web will lead you to a variety of explanations and learning materials about this subject. |

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

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