Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- #include <math.h>
- double halton(int index, int base) {
- double fraction = 1;
- double result = 0;
- while(index > 0) {
- fraction /= base;
- result += fraction * (index % base);
- index = floor(index / base);
- }
- return result;
- }
- double euclidianDistance(double x1, double x2, double y1, double y2){
- return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
- }
- bool happy(std::vector<double> x_list, std::vector<double> y_list, double x_point, double y_point, double min_distance){
- for(unsigned i = 0; i < x_list.size(); i++) {
- double dist = euclidianDistance(x_list[i], x_point, y_list[i], y_point);
- if(dist < min_distance){
- return false;
- }
- }
- return true;
- }
- int main() {
- double min_distance = 5.0;
- int num_uavs = 4;
- int x_base = 2;
- int y_base = 3;
- int counter = 0;
- double field_size = min_distance * num_uavs;
- int x_index = 1;
- int y_index = 1;
- std::vector<double> x;
- std::vector<double> y;
- while(x.size() < num_uavs){
- if(x.size() == 0){
- x.push_back(0.0);
- y.push_back(0.0);
- } else {
- double new_x = field_size * halton(x_index, x_base);
- double new_y = field_size * halton(y_index, y_base);
- if(happy(x, y, new_x, new_y, min_distance)) {
- x.push_back(new_x);
- y.push_back(new_y);
- }
- }
- x_index += 1;
- y_index += 1;
- }
- for(unsigned i = 0; i < x.size(); i++) {
- std::cout << "uav" << i+1 << " - x: " << x[i] << " / y: " << y[i] << "\n";
- }
- for(unsigned i = 0; i < x.size()-1; i++) {
- for(unsigned j = i+1; j < x.size(); j++) {
- std::cout << "distance between uav" << i+1 << " and the uav" << j+1 << " is " << euclidianDistance(x[i], x[j], y[i], y[j]) << " meters \n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment