.PS
# Spiral.m4 (Uses Circuit_macros library)
# threeD_init Version 9.2: TikZ PGF m4 macro settings.
  
# threeD_init begin
  # gen_init Version 9.2: TikZ PGF m4 macro settings.
  

  define rpoint__ {
    rp_wid = last line.end.x-last line.start.x
    rp_ht = last line.end.y-last line.start.y
    rp_len = sqrt(abs((rp_wid)^2+(rp_ht)^2)); move to last line.start
    rp_ang = atan2(rp_ht,rp_wid) }

  rtod_ = 57.295779513082323;  dtor_ = 0.017453292519943295
  twopi_ = 6.2831853071795862;  pi_ = (twopi_/2)
  rp_ang = 0;  right
  
  
  linethick = 0.8
  arrowwid = 0.05*scale; arrowht = 0.1*scale 
  
  
  # gen_init end

  
# setview
 m4azim=(20)*(dtor_)
 m4elev=(20)*(dtor_)
 m4rot =0
 m4caz=cos(m4azim)
 m4saz=sin(m4azim)
 m4cel=cos(m4elev)
 m4sel=sin(m4elev)
 m4cro=1
 m4sro=0
 view3D1=m4caz*m4cel
 view3D2=m4saz*m4cel
 view3D3=m4sel  ; light3D1=view3D1; light3D2=view3D2; light3D3=view3D3


# threeD_init end
if "dpictools" != "1" then {
  copy "/cygdrive/d/Dwight/lib/dpictools.pic" }   

  azimuth = 15                     # Set view angles in degrees
  elevation = 35
  
# setview
 m4azim=(azimuth)*(dtor_)
 m4elev=(elevation)*(dtor_)
 m4rot =0
 m4caz=cos(m4azim)
 m4saz=sin(m4azim)
 m4cel=cos(m4elev)
 m4sel=sin(m4elev)
 m4cro=1
 m4sro=0
 view3D1=m4caz*m4cel
 view3D2=m4saz*m4cel
 view3D3=m4sel  ; light3D1=view3D1; light3D2=view3D2; light3D3=view3D3


  crad = 1                         # Sphere radius
  thetamin = -89                   # Rotation about the y axis (deg)
  thetamax = 89
  turns = 6

  South: 0,0                       # South pole
         # North pole
  North: -((sin(m4rot))*(  (cos(m4elev))*(2*crad))),        (cos(m4rot))*(  (cos(m4elev))*(2*crad))
           # Centre of the sphere
  C: -((sin(m4rot))*(  (cos(m4elev))*(crad))),        (cos(m4rot))*(  (cos(m4elev))*(crad))
                                   # Shaded sphere at C
  g = 0.65
  Hlight: C+((crad*g)*(cos((135)*dtor_)),(crad*g)*(sin((135)*dtor_)))
  rm = crad*(1+g)
  n = int(rm/(linethick *(scale/72)))
  lt = linethick * 2
  for i=1 to n do {
    r = i/n*rm
    u = 1-(i/n)^2
    if r < (1-g)*crad then {
      circle thick lt outlined sprintf("{rgb,1:red,%7.5f;green,%7.5f;blue,%7.5f}",u,u,u) rad r at Hlight }\
    else {
      arc cw thick lt outlined sprintf("{rgb,1:red,%7.5f;green,%7.5f;blue,%7.5f}",u,u,u) \
        from             ((((((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2)+(r)^2-(crad)^2)/2)/(((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2) between Hlight and C) +\
   ((sqrt(max(0,(((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2)*(r)^2-(((((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2)+(r)^2-(crad)^2)/2)^2))/(((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2))*(-((C).y-(Hlight).y)),(sqrt(max(0,(((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2)*(r)^2-(((((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2)+(r)^2-(crad)^2)/2)^2))/(((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2))*(((C).x-(Hlight).x))) \
        to             ((((((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2)+(r)^2-(crad)^2)/2)/(((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2) between Hlight and C) -\
   ((sqrt(max(0,(((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2)*(r)^2-(((((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2)+(r)^2-(crad)^2)/2)^2))/(((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2))*(-((C).y-(Hlight).y)),(sqrt(max(0,(((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2)*(r)^2-(((((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2)+(r)^2-(crad)^2)/2)^2))/(((C).x-(Hlight).x)^2+((C).y-(Hlight).y)^2))*(((C).x-(Hlight).x))) with .c at Hlight }
    }
  circle rad crad +linethick *(scale/72)/2 at C

# shadedball(crad,0.65*crad,135) at C

                                   # The parametric spiral curve S(theta)
                                   # theta is degrees rotation about y
                                   # Rotation about z is 2*turns*theta


                                   # The visibility function is the dot product
                                   # of the view vector and the radius vector
define Radiusvis { $2 = (((((cos((-2*turns*$1*dtor_)))*((cos((-$1*dtor_)))*(crad)))*(view3D1)+(  (sin((-2*turns*$1*dtor_)))*((cos((-$1*dtor_)))*(crad)))*(view3D2))+(  -((sin((-$1*dtor_)))*(crad)))*(view3D3))) }

  dphi = 5                         # Horiz degrees between test pts on the curve
  dt = (dphi)/(2*turns)
  n = 0
  P[n]: ((cos(m4rot))*(  ((sin(-m4azim))*((cos((-2*turns*thetamin*dtor_)))*((cos((-thetamin*dtor_)))*(crad)))+(cos(-m4azim))*(  (sin((-2*turns*thetamin*dtor_)))*((cos((-thetamin*dtor_)))*(crad)))))-((sin(m4rot))*(  ((cos(m4elev))*(crad  -((sin((-thetamin*dtor_)))*(crad)))-((sin(m4elev))*(((cos(-m4azim))*((cos((-2*turns*thetamin*dtor_)))*((cos((-thetamin*dtor_)))*(crad)))-((sin(-m4azim))*(  (sin((-2*turns*thetamin*dtor_)))*((cos((-thetamin*dtor_)))*(crad))))))))))),        ((sin(m4rot))*(  ((sin(-m4azim))*((cos((-2*turns*thetamin*dtor_)))*((cos((-thetamin*dtor_)))*(crad)))+(cos(-m4azim))*(  (sin((-2*turns*thetamin*dtor_)))*((cos((-thetamin*dtor_)))*(crad)))))+(cos(m4rot))*(  ((cos(m4elev))*(crad  -((sin((-thetamin*dtor_)))*(crad)))-((sin(m4elev))*(((cos(-m4azim))*((cos((-2*turns*thetamin*dtor_)))*((cos((-thetamin*dtor_)))*(crad)))-((sin(-m4azim))*(  (sin((-2*turns*thetamin*dtor_)))*((cos((-thetamin*dtor_)))*(crad))))))))))
  Radiusvis(thetamin,xs)           # Visibility of the initial pt
  for theta=thetamin+dt to thetamax by dt do {
    Radiusvis(theta,xf)            # Visibility of the next pt
    if xs*xf < 0 then {
      findroot( Radiusvis, theta-dt,theta,1e-8,x ) # Find the transition pt
      n+=1; P[n]: ((cos(m4rot))*(  ((sin(-m4azim))*((cos((-2*turns*x*dtor_)))*((cos((-x*dtor_)))*(crad)))+(cos(-m4azim))*(  (sin((-2*turns*x*dtor_)))*((cos((-x*dtor_)))*(crad)))))-((sin(m4rot))*(  ((cos(m4elev))*(crad  -((sin((-x*dtor_)))*(crad)))-((sin(m4elev))*(((cos(-m4azim))*((cos((-2*turns*x*dtor_)))*((cos((-x*dtor_)))*(crad)))-((sin(-m4azim))*(  (sin((-2*turns*x*dtor_)))*((cos((-x*dtor_)))*(crad))))))))))),        ((sin(m4rot))*(  ((sin(-m4azim))*((cos((-2*turns*x*dtor_)))*((cos((-x*dtor_)))*(crad)))+(cos(-m4azim))*(  (sin((-2*turns*x*dtor_)))*((cos((-x*dtor_)))*(crad)))))+(cos(m4rot))*(  ((cos(m4elev))*(crad  -((sin((-x*dtor_)))*(crad)))-((sin(m4elev))*(((cos(-m4azim))*((cos((-2*turns*x*dtor_)))*((cos((-x*dtor_)))*(crad)))-((sin(-m4azim))*(  (sin((-2*turns*x*dtor_)))*((cos((-x*dtor_)))*(crad))))))))))
      if xs < 0 then { 
  dfitcurve(P,n,dashed)  } else { 
  dfitcurve(P,n)  }
      P[0]: P[n]
      n = 0
      xs = -xs
      }
    n+=1; P[n]: ((cos(m4rot))*(  ((sin(-m4azim))*((cos((-2*turns*theta*dtor_)))*((cos((-theta*dtor_)))*(crad)))+(cos(-m4azim))*(  (sin((-2*turns*theta*dtor_)))*((cos((-theta*dtor_)))*(crad)))))-((sin(m4rot))*(  ((cos(m4elev))*(crad  -((sin((-theta*dtor_)))*(crad)))-((sin(m4elev))*(((cos(-m4azim))*((cos((-2*turns*theta*dtor_)))*((cos((-theta*dtor_)))*(crad)))-((sin(-m4azim))*(  (sin((-2*turns*theta*dtor_)))*((cos((-theta*dtor_)))*(crad))))))))))),        ((sin(m4rot))*(  ((sin(-m4azim))*((cos((-2*turns*theta*dtor_)))*((cos((-theta*dtor_)))*(crad)))+(cos(-m4azim))*(  (sin((-2*turns*theta*dtor_)))*((cos((-theta*dtor_)))*(crad)))))+(cos(m4rot))*(  ((cos(m4elev))*(crad  -((sin((-theta*dtor_)))*(crad)))-((sin(m4elev))*(((cos(-m4azim))*((cos((-2*turns*theta*dtor_)))*((cos((-theta*dtor_)))*(crad)))-((sin(-m4azim))*(  (sin((-2*turns*theta*dtor_)))*((cos((-theta*dtor_)))*(crad))))))))))
    }
  n+=1; P[n]: ((cos(m4rot))*(  ((sin(-m4azim))*((cos((-2*turns*thetamax*dtor_)))*((cos((-thetamax*dtor_)))*(crad)))+(cos(-m4azim))*(  (sin((-2*turns*thetamax*dtor_)))*((cos((-thetamax*dtor_)))*(crad)))))-((sin(m4rot))*(  ((cos(m4elev))*(crad  -((sin((-thetamax*dtor_)))*(crad)))-((sin(m4elev))*(((cos(-m4azim))*((cos((-2*turns*thetamax*dtor_)))*((cos((-thetamax*dtor_)))*(crad)))-((sin(-m4azim))*(  (sin((-2*turns*thetamax*dtor_)))*((cos((-thetamax*dtor_)))*(crad))))))))))),        ((sin(m4rot))*(  ((sin(-m4azim))*((cos((-2*turns*thetamax*dtor_)))*((cos((-thetamax*dtor_)))*(crad)))+(cos(-m4azim))*(  (sin((-2*turns*thetamax*dtor_)))*((cos((-thetamax*dtor_)))*(crad)))))+(cos(m4rot))*(  ((cos(m4elev))*(crad  -((sin((-thetamax*dtor_)))*(crad)))-((sin(m4elev))*(((cos(-m4azim))*((cos((-2*turns*thetamax*dtor_)))*((cos((-thetamax*dtor_)))*(crad)))-((sin(-m4azim))*(  (sin((-2*turns*thetamax*dtor_)))*((cos((-thetamax*dtor_)))*(crad))))))))))
  if xs < 0 then { 
  dfitcurve(P,n,dashed)  } else { 
  dfitcurve(P,n)  }

                                   # Axes
  X: ((cos(m4rot))*(  (sin(-m4azim))*(crad))-((sin(m4rot))*(  -((sin(m4elev))*((cos(-m4azim))*(crad)))))),        ((sin(m4rot))*(  (sin(-m4azim))*(crad))+(cos(m4rot))*(  -((sin(m4elev))*((cos(-m4azim))*(crad)))))
  Y: ((cos(m4rot))*(  (cos(-m4azim))*(crad))-((sin(m4rot))*(  -((sin(m4elev))*(-((sin(-m4azim))*(crad))))))),        ((sin(m4rot))*(  (cos(-m4azim))*(crad))+(cos(m4rot))*(  -((sin(m4elev))*(-((sin(-m4azim))*(crad))))))

define linevis { # ratio           # Visibility for lines fom South to Tmp
  $2 = sqrt(abs(((($1 between South and Tmp)).x-(C).x)^2+((($1 between South and Tmp)).y-(C).y)^2))-crad }

define invisline { # name          # Draw dashed invisible part of line in
  Tmp: $1                          # the plane
  dw = 3*(scale/72.27)
  findroot( linevis, 0, 1, 1e-8, x )
  line dashed dw from South to x between South and Tmp chop 0 chop dw/2 }

linethick = 0.4
  arrowwid = 0.04*scale; arrowht = 0.2/3*scale 
  invisline(X)
  arrow to X chop dw/2 chop 0; "$x$" rjust
  invisline(Y)
  arrow to Y chop dw/2 chop 0; "$y$" ljust
  line dashed from South to North chop 0 chop dashwid
  arrow up crad*0.5 chop 0.05 chop 0 ; "$z$" ljust

.PE
