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
|
#include <iostream>
#include <cmath>
#include <vector>
#include <tuple>
#include <map>
using std::cin;
using std::cout;
class Point
{
public:
Point() : _x(0), _y(0){}
Point(const double& x, const double& y) : _x(x), _y(y) {}
double getX() const {return _x;}
double getY() const {return _y;}
void setX(const double& x) { _x = x;}
void setY(const double& y) {_y = y;}
friend std::ostream& operator << (std::ostream& os, const Point& p);
private:
double _x;
double _y;
};
double distance (const Point& lhs, const Point& rhs);
int main()
{
std::vector<Point> Points;
std::map<double, std::pair<Point, Point>> myMap;
double x;
double y;
do
{
cout << "Enter (x,y) Coordinates ((-1, -1) to stop): ";
cin >> x >> y;
cout << x << " " << y << '\n';
if ((x == -1) || (y == -1))
{
cout << "-1 -1 Entered. \nCalculating Distance and closest points..." << '\n';
break;
}
else
{
Point temp;
temp.setX(x);
temp.setY(y);
Points.push_back(temp);
}
} while (x != -1 || y != -1);
for (size_t i = 0; i < Points.size(); ++i)
{
for (size_t j = i + 1; j < Points.size(); ++j)
{
myMap[distance(Points[i], Points[j])] = std::make_pair(Points[i], Points[j]);
}
}
auto itr = myMap.begin();
std::cout << itr -> first << " " << itr ->second.first << " " << itr -> second.second << '\n';
}
double distance (const Point& lhs, const Point& rhs)
{
return pow(std::pow(lhs.getX() - rhs.getX(), 2) + std::pow(lhs.getY() - rhs.getY(), 2), 0.5);
}
std::ostream& operator << (std::ostream& os, const Point& p)
{
os << "(" << p._x << ", " << p._y << ")" << " ";
return os;
}
|