# Draw spheres using WPF and C# This example shows how you can draw spheres in a 3D WPF program. The following code shows the AddSphere method, which adds a sphere to a MeshGeometry3D. It’s kind of long, but the ideas (which I’ll explain shortly) aren’t too complicated.

```// Add a sphere.
private void AddSphere(MeshGeometry3D mesh, Point3D center,
double radius, int num_phi, int num_theta)
{
double phi0, theta0;
double dphi = Math.PI / num_phi;
double dtheta = 2 * Math.PI / num_theta;

phi0 = 0;
double y0 = radius * Math.Cos(phi0);
double r0 = radius * Math.Sin(phi0);
for (int i = 0; i < num_phi; i++)
{
double phi1 = phi0 + dphi;
double y1 = radius * Math.Cos(phi1);
double r1 = radius * Math.Sin(phi1);

// Point ptAB has phi value A and theta value B.
// For example, pt01 has phi = phi0 and theta = theta1.
// Find the points with theta = theta0.
theta0 = 0;
Point3D pt00 = new Point3D(
center.X + r0 * Math.Cos(theta0),
center.Y + y0,
center.Z + r0 * Math.Sin(theta0));
Point3D pt10 = new Point3D(
center.X + r1 * Math.Cos(theta0),
center.Y + y1,
center.Z + r1 * Math.Sin(theta0));
for (int j = 0; j < num_theta; j++)
{
// Find the points with theta = theta1.
double theta1 = theta0 + dtheta;
Point3D pt01 = new Point3D(
center.X + r0 * Math.Cos(theta1),
center.Y + y0,
center.Z + r0 * Math.Sin(theta1));
Point3D pt11 = new Point3D(
center.X + r1 * Math.Cos(theta1),
center.Y + y1,
center.Z + r1 * Math.Sin(theta1));

// Create the triangles.

// Move to the next value of theta.
theta0 = theta1;
pt00 = pt01;
pt10 = pt11;
}

// Move to the next value of phi.
phi0 = phi1;
y0 = y1;
r0 = r1;
}
}``` This method basically loops variable phi through values between 0 and π. The value phi represents an angle of rotation down from the vertical Y axis as shown in the figure on the right.

For each value of phi, the method loops variable theta through the values between 0 and 2 * π. The value theta represents a rotation around the Y axis as shown in the figure on the right.

Together phi and theta determine the spherical coordinates of a point. (If you’re familiar with spherical coordinates, you can skip the next two paragraphs.)

Consider the blue triangle shown in the picture. This is a right triangle (the radius is the hypotenuse), so the length of the side along the Y axis is y0 = radius * cos(phi) and the length of the side parallel to the X-Z plane has length r0 = radius * sin(phi).

Now consider the green triangle shown in the picture. It’s a right triangle with the dashed line as its hypotenuse. Its hypotenuse has the same length (and is parallel to) the line labeled r0. In that case the point’s X coordinate is given by x0 = r0 * cos(theta) and z0 = r0 * sin(theta). That’s how the program uses spherical coordinates to calculate the Cartesian coordinates of the points on the sphere. The picture on the right shows the points calculated for particular values of phi and theta.

Having found those points, the code simply calls the AddTriangle method to create the triangles pt00-pt11-pt10 and pt00-pt01-pt11.

The rest of the program is similar to previous examples that make 3D drawings in WPF. Download the example to see the details.     Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in algorithms, drawing, geometry, graphics, mathematics, wpf, XAML and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

### 6 Responses to Draw spheres using WPF and C#

1. Stephen Mitchell says:

Hello Rod,

Quick question, what application and techniques do you use for creating the illustrations on your blog posts? Will be helpful.

Thank you.

• RodStephens says:

They are mostly just screen shots of Visual Studio programs. Occasionally I make a drawing in a Word document and take a screen shot of that.

2. Fabio says:

Hi,
I’m sorry for this question but how can we draw a half sphere. or cut the sphere in a point between half and full sphere.

• RodStephens says:

No need to be sorry. To draw a half sphere, simply make phi loop from 0 to pi / 2 instead of fro 0 to pi.

You may want to draw a circle for the bottom of the hemisphere, too.

Note that these sorts of things are thoroughly covered in my book WPF 3d, Three-Dimensional Graphics with WPF and C#. I don’t think it covers a hemisphere method, but it describes the tools that you can use to build one and I know it shows how to draw a circle.

3. B. says:

Dear Sir,

could you please tell me where I can read the source code of the AddTriangle function or where the documentation is?

Sincerely

This site uses Akismet to reduce spam. Learn how your comment data is processed.