Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <ctime>
- #include <vector>
- #include <algorithm>
- using namespace std;
- class Coordinate
- {
- double x, y;
- public:
- Coordinate(double _x=0, double _y=0) :x(_x), y(_y) {}
- double distance(Coordinate& c) { return sqrt(pow(c.x - x, 2) + pow(c.y - y, 2)); }
- void print() { cout << "(" << x << "," << y << ")"; }
- };
- class CompareCoords
- {
- Coordinate ref;
- public:
- CompareCoords(Coordinate c) :ref(c) {}
- bool operator () (Coordinate& a, Coordinate& b) { return ref.distance(a) < ref.distance(b); }
- };
- int main()
- {
- vector<Coordinate> coords;
- coords.reserve(20);
- for (int i = 0; i < 20; ++i)
- coords.push_back(Coordinate(rand() % 100, rand() % 100));
- Coordinate me(0, 0);
- sort(coords.begin(), coords.end(), CompareCoords(me));
- for (int i = 0; i < coords.size(); ++i)
- coords[i].print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement