15,567,786 members
Articles / Programming Languages / C#
Article
Posted 27 Mar 2007

124.6K views
78 bookmarked

# Spherical Coordinates in C#

Rate me:

## 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.

## Background

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 azimuthal 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. And 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".

C#
```public Point3 Spherical(float r, float theta, float phi)
{
Point3 pt = new Point3();
float snt = (float)Math.Sin(theta * Math.PI / 180);
float cnt = (float)Math.Cos(theta * Math.PI / 180);
float snp = (float)Math.Sin(phi * Math.PI / 180);
float cnp = (float)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;
} ```

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.

## Using the code

This application can be tested using the following `Form1 `class:
C#
```using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Example5_6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
// Subscribing to a paint eventhandler to drawingPanel:
panel1.Paint += new PaintEventHandler(panel1Paint);
}

private void panel1Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
float a = panel1.Height / 3;
DrawSphere ds = new DrawSphere(this, a, 0, 0, -a / 2);
ds.DrawIsometricView(g);
ds = new DrawSphere(this, 2 * a / 3, -a/2, -a/2, a / 2);
ds.DrawIsometricView(g);
}
}
} ```

Here we create two spheres with different radii 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.

This project is from the examples (example5-6 in Chapter 5) of the new book "Practical C# Charts and Graphics", where you can find more advanced chart and graphics programming for real-world .NET applications. For more information, please visit the website at www.publishing.unicadinc.com

Dr. Jack Xu has a Ph.D in theoretical physics. He has over 15 years programming experience in Basic, Fortran, C, C++, Matlab, and C#, specializing in numerical computation methods, algorithms, physical modeling, computer-aided design (CAD) development, graphics user interface, and 3D graphics. Currently, he is responsible for developing commercial CAD tools based on Microsoft .NET framework.

A list of licenses authors might use can be found here

Written By
United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

 First Prev Next
 Re: DrawSphere Alaric_24-Apr-12 8:39 Alaric_ 24-Apr-12 8:39
 Re: DrawSphere John Demetriou8-Oct-17 23:02 John Demetriou 8-Oct-17 23:02
 Re: DrawSphere Member 1208224530-Nov-15 16:45 Member 12082245 30-Nov-15 16:45
 My vote of 5 Manoj Kumar Choubey18-Feb-12 4:23 Manoj Kumar Choubey 18-Feb-12 4:23
 My vote of 1 victorbos29-Sep-09 14:17 victorbos 29-Sep-09 14:17
 Cube? Darchangel7-Aug-07 4:46 Darchangel 7-Aug-07 4:46
 Re: Cube? Albin Abel15-Mar-11 5:15 Albin Abel 15-Mar-11 5:15
 How do you draw a 3d pyramid and funnel/Cone Patrick Blackman27-Mar-07 17:44 Patrick Blackman 27-Mar-07 17:44
 Re: How do you draw a 3d pyramid and funnel/Cone [modified] Jack J. H. Xu28-Mar-07 6:36 Jack J. H. Xu 28-Mar-07 6:36
 Hi, Patrick, Thank you for your interest in my work. The procedure of drawing any 3d objects, including 3d pyramid, cone, and funnel you mentioned, is the same. Because our computer screen is 2d, you cannot directly display 3d objects on a 2d screen. you have to project your 3d objects to 2d. Specifically, you first need to know coordinates of your 3d object in 3d space. in this step, you can choose the most convenient 3d coordinate system according to the nature of your 3d objects. in my article, the spherical system is used because i want to draw sphere objects. In most of cases, the 3d Cartesian coordinate system is usually used. Next, you project these 3d coordinates to 2d. finally you can draw your 3d object on your screen using these projected 2d coordinates. To achieve this goal in C#, you need to define 3D point, 3d matrix, 3d projection, and 3d transformation, because there are no default 3d objects in C#. my new book "Practical C# Charts and Graphics" presents all the details on how to define these 3d objects in 3d (actually it is 4d) homogeneous coordinate system, how to perform projection and transformation, and how to draw 3d object on a 2d screen. The book is expected to come out in 2-4 weeks. Then you can order it from Amazon or any bookstores worldwide. if you need it right now, the publisher might be able to help you because they should have some copies of the book. you can send an email to ask them how to order it: sales.publishing@unicadinc.com Best. Jack -- modified at 23:16 Wednesday 28th March, 2007 -- modified at 23:21 Wednesday 28th March, 2007 Dr. Jack Xu has a Ph.D in theoretical physics. He has over 15 years programming experience in Basic, FORTRAN, C, C++, Matlab, and C#.
 Re: How do you draw a 3d pyramid and funnel/Cone Patrick Blackman28-Mar-07 15:00 Patrick Blackman 28-Mar-07 15:00
 Re: How do you draw a 3d pyramid and funnel/Cone Jack J. H. Xu29-Mar-07 10:37 Jack J. H. Xu 29-Mar-07 10:37
 Last Visit: 31-Dec-99 19:00     Last Update: 6-Feb-23 19:59 Refresh 1