Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. /// Math reference of the sphere @ http://paulbourke.net/geometry/circlesphere/.
  2.         /// Code reference of the sphere @ http://csharphelper.com/blog/2015/04/draw-spheres-using-wpf-and-c/.
  3.         /// </summary>
  4.         /// <param name="mesh">Mesh that holds the object.</param>
  5.         /// <param name="center"></param>
  6.         /// <param name="radius">Spheres' radius (size of sphere).</param>
  7.         /// <param name="num_phi">Lines of latitude (Horizontal).</param>
  8.         /// <param name="num_theta">Lines of longitude (Vertical).</param>
  9.         private void AddSphere(MeshGeometry3D mesh, Point3D center, double radius, int num_phi, int num_theta)
  10.         {
  11.             double phi0, theta0;
  12.             double dphi = Math.PI / num_phi;
  13.             double dtheta = 2 * Math.PI / num_theta;
  14.  
  15.             phi0 = 0;
  16.             double y0 = radius * Math.Cos(phi0);
  17.             double r0 = radius * Math.Sin(phi0);
  18.             for (int i = 0; i < num_phi; i++)
  19.             {
  20.                 double phi1 = phi0 + dphi;
  21.                 double y1 = radius * Math.Cos(phi1);
  22.                 double r1 = radius * Math.Sin(phi1);
  23.  
  24.                 // Point ptAB has phi value A and theta value B.
  25.                 // For example, pt01 has phi = phi0 and theta = theta1.
  26.                 // Find the points with theta = theta0.
  27.                 theta0 = 0;
  28.                 Point3D pt00 = new Point3D(center.X + r0 * Math.Cos(theta0), center.Y + y0, center.Z + r0 * Math.Sin(theta0));
  29.                 Point3D pt10 = new Point3D(center.X + r1 * Math.Cos(theta0), center.Y + y1, center.Z + r1 * Math.Sin(theta0));
  30.                 for (int j = 0; j < num_theta; j++)
  31.                 {
  32.                     // Find the points with theta = theta1.
  33.                     double theta1 = theta0 + dtheta;
  34.                     Point3D pt01 = new Point3D(center.X + r0 * Math.Cos(theta1), center.Y + y0, center.Z + r0 * Math.Sin(theta1));
  35.                     Point3D pt11 = new Point3D(center.X + r1 * Math.Cos(theta1), center.Y + y1, center.Z + r1 * Math.Sin(theta1));
  36.  
  37.                     // Create the triangles.
  38.                     AddTriangle(mesh, pt00, pt11, pt10);
  39.                     AddTriangle(mesh, pt00, pt01, pt11);
  40.  
  41.                     // Move to the next value of theta.
  42.                     theta0 = theta1;
  43.                     pt00 = pt01;
  44.                     pt10 = pt11;
  45.                 }
  46.  
  47.                 // Move to the next value of phi.
  48.                 phi0 = phi1;
  49.                 y0 = y1;
  50.                 r0 = r1;
  51.             }
  52.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement