<p>You are given an array of points in the <strong>X-Y</strong> plane <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p>

<p>Return <em>the minimum area of any rectangle formed from these points, with sides <strong>not necessarily parallel</strong> to the X and Y axes</em>. If there is not any such rectangle, return <code>0</code>.</p>

<p>Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/21/1a.png" style="width: 398px; height: 400px;" />
<pre>
<strong>Input:</strong> points = [[1,2],[2,1],[1,0],[0,1]]
<strong>Output:</strong> 2.00000
<strong>Explanation:</strong> The minimum area rectangle occurs at [1,2],[2,1],[1,0],[0,1], with an area of 2.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/22/2.png" style="width: 400px; height: 251px;" />
<pre>
<strong>Input:</strong> points = [[0,1],[2,1],[1,1],[1,0],[2,0]]
<strong>Output:</strong> 1.00000
<strong>Explanation:</strong> The minimum area rectangle occurs at [1,0],[1,1],[2,1],[2,0], with an area of 1.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/22/3.png" style="width: 383px; height: 400px;" />
<pre>
<strong>Input:</strong> points = [[0,3],[1,2],[3,1],[1,3],[2,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no possible rectangle to form from these points.
</pre>

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

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

class Solution {
public:
    pair<int, int> getFourth(pair<int, int> p1, pair<int, int> p2, pair<int, int> p3) {
        int dx = p3.first - p1.first, dy = p3.second - p1.second;
        pair<int, int> p4 = {p2.first - dx, p2.second - dy};
        return p4;
    }

    int getSign(int num, int den) {
        if(num > 0 && den < 0) return -1;
        else if(num < 0 && den > 0) return -1;
        return 1; 
    }

    bool check90(pair<int, int> &p1, pair<int, int> &p2, pair<int, int> &p3) {
        double m1, m2;

        int num1 = (p3.second - p1.second), num2 = (p3.second - p2.second);
        int den1 = (p3.first - p1.first), den2 = (p3.first - p2.first);

        if(num1 == den2 && num1 == 0) return true;
        if(num2 == den1 && num2 == 0) return true;

        int gcd1 = __gcd(abs(num1), abs(den1));
        int gcd2 = __gcd(abs(num2), abs(den2));

        if(abs(num1)/gcd1 == abs(den2)/gcd2 && abs(num2)/gcd2 == abs(den1)/gcd1) {
            if( getSign(num1, den1) !=  getSign(num2, den2)) return true;
        } 

        return false;
    }

    double getDist(pair<int, int> &p1, pair<int, int> &p2) {
        return sqrt(pow(p1.first - p2.first, 2) + pow(p1.second - p2.second, 2));
    }

    double getArea(pair<int, int> &p1, pair<int, int> &p3, pair<int, int> &p4) {
        return getDist(p1, p3) * getDist(p1, p4);
    }

    double minAreaFreeRect(vector<vector<int>>& points) {
        set<pair<int, int>> st;
        int n = points.size();
        for(auto i: points) {
            st.insert({i[0], i[1]});
        }

        double ans = 1e9;

        for(int i = 0; i < n; i++) {
            pair<int, int> p1 = {points[i][0], points[i][1]};
            for(int j = 0; j < n; j++) {
                if(j == i) continue;
                pair<int, int> p2 = {points[j][0], points[j][1]};
                for(int k = 0; k < n; k++) {
                    if(k == i || k == j) continue;
                    pair<int, int> p3 = {points[k][0], points[k][1]};
                    if(!check90(p1, p2, p3)) continue;
                    pair<int, int> p4 = getFourth(p1, p2, p3);
                    if(st.count(p4)) {
                        double area = getArea(p1, p3, p4);
                        if(area != 0) {
                            ans = min(ans, area);
                        }
                    }
                }
            }
        }
        if(ans == 1e9) return 0;

        return ans;
    }
};
12611