Draw spheres using WPF and C#

[draw spheres]

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.
            AddTriangle(mesh, pt00, pt11, pt10);
            AddTriangle(mesh, pt00, pt01, pt11);

            // 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;

[draw spheres]
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).

[draw spheres]
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.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

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.

Leave a Reply

Your email address will not be published. Required fields are marked *