ARTICLE

Spherical Coordinates in VB.NET

Posted by Jack xu Articles | VB.NET Articles August 31, 2007
This article shows how to create sphere objects in a spherical coordinate system using VB and GDI+.
Download Files:
 
Reader Level:

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/).

Login to add your contents and source code to this article
share this article :
post comment
 

Jack - Really enjoy your work! I was looking at your coding example for creating a sphere. This is something I am playing with for my own edification. I was trying to find the DrawSphere class for vb.net. I was hoping you would be kind enough to send me the class file. You could send the file to my email address which is Jack.Carter1@cox.net. Thank-you in advance for your help!

Posted by Jack Carter Apr 26, 2009
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor