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
|
#include <iostream>
#include <memory>
#include <set>
#include <cmath>
struct Point
{
double _x;
double _y;
double _z;
Point (double x, double y, double z) : _x(x), _y(y), _z(z){};
};
std::ostream& operator << (std::ostream& os, const Point& p)
{
os << "(" << p._x << ", " << p._y << ", " << p._z << ")";
}
double distance(const Point& lhs, const Point& rhs);
template <typename T, int a, int b, int c>
struct PointDouble
{
double _PD_x;
double _PD_y;
double _PD_z;
PointDouble (double x, double y, double z) : _PD_x(x), _PD_y(y), _PD_z(z){}
template <typename T1, int a1, int b1, int c1>
struct DistanceCompare
{
T _origin = T(a1, b1, c1);
bool operator()(const Point& lhs, const Point& rhs)
{
return distance(lhs, _origin) < distance (rhs, _origin);
}
} ;
std::multiset<Point, DistanceCompare<T, a, b, c>> neighborhood;
};
int main()
{
Point a(.9, 33, 6), b(-1, 2, -3), c(-2, 3, 4), d(2, -3, -4), e(5, -4, -14), f(0.7, 0.2, 0.7);
PointDouble<Point, 1, 33, 6> p(1,33,6);
Point p1(1, 33, 6);
p.neighborhood.insert (a);
p.neighborhood.insert (b);
p.neighborhood.insert (c);
p.neighborhood.insert (d);
p.neighborhood.insert (e);
p.neighborhood.insert (f);
std::cout << "The neighborhood of: " << p1 << " looks like this: \n";
for (auto& elem : p.neighborhood)
{
std::cout << elem << '\n';
}
std::cout << "\n\n";
PointDouble<Point, 0, 0, 0> s(0, 0, 0);
Point s1(0, 0, 0);
s.neighborhood.insert (a);
s.neighborhood.insert (b);
s.neighborhood.insert (c);
s.neighborhood.insert (d);
s.neighborhood.insert (e);
s.neighborhood.insert (f);
std::cout << "The neighborhood of: " << s1 << " looks like this: \n";
for (auto& elem : s.neighborhood)
{
std::cout << elem << '\n';
}
}
double distance(const Point& lhs, const Point& rhs)
{
return std::pow(lhs._x - rhs._x, 2) + std::pow(lhs._y - rhs._y, 2) + std::pow(lhs._z - rhs._z, 2);
}
|