1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#include <iostream>
#include <vector>
#include <cmath>
#include <chrono>
#include <random>
#include <numeric>
#include <algorithm>
#include <map>
#include <tuple>
constexpr auto SIZE = 20;
struct Point
{
double m_x = 0;
double m_y = 0;
double m_z = 0;
Point(){}
Point (const double& x, const double& y, const double& z)
: m_x(x), m_y(y), m_z(z) {}
};
double pointDistance(const Point& lhs, const Point& rhs)
{
return std::pow(lhs.m_x - rhs.m_x, 2) + std::pow(lhs.m_y - rhs.m_y, 2) + std::pow(lhs.m_z - rhs.m_z,2);
//not math distance definition but monotonic with it and sufficient for here
}
std::ostream& operator << (std::ostream& os, const Point& p)
//for printing point objects
{
os << "(" << p.m_x << "," << p.m_y << "," << p.m_z << ")";
return os;
}
int NchooseR(int n, int r ); // number of combinations of r from n;
std::vector<std::vector<int>> allCombos (int n, int r ); // actual combinations of r from n;
void print_combos(const std::vector<std::vector<int>>& result, const int& x, const int& r);//printing actual combinations;
int main()
{
std::vector<Point> vecPoints{};
std::mt19937 mt(static_cast<unsigned int>(std::time(NULL)));
std::uniform_int_distribution<int> dist(-100, 100);
for (auto i = 0; i < SIZE; ++i )
{
vecPoints.push_back(Point(dist(mt), dist(mt), dist(mt)));
//generating Point objects with random numbers range (-100,100)
}
std::map<double, std::pair<Point, Point>> distancePoints{};
for (const auto& elem : allCombos(SIZE, 2))
{
distancePoints[pointDistance(vecPoints[elem[0]], vecPoints[elem[1]])]
= std::make_pair(vecPoints[elem[0]], vecPoints[elem[1]]);
//using the std::map constructor, LHS is the distance b/w two Point objects ...
//RHS is the pair of the corresponding Point objects
}
for (auto citr = distancePoints.cbegin(); citr != distancePoints.cend(); ++citr)
{
//printing the map
std::cout << citr -> first << " " << citr -> second.first
<< " " << citr -> second.second << '\n';
}
}
int NchooseR(int n, int r )//// number of combinations of r from n;
{
if (r > n) return 0;
if (r * 2 > n) r = n-r;
if (r == 0) return 1;
int result = n;
for( int i = 2; i <= r; ++i ) {
result *= (n-i+1);
result /= i;
}
return result;
}
std::vector<std::vector<int>> allCombos (int n, int r )
{
int x = NchooseR(n, r);
std::vector<std::vector<int>> result(x,std::vector<int>(r));
//2D vector, # inner vectors = number of combinations
//size of inner vector = 2 (in this case)
int j {}, k {};
std::vector<bool> v(n);
std::fill(v.begin() + n - r, v.end(), true);
do
{
for (int i = 0; i < n; ++i)
{
if (v[i])
{
result[j][k++] = i + 1;
}
}
k = 0, ++j;
} while (std::next_permutation(v.begin(), v.end()));
//some useful links:
//http://stackoverflow.com/questions/11483060/stdnext-permutation-implementation-explanation
//http://stackoverflow.com/questions/12991758/creating-all-possible-k-combinations-of-n-items-in-c
http://en.cppreference.com/w/cpp/algorithm/next_permutation
return result;
}
void print_combos(const std::vector<std::vector<int>>& result, const int& x, const int& r)
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < r; j++)
{
std::cout << result[i][j] << " ";
}
std::cout << '\n';
}
}
|