<p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= points.length &lt;= 300</code></li>
	<li><code>points[i].length == 2</code></li>
	<li><code>-10<sup>4</sup> &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
	<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>

struct Vector {
    int x;
    int y;

    Vector operator - (const Vector& b) const {
        return Vector {
            x - b.x,
            y - b.y,
        };
    }

    bool operator != (const Vector& b) const {
        return x != b.x || y != b.y;
    }
};

bool lex_less_than(Vector a, Vector b) {
    if(a.x != b.x) {
        return a.x < b.x;
    } else {
        return a.y < b.y;
    }
}

int64_t cross(Vector a, Vector b) {
    return static_cast<int64_t>(a.x) * static_cast<int64_t>(b.y) -
        static_cast<int64_t>(a.y) * static_cast<int64_t>(b.x);
}

struct Line {
    Vector point;
    Vector dir;
};

bool cmp(Line a, Line b) {
    int64_t c = cross(a.dir, b.dir);
    if(c != 0) {
        return c > 0;
    }

    c = cross(a.dir, b.point - a.point);
    if(c != 0) {
        return c > 0;
    }

    return lex_less_than(a.point, b.point);
}

class Solution {
public:
    int maxPoints(vector<vector<int>>& points) {
        if(points.size() <= 2) {
            return static_cast<int>(points.size());
        }

        std::vector<Line> lines;
        for(int i = 0; i < points.size(); i++) {
            Vector a{points[i][0], points[i][1]};
            for(int j = i + 1; j < points.size(); j++) {
                Vector b{points[j][0], points[j][1]};
                if(lex_less_than(a, b)) {
                    lines.push_back(Line{a, b - a});
                } else {
                    lines.push_back(Line{b, a - b});
                }
            }
        }

        std::sort(lines.begin(), lines.end(), cmp);

        for(auto line : lines) {
            std::cout << "[" << line.point.x << ", " << line.point.y << "], [" <<
                line.dir.x << ", " << line.dir.y << "]" << std::endl;
        }

        int result = 0;
        
        Line block_first_line = lines[0];
        int block_result = 2;

        for(int i = 1; i < lines.size(); i++) {
            if(block_first_line.point != lines[i].point ||
                cross(block_first_line.dir, lines[i].dir) != 0
            ) {
                result = std::max(result, block_result);
                block_result = 2;
                block_first_line = lines[i];
            } else {
                block_result++;
            }
        }

        result = std::max(result, block_result);

        return result;
    }
};
15283