HelixToolkit.Wpf.MeshBuilder.AddSphere C# (CSharp) Method

AddSphere() public method

Adds a sphere.
public AddSphere ( System.Windows.Media.Media3D.Point3D center, double radius, int thetaDiv = 20, int phiDiv = 10 ) : void
center System.Windows.Media.Media3D.Point3D /// The center of the sphere. ///
radius double /// The radius of the sphere. ///
thetaDiv int /// The number of divisions around the sphere. ///
phiDiv int /// The number of divisions from top to bottom of the sphere. ///
return void
        public void AddSphere(Point3D center, double radius, int thetaDiv = 20, int phiDiv = 10)
        {
            int index0 = this.Positions.Count;
            double dt = 2 * Math.PI / thetaDiv;
            double dp = Math.PI / phiDiv;

            for (int pi = 0; pi <= phiDiv; pi++)
            {
                double phi = pi * dp;

                for (int ti = 0; ti <= thetaDiv; ti++)
                {
                    // we want to start the mesh on the x axis
                    double theta = ti * dt;

                    // Spherical coordinates
                    // http://mathworld.wolfram.com/SphericalCoordinates.html
                    double x = Math.Cos(theta) * Math.Sin(phi);
                    double y = Math.Sin(theta) * Math.Sin(phi);
                    double z = Math.Cos(phi);

                    var p = new Point3D(center.X + (radius * x), center.Y + (radius * y), center.Z + (radius * z));
                    this.positions.Add(p);

                    if (this.normals != null)
                    {
                        var n = new Vector3D(x, y, z);
                        this.normals.Add(n);
                    }

                    if (this.textureCoordinates != null)
                    {
                        var uv = new Point(theta / (2 * Math.PI), phi / Math.PI);
                        this.textureCoordinates.Add(uv);
                    }
                }
            }

            this.AddRectangularMeshTriangleIndices(index0, phiDiv + 1, thetaDiv + 1, true);
        }

Usage Example

        public DemoElement3D()
        {
            var gm = new GeometryModel3D();
            var mb = new MeshBuilder();
            mb.AddSphere(new Point3D(0, 0, 0), 2, 100, 50);
            gm.Geometry = mb.ToMesh();
            gm.Material = Materials.Blue;

            Visual3DModel = gm;
        }
All Usage Examples Of HelixToolkit.Wpf.MeshBuilder::AddSphere