# Build a geodesic sphere with WPF and C# # Building Geodesic Spheres

Building a geodesic sphere is actually pretty easy after you know how to build an icosahedron.

Start by building an icosahedron as described in the post Platonic Solids Part 6: The icosahedron. Each of the icosahedron’s faces is a triangle. Divide the triangle into 9 new triangles as shown in Figure 1. In Figure 1 the points labeled , , and  are the triangle’s original corners (ordered for proper outward orientation according to the right-hand rule).

Push out the newly created points A, B, C, D, E, F, and G so they are the same distance from the center of the sphere as the original icosahedron vertices. If the icosahedron is centered at the origin and its outer radius is R, then you would move the new point (X, Y, Z) to:

```    new_X = X * R / Sqr(X * X + Y * Y + Z * Z)
new_Y = Y * R / Sqr(X * X + Y * Y + Z * Z)
new_Z = Z * R / Sqr(X * X + Y * Y + Z * Z)```

Now use the new points to build the triangles shown in Figure 1. The result is a new solid with 9 times as many triangles as before. Because the initial icosahedron had 20 faces, the new solid has 9×20 = 180 faces.

If you like you can repeat those steps to convert each of the 180 faces into 9 new triangles giving 120×9 = 1,620 faces. You can continue subdividing triangles to get as smooth a result as you desire. (Although 1,620 faces is probably enough unless you’re building a really huge sphere, perhaps for a lunar colony.) You may recall from my first post on the Platonic solids that the only Platonic solids in 3 dimensions are the tetrahedron, cube, octahedron, dodecahedron, and icosahedron. The reason the new solid isn’t Platonic is because its faces don’t all have the same side lengths.

The icosahedron starts with faces that are equilateral triangles so it is Platonic. When you divide an equilateral triangle as described above, the resulting triangles are not equilateral. The example includes code that displays the number of different kinds of triangles in the solid and their angles.

The first level sphere contains two kinds of triangles with angles:

```    54.6347°, 54.6347°, 70.7305°
58.5832°, 60.7084°, 60.7084°```

The second level sphere contains 12 kinds of triangles and the third level sphere contains 85 kinds of triangles.

# Example Code

The example program uses a Triangle class to manage the triangles it builds. This class is mostly a wrapper for an array that holds three points. It also includes the following code to subdivide a triangle into 9 new triangles.

```// Subdivide this triangle and put the
// new triangles in the list triangles.
public void Subdivide(List<Triangle> triangles,
Point3D center, double radius)
{
// Find the dividing points.
Vector3D v01 = Points - Points;
Vector3D v02 = Points - Points;
Vector3D v12 = Points - Points;
Point3D A = Points + v01 * 1.0 / 3.0;
Point3D B = Points + v02 * 1.0 / 3.0;
Point3D C = Points + v01 * 2.0 / 3.0;
Point3D D = Points + v01 * 2.0 / 3.0 + v12 * 1.0 / 3.0;
Point3D E = Points + v02 * 2.0 / 3.0;
Point3D F = Points + v12 * 1.0 / 3.0;
Point3D G = Points + v12 * 2.0 / 3.0;

// Normalize the points.
NormalizePoint(ref A, center, radius);
NormalizePoint(ref B, center, radius);
NormalizePoint(ref C, center, radius);
NormalizePoint(ref D, center, radius);
NormalizePoint(ref E, center, radius);
NormalizePoint(ref F, center, radius);
NormalizePoint(ref G, center, radius);

// Make the triangles.
triangles.Add(new Triangle(Points, A, B));
triangles.Add(new Triangle(A, C, D));
triangles.Add(new Triangle(A, D, B));
triangles.Add(new Triangle(B, D, E));
triangles.Add(new Triangle(C, Points, F));
triangles.Add(new Triangle(C, F, D));
triangles.Add(new Triangle(D, F, G));
triangles.Add(new Triangle(D, G, E));
triangles.Add(new Triangle(E, G, Points));
}

// Make the point the indicated distance away from the center.
private void NormalizePoint(ref Point3D point,
Point3D center, double distance)
{
Vector3D vector = point - center;
point = center + vector / vector.Length * distance;
}```

The Subdivide method creates points A, B, C, D, E, F, and G. It calls the NormalizePoint method to make those points the correct distance from the sphere’s center and then uses the normalized points to build the 9 new triangles shown in Figure 1.

The NormalizePoint method finds a vector from the sphere’s center to a point. It divides the vector by its length so it has length 1, multiplies it by the desired radius, and adds the result to the sphere’s center. The result is a point in the same direction from the center of the sphere as the original point but with distance radius from the center.

The main program uses the following code snippet to build the geodesic sphere.

```// Build the initial icosahedron.
...

// The radius is the distance from
// the top point to the origin.
double radius = points.Y;

// Divide the triangles if desired
// to make the geodesic sphere.
Point3D origin = new Point3D(0, 0, 0);
for (int i = 0; i < Level; i++)
{
List<Triangle> new_triangles = new List<Triangle>();
foreach (Triangle triangle in triangles)
{
triangle.Subdivide(new_triangles, origin, radius);
}
triangles = new_triangles;
}```

The program first builds the initial icosahedron (not shown here). Then this code sets the radius equal to the Y coordinate of the top vertex.

Next the code enters a loop that ranges from 0 to Level - 1 to refine the sphere the desired number of times. Inside each loop, it creates a new_triangles list to hold the sphere’s subdivided triangles. It then loops through the triangles and calls their Subdivide methods to make new triangles in the new_triangles list. When it has finished subdividing the triangles, the code sets the triangles list equal to the new_triangles list and repeats if necessary.

After that the program works much as previous examples do. The only other interesting change is in the way the program draws the triangles’ edges when you check the Edges box.

You could simply draw all of every triangle’s edges but each edge is shared by two triangles so you would end up drawing each edge twice.

Instead the program uses an Edge class to represent the edges. This class uses the following code to implement the IEquatable<Edge> interface.

```public bool Equals(Edge other)
{
Vector3D v1 = this.Point1 - other.Point1;
Vector3D v2 = this.Point2 - other.Point2;
if ((v1.Length < 0.001) && (v2.Length < 0.001)) return true;

v1 = this.Point2 - other.Point1;
v2 = this.Point1 - other.Point2;
if ((v1.Length < 0.001) && (v2.Length < 0.001)) return true;
return false;
}```

This method returns true if this Edge is close enough to another Edge that we should consider them the same. The code checks the distance between the objects’ points in case the points are slightly different due to rounding errors.

The main program then builds a HashSet to contain Edge objects and loops through the triangles.

For each triangle, the program adds any edges that are not already in the HashSet to the HashSet.

When it is finished, the HashSet contains a list of the edges with each appearing only once. The program loops through the HashSet and adds the necessary segments to the 3D edge model.

This sounds more complicated than it really is. There are a few details to handle, but each is relatively easy if you use the Triangle and Edge classes. Download the example and see the code for additional details.     ## 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, graphics, mathematics, wpf and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

### 6 Responses to Build a geodesic sphere with WPF and C#

1. hazim says:

Wow!!
Iam very like your work, because your work very nice and perfect .

2. Phan Sang says:

Hi Rods, I don’t understand this section:
The first level sphere contains two kinds of triangles with angles:
54.6347°, 54.6347°, 70.7305°
58.5832°, 60.7084°, 60.7084°

Is there any formula to calculate these angles ? and how do you know they have 2, 12 or 85 kinds according to their level ?

• RodStephens says:

The first level is an icosahedron and the triangles are all equilateral. After that the program follows the method described in the post. It generates the new points and moves them to the edge of the sphere.

I pulled those angles from the program’s results. I don’t know of a formula for calculating them, other than to actually build the geodesic sphere.

As for the number of triangles in each level, you start with the icosahedron so you have 20 triangles. At each step you convert 9 new ones so you get 20 * 9 = 180 triangles at the second level, 180 * 9 = 1,620 at the third level, and so on.

3. Vlad Voina says:

Using two subdivision methods , I : 4 triangles , II : 9 .. I mixed them to find different quantities of faces ( 1so for example I , II would be applying a subdivide of 4 and using that output to feed into II method to further subdivide each of those 4 faces 9 times )

Here are the combinations with repetitions

0 = [ none ] = 20
1 = [ I ] = 80
1 = [ II ] = 180
2 = [ I , I ] = 320
2 = [ I , II ] = 720 ( also equals to II , I )
3 = [ I , I , I ] = 1,280
3 = [ I , I , II ] = 2,880 ( == II , I , I == I , II , I )
4 = [ I , I , I , I ] = 5,120
3 = [ I , II , II ] = 6,480
4 = [ I , I , I , II ] = 11,520
3 = [ II , II , II ] = 14,580
5 = [ I , I , I , I , I ] = 20,480
4 = [ I , I , II , II ] = 25,920
5 = [ I , I , I , I , II ] = 46,080
4 = [ I , II , II , II ] = 58,320
6 = [ I , I , I , I , I , I ] = 81,920
5 = [ I , I , I , II , II ] = 103,680
4 = [ II , II , II , II ] = 131,220
6 = [ I , I , I , I , I , II ] = 184,320
5 = [ I , I , II , II , II ] = 233,280
… ?
7 = [ I , I , I , I , I , I , I ] = 327,680
… ?

Multiplication sequence between the faces counts
4 , 2 + 1/4 , 1 + 7/9 , 2 + 1/4 , 1 + 7/9 , …

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