<html>
  <head>
    <title>Wrap Sphere</title>
    <script type="text/javascript" src="nt3d.js"></script>
    <script type="text/javascript">
      function wrap_sphere(sphere_radius, extrusion_radius, turns, circle_angle, aspect_ratio, distortion_angle, steps_around, steps_along) {
        // Spiral path along sphere surface
        var path = [];
        for (var i = 0; i < steps_along; i++) {
          var progress_0to2 = i / (steps_along/2);
          var progress_0to1andback = progress_0to2 < 1 ? progress_0to2 : 2 - progress_0to2;
          var progress_neg1to1andback = progress_0to1andback * 2 - 1;
          var z = progress_neg1to1andback * sphere_radius;
          var r = Math.sqrt(sphere_radius*sphere_radius - z*z);
          var phase_adjust = progress_0to2 < 1 ? 0 : Math.PI;
          var x = r*Math.cos(progress_0to1andback*turns*2*Math.PI + phase_adjust);
          var y = r*Math.sin(progress_0to1andback*turns*2*Math.PI + phase_adjust);
          path.push([x,y,z]);
        }

        // Shape to be extruded
        var shape = nt3d.circle(extrusion_radius, steps_around);
        shape = nt3d.rotate_about_origin(shape, [0,0,1], circle_angle*Math.PI*2);
        for (var i = 0; i < shape.length; i++) {
          shape[i][0] *= aspect_ratio;
        }
        shape = nt3d.rotate_about_origin(shape, [0,0,1], distortion_angle*Math.PI*2);

        return nt3d.closed_extrude(
          path,
          shape,
          nt3d.shapenormals_from_closed_path(path),
          nt3d.pathnormals_from_point(path, [0, 0, 0]));
      }
      var params = [["Sphere radius", 100],
                    ["Extrusion radius", 5],
                    ["Turns", 3],
                    ["Angle of circle (in turns) (useful when steps_around is small)", .125],
                    ["Aspect ratio", 2],
                    ["Angle of distortion (in turns)", .25],
                    ["Steps around", 4],
                    ["Steps along", 500]];
    </script>
  </head>
  <body onload="nt3d.framework(wrap_sphere, params)">
    <h1>Wrap Sphere</h1>
    <p>Extrude something in a spiral along the surface of a sphere.</p>
  </body>
</html>