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

Home
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.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
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
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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.