Introduction:
Normally, the Cartesian coordinate system is used in transformations and projections for graphics objects. In this case, you simply specify a point using X, Y, and Z coordinates. In practice, other coordinate systems can also be applied, and are sometimes more convenient than the Cartesian coordinate system. In this article, I will discuss the spherical coordinate system in 3D space and show you how to create the spherical graphics objects in this system.
In the spherical coordinate system, a point is specified by r,
, and
. Here r is the distance from the point to the origin,
is the polar angle, and
is the azimuthally angle in the X-Z plane from the X-axis. In this notation, I alternate the conventional Y and Z axes so that the computer screen is described by the X-Y plane. Figure 1 shows a point in this spherical coordinate system.
Figure 1: Spherical coordinate system
From this figure, we can obtain the following relationships

The spherical coordinates (r,
,
) are related to the Cartesian coordinates by

Sometimes it is more convenient to create sphere-like objects in terms of the spherical coordinate system. The following example application program will create two spheres. We need to add the Matrix3 and Point3 classes to the current project. Ans also add a new class, DrawSphere, to the project.
Now we need to add a Spherical method to the Matrix3 class. The Matrix3 class has been discussed in Chapter 5 of my new book "Practical C# Charts and Graphics".
Public Function Spherical(ByVal r As Single, ByVal theta As Single, ByVal phi As Single) As Point3
Dim pt As Point3 = New Point3()
Dim snt As Single = CSng(Math.Sin(theta * Math.PI / 180))
Dim cnt As Single = CSng(Math.Cos(theta * Math.PI / 180))
Dim snp As Single = CSng(Math.Sin(phi * Math.PI / 180))
Dim cnp As Single = CSng(Math.Cos(phi * Math.PI / 180))
pt.X = r * snt * cnp
pt.Y = r * cnt
pt.Z = -r * snt * snp
pt.W = 1
Return pt
End Function
This method transforms a point in the spherical coordinate system to a point in the Cartesian coordinate system.
We then add a DrawSphere class to the project. This class allows you to specify the radius and positions (the center location) of a sphere object. The SphereCoordinates method in this class creates the points on a sphere surface by specifying their longitude and latitude. The DrawIsometricView draws the sphere using the isometric projection.
Test the Code:
This application can be tested using the following Form1 class:
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Namespace Example5_6
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
Me.SetStyle(ControlStyles.ResizeRedraw, True)
' Subscribing to a paint eventhandler to drawingPanel:
AddHandler panel1.Paint, AddressOf panel1Paint
End Sub
Private Sub panel1Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics
g.SmoothingMode = SmoothingMode.AntiAlias
Dim a As Single = panel1.Height / 3
Dim ds As DrawSphere = New DrawSphere(Me, a, 0, 0, -a / 2)
ds.DrawIsometricView(g)
ds = New DrawSphere(Me, 2 * a / 3, -a / 2, -a / 2, a / 2)
ds.DrawIsometricView(g)
End Sub
End Class
End Namespace
Here we create two spheres with different radius and positions. By building and running this project, you should obtain the results shown in Figure 2.

Figure 2 Spheres created in a spherical coordinate system.
NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# Corner (http://www.c-sharpcorner.com/).