Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <iostream>
- #include <vector>
- using namespace std;
- struct Point {
- double x;
- double y;
- };
- struct Vector {
- double x;
- double y;
- double Len() const;
- };
- Point operator+(const Point& p, const Vector& v) {
- return {p.x + v.x, p.y + v.y};
- }
- Vector operator+(const Vector& v1, const Vector& v2) {
- return {v1.x + v2.x, v1.y + v2.y};
- }
- Vector operator-(const Vector& v1, const Vector& v2) {
- return {v1.x - v2.x, v1.y - v2.y};
- }
- Vector operator-(const Point& p1, const Point& p2) {
- return {p1.x - p2.x, p1.y - p2.y};
- }
- double DotProduct(const Vector& v1, const Vector& v2) {
- return v1.x * v2.x + v1.y * v2.y;
- }
- double CrossProduct(const Vector& v1, const Vector& v2) {
- return v1.x * v2.y - v1.y * v2.x;
- }
- double Vector::Len() const {
- return sqrt(x * x + y * y);
- }
- bool operator==(const Point& p1, const Point& p2) {
- return p1.x == p2.x && p1.y == p2.y;
- }
- bool operator!=(const Point& p1, const Point& p2) {
- return !operator==(p1, p2);
- }
- istream& operator>>(istream& in, Vector& v) {
- in >> v.x >> v.y;
- return in;
- }
- istream& operator>>(istream& in, Point& v) {
- in >> v.x >> v.y;
- return in;
- }
- vector<Point> ConvexHull(vector<Point>& p) {
- if (p.empty()) return {};
- auto it = min_element(p.begin(), p.end(), [](const Point& p1, const Point& p2) -> bool {
- return p1.y < p2.y || (p1.y == p2.y && p1.x < p2.x);
- });
- vector<Point> ch;
- ch.push_back(*it);
- for (;;) {
- Point cur = ch.back();
- Point cand = p.front();
- for (int i = 1; i < p.size(); ++i) {
- double cross = CrossProduct(cand - cur, p[i] - cur);
- if (cross < -eps || (cross < eps && (p[i] - cur).Len() > (cand - cur).Len())) cand = p[i];
- }
- if (cand == ch.front()) return ch;
- ch.push_back(cand);
- }
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- int n = 0; cin >> n;
- vector<Point> p(n);
- for (int i = 0; i < n; ++i) cin >> p[i];
- vector<Point> ch = ConvexHull(p);
- for (const auto& point : ch) {
- cout << point.x << " " << point.y << "\n";
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment