Max Points On A Line Algorithm

The Max Points on a Line algorithm is a computational geometry problem that aims to find the maximum number of points from a given set of points that lie on a single straight line. This problem is commonly encountered in applications such as computer vision, image processing, and pattern recognition. The solution helps in identifying collinear points, which can be useful in various geometric computations and optimizations. To solve this problem, one popular approach is to iterate through each point in the given set, and then for each point, calculate the slope between it and every other point in the set. By using a hash table to store the number of points with the same slope, it is possible to determine the maximum number of points that lie on the same line. This method has a time complexity of O(n^2) where n is the number of points in the set. However, it is important to handle edge cases, such as duplicate points and vertical lines (where the slope is infinite), to ensure the correctness of the algorithm.
/**
 * Definition for a point.
 * struct Point {
 *     int x;
 *     int y;
 *     Point() : x(0), y(0) {}
 *     Point(int a, int b) : x(a), y(b) {}
 * };
 */

bool operator<(const Point& x, const Point& y) {
    if (x.x == y.x) {
        return x.y < y.y;
    }
    return x.x < y.x;
}

bool operator==(const Point& x, const Point& y) {
    if (x.x == y.x && x.y == y.y) {
        return true;
    }
    return false;
}

class Solution {
public:
    int maxPoints(vector<Point> &points) {
        int n = points.size();
        if (n <= 2) {
            return n;
        }
        int result = 2;
        sort(points.begin(), points.end());
        for (int i = 0; i < n; i++) {
            vector<pair<int, int>> slopes;
            int count = 0;
            for (int j = i + 1; j < n; j++) {
                int x = points[i].x - points[j].x;
                int y = points[i].y - points[j].y;
                int z = gcd(x, y);
                if (z != 0) {
                    x /= z, y /= z;
                }
                if (x == 0 && y == 0) {
                    count++;
                } else {
                    slopes.push_back(make_pair(x, y));
                }
            }
            if (slopes.empty()) {
                result = max(result, count + 1);
                continue;
            }
            sort(slopes.begin(), slopes.end());
            int curr = 2 + count;
            result = max(result, curr);
            for (int j = 1; j < slopes.size(); j++) {
                if (slopes[j] == slopes[j-1]) {
                    curr++;
                } else {
                    curr = 2 + count;
                }
                result = max(result, curr);
            }
        }
        return result;
    }
    
    int gcd(int a, int b) {
        while (b != 0) {
            int t = b;
            b = a % b;
            a = t;
        }
        return a;
    }
};

LANGUAGE:

DARK MODE: