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 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}
Generating random integers.
Module Tester
Sub Main()
Dim randomObject As Random = New Random()
Dim randomNumber As Integer
Dim output As String = ""
Dim i As Integer
For i = 1 To 20
randomNumber = randomObject.Next(1, 7)
output &= randomNumber & " "
If i Mod 5 = 0 Then " is i a multiple of 5?
output &= vbCrLf
End If
Next
Console.WriteLine(output)
End Sub
End Module
4 1 3 4 3
5 6 2 6 5
5 3 6 4 1
6 6 3 6 3
Math. Cos and Sin
Public Class Tester
Public Shared Sub Main
Dim X As Single
Dim Y As Single
X = CSng( Math.Cos(100))
Y = CSng( Math.Sin(100))
Console.WriteLine(X)
Console.WriteLine(Y)
End Sub
End Class
0.8623189
-0.5063657
Math.Round
Public Class Tester
Public Shared Sub Main
Dim digits As Integer
For digits = 0 To 5
Console.WriteLine(Math.Round(Math.PI, digits))
Next digits
End Sub
End Class
3
3.1
3.14
3.142
3.1416
3.14159
Math.Sqrt and Atan2
Public Class Tester
Public Shared Sub Main
Dim magnitude As Single
Dim radians As Single
magnitude = CSng(Math.Sqrt(100 ^ 2 + 120 ^ 2))
radians = CSng(Math.Atan2(100, 123))
Console.WriteLine(magnitude)
Console.WriteLine(radians)
End Sub
End Class
156.205
0.6826226
Randomize
Option Strict On
Public Module RandomizeStatement
Public Sub Main()
Dim number As Double = 1.0325
Rnd(-1)
Randomize(number) " Number is any Double
For ctr As Integer = 1 to 10
Console.WriteLine(Rnd())
Next
End Sub
End Module
0.3610106
0.579608
0.2782471
0.05653632
0.2865489
0.33937
0.6398301
0.2991799
0.8264404
0.1662288