Advertisement
Guest User

Untitled

a guest
Dec 16th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <unistd.h>
  5. #include <sys/select.h>
  6.  
  7. double distance(int, int, double*, double*);
  8. int kbhit();
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.   FILE* output;
  13.   char answer_yn;
  14.   int validInput;
  15.   long int i = 0, j, max_recursion_number = 500000, iterations; //adjust max_recursion_number depending on cpu available
  16.   double t = 0.0001;  //works well for the current max_recursion_number, adjust if needed
  17.   double collision = 0.1;
  18.   double v[2], m[2], a[2];
  19.   double *y,*x;
  20.   y = (double*)malloc(max_recursion_number*sizeof(double));
  21.   x = (double*)malloc(max_recursion_number*sizeof(double));
  22.   if(x == NULL || y == NULL)
  23.   {
  24.     perror("malloc");
  25.     return(EXIT_FAILURE);
  26.   }
  27.  
  28.   x[0] = 1;
  29.   y[0] = 0;
  30.   x[1] = -1;
  31.   y[1] = 0;
  32.   validInput = (argc == 7);
  33.   validInput = validInput && sscanf(argv[1],"%lf", &x[2]);
  34.   validInput = validInput && sscanf(argv[2],"%lf", &y[2]);
  35.   validInput = validInput && sscanf(argv[3],"%lf", &v[0]);
  36.   validInput = validInput && sscanf(argv[4],"%lf", &v[1]);
  37.   validInput = validInput && sscanf(argv[5],"%lf", &m[0]) && (m[0] >= 0);
  38.   validInput = validInput && sscanf(argv[6],"%lf", &m[1]) && (m[1] >= 0);
  39.   if(!validInput)
  40.    {
  41.     fputs("Input validation failure, please use the program with x(0), y(0), vx(0), vy(0), m1, m2 as the command line options, exiting.\n", stderr);
  42.     return(EXIT_FAILURE);
  43.    }
  44.   x[3] = x[2] + t*v[0];
  45.   y[3] = y[2] + t*v[1];
  46.  
  47.   for(i = 3; i < max_recursion_number-1; i++)
  48.   {
  49.     if(distance(0, i, x, y) < collision || distance(1, i, x, y) < collision)
  50.    {
  51.      printf("Collision occured in time %lf \n", i*t);
  52.      break;
  53.    }
  54.    
  55.    a[0] = -(m[1]*(x[i]-x[0])*pow(distance(0,i,x,y), (-3)) + m[0]*(x[i]-x[1])*pow(distance(1,i,x,y), (-3)));
  56.    a[1] = -(m[1]*(y[i]-y[0])*pow(distance(0,i,x,y), (-3)) + m[0]*(y[i]-y[1])*pow(distance(1,i,x,y), (-3)));
  57.    x[i+1] = t*t*a[0] + 2*x[i] - x[i-1];
  58.    y[i+1] = t*t*a[1] + 2*y[i] - y[i-1];
  59.   }
  60.  
  61.   if(i == max_recursion_number)
  62.   {
  63.     printf("All iterations successful, collision did not occur in given time \n");
  64.   }
  65.   iterations = i;
  66.  
  67.   // Now write the output into file
  68.  
  69.   output=fopen("output2.dat", "w");
  70.   if(output != (FILE*)NULL)
  71.   {
  72.    for(j=2; j<=i; j++)
  73.    {
  74.     fprintf(output, "%lf %lf %lf\n", x[j], y[j], (j-2)*t);
  75.    }
  76.    fclose(output);
  77.    printf("Trajectory written to output2.dat \n");
  78.   }
  79.   else
  80.   {
  81.     printf("Error writing to file.");
  82.   }  
  83.  
  84. // Plotting starts here
  85.   while( (answer_yn != 'y')&&(answer_yn != 'n') )
  86.   {
  87.    printf("Plot?(y/n)");
  88.    answer_yn = getchar();  
  89.   }
  90.  
  91.   if(answer_yn == 'y')
  92.   {
  93.    double min_total, max_total, scale;
  94.    int k, n, p, x_coord, y_coord, graph_size;
  95.    long int number_of_steps, time_scaling;
  96.    int graph[52][52], ans, ans2; //int ans intead of char because of EOF trap
  97.    graph_size = 50;  
  98.    printf("Enter the number of steps in which you want to display the motion: ");
  99.    scanf("%ld", &number_of_steps);
  100.    time_scaling = iterations/number_of_steps;
  101.    for(j = 0; j < graph_size+1; j++)
  102.    {
  103.     for(k = 0; k < graph_size+1; k++)
  104.     {
  105.       graph[j][k] = 1;
  106.     }
  107.    }
  108.    
  109.    for(j = 2; j<iterations; j++)
  110.    {
  111.      if((max_total - x[j]) < 0)
  112.      {
  113.        max_total = x[j];
  114.      }
  115.      if((min_total - x[j]) > 0)
  116.      {
  117.        min_total = x[j];
  118.      }
  119.    }
  120.        
  121.     for(j = 2; j<iterations; j++)
  122.     {
  123.      if((max_total - y[j]) < 0)
  124.      {
  125.        max_total = y[j];
  126.      }
  127.      if((min_total - y[j]) > 0)
  128.      {
  129.        min_total = y[j];
  130.      }
  131.     }
  132.    
  133.    scale = max_total - min_total;
  134.  
  135.    x_coord = graph_size*(1 - min_total)/scale;
  136.    y_coord = graph_size*(0 - min_total)/scale;
  137.    graph[x_coord][y_coord] = 8;
  138.    x_coord = graph_size*(-1 - min_total)/scale;
  139.    y_coord = graph_size*(0 - min_total)/scale;
  140.    graph[x_coord][y_coord] = 8;
  141.    j = 2;
  142.    printf("Usage: a for automatic mode, m for manual mode,\n in manual: Enter to plot the next step; q to plot all the steps and quit. \n An 8 marks the fixed objects, 0 - current location, 9 - trajectory (previous locations). \n Hint: press and hold Enter in manual mode \n Hint: you can enter automatic mode at any time. \n So: ");
  143.    scanf("%s", &ans);
  144.    for(p = 0; p < number_of_steps; p++)
  145.    {
  146.     if(ans == 'a')
  147.     {
  148.      x_coord = graph_size*(x[j] - min_total)/scale;
  149.      y_coord = graph_size*(y[j] - min_total)/scale;
  150.      printf("%d %d \n", x_coord, y_coord);
  151.      printf("Iteration: %ld/%ld   ", j, iterations);
  152.      printf("Step: %d/%d \n", p, number_of_steps);
  153.      if(kbhit())
  154.      {
  155.        printf("Continue? (y to continue, n to stop and quit)");
  156.        answer_yn = '0';
  157.        while( (answer_yn != 'y')&&(answer_yn != 'n') )
  158.        {
  159.         answer_yn = getchar();  
  160.        }
  161.        if(answer_yn == 'n')
  162.        {
  163.        ans = 'q';
  164.        }
  165.      }
  166.      graph[x_coord][y_coord] = 0;
  167.    
  168.      for(n = 0; n < graph_size+1; n++)
  169.      {
  170.       for(k = 0; k < graph_size+1; k++)
  171.       {
  172.         printf("%d", graph[k][graph_size-n]);
  173.       }
  174.       printf("\n");
  175.      }
  176.           usleep(10000);
  177.    
  178.      j = j + time_scaling;
  179.      graph[x_coord][y_coord] = 9;
  180.     }
  181.    
  182.     else if(ans == 'm')
  183.     {
  184.      printf("Continue?:");
  185.      ans2 = getchar();
  186.      if(ans2 == 'a') ans = 'a';
  187.      if(ans2 == 'q') ans = 'q';
  188.      x_coord = graph_size*(x[j] - min_total)/scale;
  189.      y_coord = graph_size*(y[j] - min_total)/scale;
  190.      printf("%d %d \n", x_coord, y_coord);
  191.      printf("Iteration: %ld/%ld   ", j, iterations);
  192.      printf("Step: %d/%d \n", p, number_of_steps);
  193.      graph[x_coord][y_coord] = 0;
  194.    
  195.      for(n = 0; n < graph_size+1; n++)
  196.      {
  197.       for(k = 0; k < graph_size+1; k++)
  198.       {
  199.         printf("%d", graph[k][graph_size-n]);
  200.       }
  201.       printf("\n");
  202.      }  
  203.    
  204.      j = j + time_scaling;
  205.      graph[x_coord][y_coord] = 9;
  206.     }
  207.    
  208.     else if(ans == 'q')
  209.     {
  210.      x_coord = graph_size*(x[j] - min_total)/scale;
  211.      y_coord = graph_size*(y[j] - min_total)/scale;
  212.      graph[x_coord][y_coord] = 0;
  213.      j = j + time_scaling;
  214.      graph[x_coord][y_coord] = 9;
  215.     }
  216.     else
  217.     {
  218.       printf("enter a valit character next time, asshole \n");
  219.       break;
  220.     }
  221.    }
  222.  
  223.    if(ans == 'q')
  224.    {
  225.    printf("Final result: \n");
  226.    for(n = 0; n < graph_size+1; n++)
  227.    {
  228.     for(k = 0; k < graph_size+1; k++)
  229.     {
  230.      printf("%d", graph[k][graph_size-n]);
  231.     }
  232.     printf("\n");
  233.     }
  234.    }
  235.  
  236.   }
  237.  
  238.  
  239.  
  240.   free(x);
  241.   free(y);
  242.  
  243.   return(0);
  244.    
  245. }
  246.  
  247. double distance(int a, int b, double* x, double* y)
  248. {
  249.   double e = 0;
  250.   e = sqrt((x[a] - x[b])*(x[a] - x[b]) + (y[a] - y[b])*(y[a] - y[b]));
  251.   return e;  
  252. }
  253.  
  254. int kbhit()
  255. {
  256.   struct timeval tv;
  257.   fd_set fds;
  258.   tv.tv_sec = 0;
  259.   tv.tv_usec = 0;
  260.   FD_ZERO(&fds);
  261.   FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
  262.   select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
  263.   return FD_ISSET(STDIN_FILENO, &fds);
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement