fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. // range : [min, max]
  6. int random(int min, int max) {
  7. static bool first = true;
  8. if (first) {
  9. srand(time(NULL)); //seeding for the first time only!
  10. first = false;
  11. }
  12. return min + rand() % (max - min + 1);
  13. }
  14.  
  15. std::pair<int,int> rand_heading_point(int start_x, int start_y, int max_x, int max_y, int heading_dir) {
  16. int turn_x = 0;
  17. int turn_y = 0;
  18.  
  19. if (heading_dir <= 90) {
  20. turn_x = random(start_x, max_x);
  21. turn_y = random(start_y, max_y);
  22. } else if (heading_dir <= 180) {
  23. turn_x = random(0, start_x);
  24. turn_y = random(start_y, max_y);
  25. } else if (heading_dir <= 270) {
  26. turn_x = random(0, max_y);
  27. turn_y = random(0, start_y);
  28. } else {
  29. turn_x = random(start_x, max_x);
  30. turn_y = random(0, start_y);
  31. }
  32.  
  33. return std::make_pair(turn_x, turn_y);
  34. }
  35.  
  36. int main() {
  37. // your code goes here
  38.  
  39. int start_x = 100;
  40. int start_y = 100;
  41. int width = 500;
  42. int height = 500;
  43. int dir = 80;
  44.  
  45. auto heading_point = rand_heading_point(start_x, start_y, width, height, dir);
  46.  
  47. cout << "x: " << heading_point.first << ", y: " << heading_point.second << endl;
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
x: 328, y: 217