Convert rectangular 3D coordinates to cylindrical coordinates. : Math « Development « VB.Net Tutorial






'Convert cylindrical coordinates to rectangular 3D coordinates.
        ' ----- Convert rectangular 3D coordinates to
        '       spherical coordinates.
        ' ----- Convert spherical coordinates to
        '       rectangular 3D coordinates.
        ' ----- Convert spherical coordinates to
        '       cylindrical coordinates.        
        ' ----- Convert cylindrical coordinates to
        '       spherical coordinates.        
        
' Quote from
'Visual Basic 2005 Cookbook Solutions for VB 2005 Programmers
'by Tim Patrick (Author), John Craig (Author)
'# Publisher: O'Reilly Media, Inc. (September 21, 2006)
'# Language: English
'# ISBN-10: 0596101775
'# ISBN-13: 978-0596101770

Public Class Tester

    Public Shared Sub Main
        Dim result As New System.Text.StringBuilder
        Dim pointRec As New Point3D(3, 4, 5)
        Dim pointCyl As Point3D = RectToCylinder(pointRec)
        Dim pointSph As Point3D = RectToSphere(pointRec)
        Dim pointRecToCyl As Point3D = RectToCylinder(pointRec)
        Dim pointRecToSph As Point3D = RectToSphere(pointRec)
        Dim pointCylToRec As Point3D = CylinderToRect(pointCyl)
        Dim pointCylToSph As Point3D = CylinderToSphere(pointCyl)
        Dim pointSphToRec As Point3D = SphereToRect(pointSph)
        Dim pointSphToCyl As Point3D = SphereToCylinder(pointSph)

        result.AppendLine("Rec: " & pointRec.Tostring())
        result.AppendLine("Cyl: " & pointCyl.Tostring())
        result.AppendLine("Sph: " & pointSph.Tostring())
        result.AppendLine()

        result.AppendLine("Rec to Cyl: " & pointRecToCyl.Tostring())
        result.AppendLine("Rec to Sph: " & pointRecToSph.Tostring())
        result.AppendLine("Cyl to Rec: " & pointCylToRec.Tostring())
        result.AppendLine("Cyl to Sph: " & pointCylToSph.Tostring())
        result.AppendLine("Sph to Rec: " & pointSphToRec.Tostring())
        result.AppendLine("Sph to Cyl: " & pointSphToCyl.Tostring())

        Console.WriteLine(result.ToString())


    End Sub

    Public Shared Function RectToCylinder(ByVal pointA As Point3D) _
          As Point3D
        ' ----- Convert rectangular 3D coordinates to
        '       cylindrical coordinates.
        Dim rho As Double
        Dim theta As Double

        rho = Math.Sqrt(pointA.X ^ 2 + pointA.Y ^ 2)
        theta = Math.Atan2(pointA.Y, pointA.X)
        Return New Point3D(rho, theta, pointA.Z)
    End Function

    Public Shared Function CylinderToRect(ByVal pointA As Point3D) _
          As Point3D
        ' ----- Convert cylindrical coordinates to
        '       rectangular 3D coordinates.
        Dim x As Double
        Dim y As Double

        x = pointA.X * Math.Cos(pointA.Y)
        y = pointA.X * Math.Sin(pointA.Y)
        Return New Point3D(x, y, pointA.Z)
    End Function

    Public Shared Function RectToSphere(ByVal pointA As Point3D) _
          As Point3D
        ' ----- Convert rectangular 3D coordinates to
        '       spherical coordinates.
        Dim rho As Double
        Dim theta As Double
        Dim phi As Double

        rho = Math.Sqrt(pointA.X ^ 2 + pointA.Y ^ 2 + _
           pointA.Z ^ 2)
        theta = Math.Atan2(pointA.Y, pointA.X)
        phi = Math.Acos(pointA.Z / Math.Sqrt( _
           pointA.X ^ 2 + pointA.Y ^ 2 + pointA.Z ^ 2))
        Return New Point3D(rho, theta, phi)
    End Function

    Public Shared Function SphereToRect(ByVal pointA As Point3D) _
          As Point3D
        ' ----- Convert spherical coordinates to
        '       rectangular 3D coordinates.
        Dim x As Double
        Dim y As Double
        Dim z As Double

        x = pointA.X * Math.Cos(pointA.Y) * Math.Sin(pointA.Z)
        y = pointA.X * Math.Sin(pointA.Y) * Math.Sin(pointA.Z)
        z = pointA.X * Math.Cos(pointA.Z)
        Return New Point3D(x, y, z)
    End Function

    Public Shared Function CylinderToSphere(ByVal pointA As Point3D) _
          As Point3D
        ' ----- Convert cylindrical coordinates to
        '       spherical coordinates.
        Dim rho As Double
        Dim theta As Double
        Dim phi As Double

        rho = Math.Sqrt(pointA.X ^ 2 + pointA.Z ^ 2)
        theta = pointA.Y
        phi = Math.Acos(pointA.Z / _
           Math.Sqrt(pointA.X ^ 2 + pointA.Z ^ 2))
        Return New Point3D(rho, theta, phi)
    End Function

    Public Shared Function SphereToCylinder(ByVal pointA As Point3D) _
          As Point3D
        ' ----- Convert spherical coordinates to
        '       cylindrical coordinates.
        Dim rho As Double
        Dim theta As Double
        Dim z As Double

        rho = pointA.X * Math.Sin(pointA.Z)
        theta = pointA.Y
        z = pointA.X * Math.Cos(pointA.Z)
        Return New Point3D(rho, theta, z)
    End Function

    
End Class


Public Class Point3D
    Public X As Double
    Public Y As Double
    Public Z As Double

    Public Sub New(ByVal xPoint As Double, _
          ByVal yPoint As Double, ByVal zPoint As Double)
        Me.X = xPoint
        Me.Y = yPoint
        Me.Z = zPoint
    End Sub

    Public Overrides Function Tostring() As String
        Return "{X=" & X & ",Y=" & Y & ",Z=" & Z & "}"
    End Function
End Class
Rec: {X=3,Y=4,Z=5}
Cyl: {X=5,Y=0.927295218001612,Z=5}
Sph: {X=7.07106781186548,Y=0.927295218001612,Z=0.785398163397448}

Rec to Cyl: {X=5,Y=0.927295218001612,Z=5}
Rec to Sph: {X=7.07106781186548,Y=0.927295218001612,Z=0.785398163397448}
Cyl to Rec: {X=3,Y=4,Z=5}
Cyl to Sph: {X=7.07106781186548,Y=0.927295218001612,Z=0.785398163397448}
Sph to Rec: {X=3,Y=4,Z=5}
Sph to Cyl: {X=5,Y=0.927295218001612,Z=5}








7.15.Math
7.15.1.Math.Sqrt and Atan2
7.15.2.Math. Cos and Sin
7.15.3.Convert rectangular 3D coordinates to cylindrical coordinates.
7.15.4.Generating random integers.
7.15.5.Math.Round
7.15.6.Randomize
7.15.7.This example demonstrates Math.Max()
7.15.8.Use the Min method to return and display the smaller of two Single variables.