Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Math reference of the sphere @ http://paulbourke.net/geometry/circlesphere/.
- /// Code reference of the sphere @ http://csharphelper.com/blog/2015/04/draw-spheres-using-wpf-and-c/.
- /// </summary>
- /// <param name="mesh">Mesh that holds the object.</param>
- /// <param name="center"></param>
- /// <param name="radius">Spheres' radius (size of sphere).</param>
- /// <param name="num_phi">Lines of latitude (Horizontal).</param>
- /// <param name="num_theta">Lines of longitude (Vertical).</param>
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement