#include <solar.h>
#include <video.h>

#define nedg 30   
#define P EARTH
#define Rp RADIUS(P)
#define Mp MASS(P)
#define Synch SYNCHRONOUS(P)
double Skyvel, Tipvel, Twindow, Skyorbit, Skylen;

main(argc,argv)
int argc; char *argv[];
  {WINDOWS *north,*earth,*space; double w,x[nedg*2],y[nedg*2];
   int i;   double s,c,ss,cc,t,top,bot,px,py,vx,vy,ax,ay,a,d,dd,dt;

   Skyorbit = (Rp+1500*KM);
   Skylen = (750*KM);
   Skyvel = ORBITALVELOCITY(P,Skyorbit);
   Tipvel = Skyvel/2;
   Twindow =TWOPI*Skyorbit/Skyvel;  Twindow = Twindow/9;
   bot = .85*Rp;  top = Skyorbit+Skylen;

   w = COS(PI/nedg);
   north = READWINDOWSET("polar.img",-w,-w,w,w,"W");
   space = WINDOWSET(-1.3333*(top-bot)/2,bot,
                      1.3333*(top-bot)/2,top,"W");
   earth = SUBWINDOW(space,-Rp,-Rp,Rp,Rp,-w,-w,w,w);
   for (i=0; i<nedg*2; ++i)
      {x[i]=COS((TWOPI*i)/nedg);  y[i]=SIN((TWOPI*i)/nedg);}
   SELECT(earth);
   PATCH(nedg,x,y,north,&x[nedg/4],&y[nedg/4]);
   PEN(14); INK(.7);
   for (t = -1; t <= 1; t += 0.001)
      {c = COS(t);  s = SIN(t);
       INK(WINDOWPIXEL(earth,s*0.987,c*0.987));  DOT(0.99*s,0.99*c);}
   SELECT(space);
   
   PEN(10); INK(.6);
   for (t = -Twindow; t < Twindow; t += Twindow/500)
      {c=Skyorbit*SIN(t*Skyvel/Skyorbit);  s=Skyorbit*COS(t*Skyvel/Skyorbit);
       cc=Skylen*SIN(t*Tipvel/Skylen);    ss=Skylen*COS(t*Tipvel/Skylen);
       LINE(c+cc,s+ss,c-cc,s-ss);}
   for (t = -Twindow; t < Twindow; t += Twindow/15)
      {c=Skyorbit*SIN(t*Skyvel/Skyorbit);  s=Skyorbit*COS(t*Skyvel/Skyorbit);
       cc=Skylen*SIN(t*Tipvel/Skylen);    ss=Skylen*COS(t*Tipvel/Skylen);
       PEN(7); INK(1);
       LINE(c+cc,s+ss,c-cc,s-ss);
       if (t <= 0) {PEN(20); DOT(c-cc,s-ss);} }
   px = 0;  py = Skyorbit-Skylen;   vx = Skyvel-Tipvel;  vy = 0;
   PEN(12);
   dt = 30*SEC;
   do {DOT(px,py);
      dd = px*px + py*py;  d = SQRT(dd);
      a = GRAVITATION*Mp/dd;
      ax = -a*px/d;   ay = -a*py/d;
      vx += ax*dt;    vy += ay*dt;
      px += vx*dt;    py += vy*dt; }  while (d >= Rp);

   GRINNELLUP(0);
   FILEUP("rocksky.img");
   }

