// Finds fastest trajectory for sliding from (0,y0) to (x,0) #include #include #include const double PI=4.0*atan(1.0); using namespace std; void GetX1(double a,double y,double &x,double &dxda){ double theta,root; root=sqrt(2.0*a*y-y*y); theta=acos(1.0-y/a); x=-root+a*theta; dxda=-(2.0*y/root)+theta; } void GetX2(double a,double y,double &x,double &dxda){ double theta,root; root=sqrt(2.0*a*y-y*y); theta=acos(1.0-y/a); theta=2.0*PI-theta; x=-root+a*theta; dxda=theta; } int main(int argc,char *argv[]){ double a,y0,x,dxda,xtarget; int ntries; printf("Enter xtarget and y0: "); scanf("%lf %lf",&xtarget,&y0); a=3.0; ntries=0; do{ ntries+=1; if(xtarget1.0E-10 && ntries<10); printf("x=%g, a=%g\n",x,a); // Print trajectory double time=0.0,y; printf(" x y\n"); printf("%8.5f %8.5f\n",0.0,y0); for(y=0.05*a;y<2.0*a;y+=0.1*a){ GetX1(a,y,x,dxda); printf("%8.5f %8.5f\n",x,y0-y); } for(y=2.0*a-0.05*a;y>0;y-=0.1*a){ GetX2(a,y,x,dxda); printf("%8.5f %8.5f\n",x,y0-y); } }