SHARE
TWEET

Trig_Blog_Sample_3

KeithS Aug 2nd, 2012 64 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.    
  26.     float angle = atan2f(y_dist, x_dist); // The differences in this and the following two lines.
  27.     float x_ratio = cosf(angle);
  28.     float y_ratio = sinf(angle);
  29.     float x_spd_comp = my_ship.spd * x_ratio;
  30.     float y_spd_comp = my_ship.spd * y_ratio;
  31.  
  32.     printf("Ship X_dest = %.2f,  Ship Y_dest = %.2f\n\n", my_ship.x_dest, my_ship.y_dest);
  33.  
  34.     while((my_ship.x_pos < (my_ship.x_dest - (my_ship.spd / 2.0f))) ||
  35.           (my_ship.x_pos > (my_ship.x_dest + (my_ship.spd / 2.0f))) ||
  36.           (my_ship.y_pos < (my_ship.y_dest - (my_ship.spd / 2.0f))) ||
  37.           (my_ship.y_pos > (my_ship.y_dest + (my_ship.spd / 2.0f))))
  38.     {
  39.         my_ship.x_pos += x_spd_comp;
  40.         my_ship.y_pos += y_spd_comp;
  41.         printf("Ship X Pos = %.2f,  Ship Y Pos = %.2f\n", my_ship.x_pos, my_ship.y_pos);
  42.         usleep(75000);
  43.     }
  44.     return 0;
  45. }
RAW Paste Data
Want to get better at C++?
Learn to code C++ in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top