Advertisement
Guest User

Untitled

a guest
Dec 15th, 2014
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. double distance(int, int, double*, double*);
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.   FILE* output;
  10.   char answer_yn;
  11.   int validInput;
  12.   long int i = 0, j, max_recursion_number = 500000, iterations;
  13.   double t = 0.0001;
  14.   double collision = 0.1;
  15.   double v[2], m[2], a[2];
  16.   double *y,*x;
  17.   y = (double*)malloc(max_recursion_number*sizeof(double));
  18.   x = (double*)malloc(max_recursion_number*sizeof(double));
  19.   if(x == NULL || y == NULL)
  20.   {
  21.     printf("Memory allocation failure, exiting.");
  22.     return(EXIT_FAILURE);
  23.   }
  24.  
  25.   x[0] = 1;
  26.   y[0] = 0;
  27.   x[1] = -1;
  28.   y[1] = 0;
  29.   validInput = (argc == 7);
  30.   validInput = validInput && sscanf(argv[1],"%lf", &x[2]);
  31.   validInput = validInput && sscanf(argv[2],"%lf", &y[2]);
  32.   validInput = validInput && sscanf(argv[3],"%lf", &v[0]);
  33.   validInput = validInput && sscanf(argv[4],"%lf", &v[1]);
  34.   validInput = validInput && sscanf(argv[5],"%lf", &m[0]) && (m[0] >= 0);
  35.   validInput = validInput && sscanf(argv[6],"%lf", &m[1]) && (m[1] >= 0);
  36.   if(!validInput)
  37.    {
  38.     printf("Input validation failure, please use the program with x(0), y(0), vx(0), vy(0), m1, m2 as the commant line options, exiting. \n");      
  39.     return(EXIT_FAILURE);
  40.    }
  41.    
  42.   x[3] = x[2] + t*v[0];
  43.   y[3] = y[2] + t*v[1];
  44.  
  45.   for(i = 3; i < max_recursion_number-1; i++)
  46.   {
  47.     if(distance(0, i, x, y) < collision || distance(1, i, x, y) < collision)
  48.    {
  49.      printf("Collision occured in time %lf \n", i*t);
  50.      break;
  51.    }
  52.    
  53.    a[0] = -(m[0]*(x[i]-x[0])*pow(distance(0,i,x,y), (-3)) + m[1]*(x[i]-x[1])*pow(distance(1,i,x,y), (-3)));
  54.    a[1] = -(m[0]*(y[i]-y[0])*pow(distance(0,i,x,y), (-3)) + m[1]*(y[i]-y[1])*pow(distance(1,i,x,y), (-3)));
  55.    x[i+1] = t*t*a[0] + 2*x[i] - x[i-1];
  56.    y[i+1] = t*t*a[1] + 2*y[i] - y[i-1];
  57.   }
  58.  
  59.   if(i == max_recursion_number)
  60.   {
  61.     printf("All iterations successful, collision did not occur in given time \n");
  62.   }
  63.   iterations = i;
  64.  
  65.   output=fopen("output2.dat", "w");
  66.   if(output != (FILE*)NULL)
  67.   {
  68.    for(j=2; j<=i; j++)
  69.    {
  70.     fprintf(output, "%lf %lf %lf\n", x[j], y[j], (j-2)*t);
  71.    }
  72.    fclose(output);
  73.    printf("Trajectory written to output.dat \n");
  74.   }
  75.   else
  76.   {
  77.     printf("Error writing to file.");
  78.   }  
  79.  
  80.   //ASK HERE
  81.   while( (answer_yn != 'y')&&(answer_yn != 'n') )
  82.   {
  83.    printf("Plot?(y/n)");
  84.    answer_yn = getchar();  
  85.   }
  86.  
  87.   if(answer_yn == 'y')
  88.   {
  89.    char ans;
  90.    double min_total, max_total, scale;
  91.    int k, n, p, x_coord, y_coord, graph_size;
  92.    long int number_of_steps, time_scaling;
  93.    int graph[52][52];
  94.    graph_size = 50;  
  95.    printf("Enter the number of steps in which you want to display the motion: ");
  96.    scanf("%ld", &number_of_steps);
  97.    time_scaling = iterations/number_of_steps;
  98.    for(j = 0; j < graph_size+1; j++)
  99.    {
  100.     for(k = 0; k < graph_size+1; k++)
  101.     {
  102.       graph[j][k] = 1;
  103.     }
  104.    }
  105.    
  106.    for(j = 2; j<iterations; j++)
  107.    {
  108.      if((max_total - x[j]) < 0)
  109.      {
  110.        max_total = x[j];
  111.      }
  112.      if((min_total - x[j]) > 0)
  113.      {
  114.        min_total = x[j];
  115.      }
  116.    }
  117.        
  118.     for(j = 2; j<iterations; j++)
  119.     {
  120.      if((max_total - y[j]) < 0)
  121.      {
  122.        max_total = y[j];
  123.      }
  124.      if((min_total - y[j]) > 0)
  125.      {
  126.        min_total = y[j];
  127.      }
  128.     }
  129.    
  130.    scale = max_total - min_total;
  131.  
  132.    x_coord = graph_size*(1 - min_total)/scale;
  133.    y_coord = graph_size*(0 - min_total)/scale;
  134.    graph[x_coord][y_coord] = 8;
  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.     printf("checkpoint4");
  139.    j = 2;
  140.    printf("Usage: Enter to plot the next step; q to plot all the remaining steps and quit. \n An 8 marks the fixed objects, 0 - current location, 9 - trajectory (previous locations). \n");
  141.    ans = getchar();
  142.    for(p = 0; p < number_of_steps; p++)
  143.    {
  144.     if(ans != 'q')
  145.     {
  146.      printf("Continue?:");
  147.      ans = getchar();
  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.      graph[x_coord][y_coord] = 0;
  154.    
  155.      for(n = 0; n < graph_size+1; n++)
  156.      {
  157.       for(k = 0; k < graph_size+1; k++)
  158.       {
  159.         printf("%d", graph[k][graph_size-n]);
  160.       }
  161.       printf("\n");
  162.      }  
  163.    
  164.      j = j + time_scaling;
  165.      graph[x_coord][y_coord] = 9;
  166.     }
  167.     else
  168.     {
  169.      x_coord = graph_size*(x[j] - min_total)/scale;
  170.      y_coord = graph_size*(y[j] - min_total)/scale;
  171.      graph[x_coord][y_coord] = 0;
  172.      j = j + time_scaling;
  173.      graph[x_coord][y_coord] = 9;
  174.     }  
  175.    }
  176.    printf("Final result: \n");
  177.    for(n = 0; n < graph_size+1; n++)
  178.    {
  179.     for(k = 0; k < graph_size+1; k++)
  180.     {
  181.      printf("%d", graph[k][graph_size-n]);
  182.     }
  183.     printf("\n");
  184.     }  
  185.    
  186.   }
  187.  
  188.  
  189.   free(x);
  190.   free(y);
  191.  
  192.   return(0);
  193.    
  194. }
  195.  
  196. double distance(int a, int b, double* x, double* y)
  197. {
  198.   double e = 0;
  199.   e = sqrt((x[a] - x[b])*(x[a] - x[b]) + (y[a] - y[b])*(y[a] - y[b]));
  200.   return e;  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement