SHARE
TWEET

Trig_Blog_Sample_2

KeithS Aug 2nd, 2012 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <math.h>
  2. #include <cstdio>
  3. #include <unistd.h>
  4.  
  5. int main()
  6. {
  7.     struct ship
  8.     {
  9.         float x_pos;
  10.         float y_pos;
  11.         float x_dest;
  12.         float y_dest;
  13.         float spd;
  14.     };
  15.  
  16.     ship my_ship;
  17.     my_ship.spd = 10.0f;
  18.     my_ship.x_pos = 0.0f;
  19.     my_ship.y_pos = 0.0f;
  20.     my_ship.x_dest = 2000.0f;
  21.     my_ship.y_dest = 1500.0f;
  22.  
  23.     float x_dist = my_ship.x_dest - my_ship.x_pos;
  24.     float y_dist = my_ship.y_dest - my_ship.y_pos;
  25.     float slant_distance = sqrtf(powf(x_dist, 2.0f) + powf(y_dist, 2.0f));
  26.     float x_ratio = x_dist / slant_distance;
  27.     float y_ratio = y_dist / slant_distance;
  28.     float x_spd_comp = my_ship.spd * x_ratio;
  29.     float y_spd_comp = my_ship.spd * y_ratio;
  30.  
  31.     printf("Ship X_dest = %.2f,  Ship Y_dest = %.2f\n\n", my_ship.x_dest, my_ship.y_dest);
  32.  
  33.     while((my_ship.x_pos < (my_ship.x_dest - (my_ship.spd / 2.0f))) ||
  34.           (my_ship.x_pos > (my_ship.x_dest + (my_ship.spd / 2.0f))) ||
  35.           (my_ship.y_pos < (my_ship.y_dest - (my_ship.spd / 2.0f))) ||
  36.           (my_ship.y_pos > (my_ship.y_dest + (my_ship.spd / 2.0f))))
  37.     {
  38.         my_ship.x_pos += x_spd_comp;
  39.         my_ship.y_pos += y_spd_comp;
  40.         printf("Ship X Pos = %.2f,  Ship Y Pos = %.2f\n", my_ship.x_pos, my_ship.y_pos);
  41.         usleep(75000);
  42.     }
  43.     return 0;
  44. }
RAW Paste Data
Top