Click here to Skip to main content
11,924,800 members (54,347 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked

Triangle List Generator for Sphere

, 20 Dec 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This article demonstrates a simple and fast method for generating a list of regular triangles that form a unit sphere surface

Download Source for Visual Studio 2012



WPF lacks built-in 3D shapes such as sphere, cylinder and cube. In result, those who intend to add such shapes to their 3D scene have no choice rather than generating their mesh through additional coding. As I've already googled and searched through CodeProject for a simple solution for generating a sphere in WPF, I didn't find a clear code that I could rely on for a serious project. Some of them too long others with limitations and ambiguity.  The code that I submit here generates a triangle list and their normal vectors for a sphere with unit radius which can be then moved and resized by simple calculations. It’s written in VB.Net and can be easily converted to other .Net based languages. 

Using the code 

Triangle Structure 

For better manipulation of Trianlges and their Normals, the following structure is developed.

Public Structure Triangle
   Dim Point1 As Point3D
   Dim Point2 As Point3D
   Dim Point3 As Point3D
   Dim Normal As Vector3D
End Structure


The points in space should be located in such a way that they all have equal distance from center of sphere and also the enumerator of such points must be able to keep a record of neighboring points. This can be accomplished by converting the Cartesian coordinate to Spherical.   


The following shows how the parameters r, θ and ɸ replace the Cartesian X, Y and Z.   

As far as r is supposed to be unit it can be omitted. Figure below shows how the enumerator passes through points on sphere surface. It’s shown that ɸ enumerator can be used to find adjacent points on certain Z surface while θ enumerator iterates through all Z surfaces to generate the sphere surface points. 

A closer look onto the sphere surface reveals how the triangles can be generated using the algorithm. As it’s shown each θ iteration represents a point which may be common in 8 triangles. In order to prevent generating additional triangles, the algorithm locates two triangles on ɸ iteration forward and θ level backward as shown below.  

Now it's time to write the code.  

Generating Sphere Points  

The CreateTriangleListForSphere function returns an ArrayList that contains Triangles. In this function, At first, the following code generates the points on surface of sphere and adds them to PointList()() Array. The Density parameters determines the number of ɸ for sphere. 

For tita As Integer = 0 To Density
            Dim vtita As Double = tita * (Math.PI / Density)
            For nphi As Integer = -Density To Density
                Dim vphi As Double = nphi * (Math.PI / Density)
                PointList(tita)(nphi + Density).X = Math.Sin(vtita) * Math.Cos(vphi)
                PointList(tita)(nphi + Density).Y = Math.Sin(vtita) * Math.Sin(vphi)
                PointList(tita)(nphi + Density).Z = Math.Cos(vtita)

Generating Triangles 

Then, iteration below connects the point lists according to described algorithm. Triangle1 and Triangle2 are respective triangles explained and illustrated already. As far as sphere is located in origin the Normal vectors can be suggested easily as the location of points on sphere surface.  

Dim TriangleList As New ArrayList

       For n_tita As Integer = 1 To PointList.GetLength(0) - 1
           For n_phi As Integer = 0 To PointList(n_tita).GetLength(0) - 2

               Dim Triangle1, Triangle2 As Triangle

               Triangle1.Point1 = PointList(n_tita)(n_phi)
               Triangle1.Point2 = PointList(n_tita)(n_phi + 1)
               Triangle1.Point3 = PointList(n_tita - 1)(n_phi)
               Triangle1.Normal = New Vector3D(Triangle1.Point1.X, Triangle1.Point1.Y, Triangle1.Point1.Z)

               Triangle2.Point1 = PointList(n_tita)(n_phi + 1)
               Triangle2.Point2 = PointList(n_tita - 1)(n_phi + 1)
               Triangle2.Point3 = PointList(n_tita - 1)(n_phi)
               Triangle2.Normal = New Vector3D(Triangle1.Point1.X, Triangle1.Point1.Y, Triangle1.Point1.Z)




1) In order to enhance the application performance, the triangle list can be saved on hard drive and then
loaded in memory instead of calling the CreateTrianlgeListForSphere function each time a sphere is needed.

2) For bigger projects it’d better to use triangle strips rather than an ArrayList of Triangles . That will reduce use of memory, CPU and GPU significantly.

3) Two loops above can be easily merged for best performance.   


Adding a custom sphere requires calling CreateTriangleListForSphere and then resize and move the Triangle points: Point1, Point2 and Point3. The Normal vector remains intact. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Amir Emamjomeh
United Kingdom United Kingdom
Amir Emamjomeh is a senior petroleum reservoir engineer and also programmer who has been involved in several industrial and research projects proposed by the petroleum industry of Iran since 2006. His programming experiences include C, C++, C# , VB.Net, MATLAB and Visual FORTRAN.

You may also be interested in...

Comments and Discussions

QuestionThanks Pin
Member 1124602820-Nov-14 5:00
memberMember 1124602820-Nov-14 5:00 
AnswerRe: Thanks Pin
Amir Emamjomeh12-Jan-15 0:55
professionalAmir Emamjomeh12-Jan-15 0:55 
GeneralInteresting Pin
Meshack Musundi4-Jan-14 5:31
professionalMeshack Musundi4-Jan-14 5:31 
QuestionHexagon Pin
bling23-Dec-13 8:35
memberbling23-Dec-13 8:35 
AnswerRe: Hexagon Pin
The_Inventor23-Dec-13 17:05
memberThe_Inventor23-Dec-13 17:05 
GeneralRe: Hexagon Pin
bling24-Dec-13 6:09
memberbling24-Dec-13 6:09 
GeneralRe: Hexagon Pin
The_Inventor24-Dec-13 14:53
memberThe_Inventor24-Dec-13 14:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151125.3 | Last Updated 20 Dec 2013
Article Copyright 2013 by Amir Emamjomeh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid