Herowaree

position_generator

Dec 10th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3.  
  4. #include <math.h>
  5.  
  6. double halton(int index, int base) {
  7. double fraction = 1;
  8. double result = 0;
  9.  
  10. while(index > 0) {
  11. fraction /= base;
  12. result += fraction * (index % base);
  13. index = floor(index / base);
  14. }
  15.  
  16. return result;
  17. }
  18.  
  19. double euclidianDistance(double x1, double x2, double y1, double y2){
  20. return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
  21. }
  22.  
  23. bool happy(std::vector<double> x_list, std::vector<double> y_list, double x_point, double y_point, double min_distance){
  24.  
  25. for(unsigned i = 0; i < x_list.size(); i++) {
  26. double dist = euclidianDistance(x_list[i], x_point, y_list[i], y_point);
  27. if(dist < min_distance){
  28. return false;
  29. }
  30. }
  31.  
  32. return true;
  33. }
  34.  
  35. int main() {
  36. double min_distance = 5.0;
  37.  
  38. int num_uavs = 4;
  39. int x_base = 2;
  40. int y_base = 3;
  41. int counter = 0;
  42.  
  43. double field_size = min_distance * num_uavs;
  44.  
  45. int x_index = 1;
  46. int y_index = 1;
  47.  
  48. std::vector<double> x;
  49. std::vector<double> y;
  50.  
  51. while(x.size() < num_uavs){
  52.  
  53. if(x.size() == 0){
  54. x.push_back(0.0);
  55. y.push_back(0.0);
  56. } else {
  57. double new_x = field_size * halton(x_index, x_base);
  58. double new_y = field_size * halton(y_index, y_base);
  59. if(happy(x, y, new_x, new_y, min_distance)) {
  60. x.push_back(new_x);
  61. y.push_back(new_y);
  62. }
  63. }
  64.  
  65. x_index += 1;
  66. y_index += 1;
  67. }
  68.  
  69. for(unsigned i = 0; i < x.size(); i++) {
  70. std::cout << "uav" << i+1 << " - x: " << x[i] << " / y: " << y[i] << "\n";
  71. }
  72.  
  73. for(unsigned i = 0; i < x.size()-1; i++) {
  74. for(unsigned j = i+1; j < x.size(); j++) {
  75. std::cout << "distance between uav" << i+1 << " and the uav" << j+1 << " is " << euclidianDistance(x[i], x[j], y[i], y[j]) << " meters \n";
  76. }
  77. }
  78.  
  79. return 0;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment