1
var minAreaFreeRect = function (points) {2
const diagonalMap = new Map();5
for (let a = 0; a < points.length - 1; a++) {6
const [x1, y1] = points[a];7
for (let b = a + 1; b < points.length; b++) {8
const [x2, y2] = points[b];9
const diagonal = calculateeDiagonal(x1, y1, x2, y2);10
const centerX = (x1 + x2) / 2;11
const centerY = (y1 + y2) / 2;12
const key = `${diagonal}_${centerX}_${centerY}`;13
const vectors = diagonalMap.get(key) ?? [];15
vectors.push({ x1, x2, y1, y2 });16
diagonalMap.set(key, vectors);20
for (const [key, vector] of diagonalMap) {21
if (vector.length < 2) continue;22
for (let a = 0; a < vector.length - 1; a++) {23
const vectorA = vector[a];24
for (let b = a + 1; b < vector.length; b++) {25
const vectorB = vector[b];27
calculateeDiagonal(vectorA.x1, vectorA.y1, vectorB.x1, vectorB.y1)30
calculateeDiagonal(vectorA.x1, vectorA.y1, vectorB.x2, vectorB.y2)33
result = Math.min(result, w * h);37
return result === Infinity ? 0 : result;39
function calculateeDiagonal(x1, y1, x2, y2) {40
return Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2);