<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<double, double> findCenter(vector<vector<int>>& points, int i, int j) {
        if (points[i][0] > points[j][0]) {
            swap(i, j);
        }
        
        return {(points[j][0] + points[i][0]) / 2.00, (points[j][1] + points[i][1]) / 2.00};
    }

    double minAreaFreeRect(vector<vector<int>>& points) {

        // find all line segments

        // find all the centers
        // find all lengths

        // [[1,2], [2,1], [1,0], [0,1]]

        // [1, 2] -> [2, 1] = 2
        // [1, 2] -> [1, 0] = 4
        // [1, 2] -> [0, 1] = 2
        // [2, 1] -> [1, 0] = 2
        // [2, 1] -> [0, 1] = 4
        // [1, 0] -> [0, 1] = 2

        // 2: [1, 2] -> [2, 1], [1, 2] -> [0, 1], [2, 1] -> [1, 0], [1, 0] -> [0, 1]
        // 4: [1, 2] -> [1, 0], [2, 1] -> [0, 1]

        // find all centers, sort, and see if there are same ones
        // if found then calculate area and break
        cout << fixed << setprecision(10);
        double ans = numeric_limits<double>::infinity();

        int n = points.size();

        //   v-- length
        map<int,    vector<pair< // vector of segments
                        pair<double, double>, // center point
                        pair<int, int>  // indexes to segment ends
                    >> 
            > lens;

        

        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                int length = (points[j][0] - points[i][0]) * (points[j][0] - points[i][0]) +
                     (points[j][1] - points[i][1]) * (points[j][1] - points[i][1]);
                
                
                lens[length].push_back({findCenter(points, i, j), {i, j}});

                sort(lens[length].begin(), lens[length].end(), [](
                    pair<pair<double, double>, pair<int, int>> a,
                    pair<pair<double, double>, pair<int, int>> b
                ) -> int {
                    if (fabs(a.first.first - b.first.first) <= 1e-9)
                        return a.first.second < b.first.second;
                    return a.first.first < b.first.first;
                });
            }
        }

        for (auto i: lens) {
            // cout << "len: " << i.first << endl;
            // for (auto j: i.second) {
            //     cout << "c: " << j.first.first << " " << j.first.second << endl;
            //     cout << "ij: " << j.second.first << " " << j.second.second << endl;
            // }


            for (int k = 0; k < i.second.size(); k++)
            for (int j = k+1; j < i.second.size(); j++) {
                // cout << "center " << endl;
                
                pair<double, double> c1 = i.second[j].first;
                pair<double, double> c2 = i.second[k].first;
                const double eps = 1e-9;
                if (fabs(c1.first-c2.first) <= eps && fabs(c1.second-c2.second) <= eps) {
                    cout << "same centers\n";
                    cout << " " << i.second[j].second.first << " " <<
                        i.second[j].second.second << " " << 
                        i.second[k].second.first << " " << i.second[k].second.second << endl;

                    vector<vector<int>> pts {
                        points[i.second[j].second.first],
                        points[i.second[j].second.second],
                        points[i.second[k].second.first],
                        points[i.second[k].second.second]
                    };
                    // 0 -- 2
                    // 0 -- 3
                    double area = sqrt(
                        (pow(1.0*pts[2][0] - pts[0][0], 2.0) +
                        pow(1.0*pts[2][1] - pts[0][1], 2.0))
                    
                    *
                        (pow(1.0*pts[2][0] - pts[1][0], 2.0) +
                        pow(1.0*pts[2][1] - pts[1][1], 2.0))
                    );

                    cout << "area: " << area << endl;

                    ans = min(ans, area);
                }
            }
        }

        if (ans == numeric_limits<double>::infinity()) return 0.000;
        return ans;
    }
};

// n^2 can be really good, in comparison to n
22248