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

VB.Net Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statements
5. Date Time
6. Class Module
7. Development
8. Collections
9. Generics
10. Attributes
11. Event
12. Stream File
13. GUI
14. GUI Applications
15. 2D Graphics
16. I18N Internationlization
17. Reflection
18. Regular Expressions
19. Security
20. Socket Network
21. Thread
22. Windows
23. XML
24. Database ADO.net
25. Design Patterns
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial » Development » Math 
7. 15. 3. Convert rectangular 3D coordinates to cylindrical coordinates.
 
 
'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 212006)
'# Language: English
'# ISBN-100596101775
'# ISBN-13978-0596101770

Public Class Tester

    Public Shared Sub Main
        Dim result As New System.Text.StringBuilder
        Dim pointRec As New Point3D(345)
        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 ^ + 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 As Double
        Dim 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 ^ + pointA.Y ^ + _
           pointA.Z ^ 2)
        theta = Math.Atan2(pointA.Y, pointA.X)
        phi = Math.Acos(pointA.Z / Math.Sqrt_
           pointA.X ^ + pointA.Y ^ + 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 As Double
        Dim As Double
        Dim 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 ^ + pointA.Z ^ 2)
        theta = pointA.Y
        phi = Math.Acos(pointA.Z / _
           Math.Sqrt(pointA.X ^ + 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 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 As Double
    Public As Double
    Public 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
w___w__w___.__ja_v_a_2s__.___c__o__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.