1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| class Solution { public double minAreaFreeRect(int[][] points) { double minArea = 0; for (int i = 0; i < points.length; i++) { for (int j = i + 1; j < points.length; j++) { for (int k = j + 1; k < points.length; k++) { for (int l = k + 1; l < points.length; l++) { if (isRectangle(points[i][0], points[i][1], points[j][0], points[j][1], points[k][0], points[k][1], points[l][0], points[l][1])) { if (minArea == 0) { minArea = getS(points[i][0], points[i][1], points[j][0], points[j][1], points[k][0], points[k][1], points[l][0], points[l][1]); } else { minArea = Math.min(minArea, getS(points[i][0], points[i][1], points[j][0], points[j][1], points[k][0], points[k][1], points[l][0], points[l][1])); } } } } } } return minArea; }
public boolean isRectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { double cx = (x1 + x2 + x3 + x4) / 4.0; double cy = (y1 + y2 + y3 + y4) / 4.0; double dd1 = (cx - x1) * (cx - x1) + (cy - y1) * (cy - y1); double dd2 = (cx - x2) * (cx - x2) + (cy - y2) * (cy - y2); double dd3 = (cx - x3) * (cx - x3) + (cy - y3) * (cy - y3); double dd4 = (cx - x4) * (cx - x4) + (cy - y4) * (cy - y4); return Math.abs(dd1 - dd2) < 1E-6 && Math.abs(dd1 - dd3) < 1E-6 && Math.abs(dd1 - dd4) < 1E-6; }
public double getS(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { return Math.abs(x1 * y2 + x2 * y3 + x3 * y1 - x1 * y3 - x2 * y1 - x3 * y2) / 2.0 + Math.abs(x1 * y3 + x3 * y4 + x4 * y1 - x1 * y4 - x3 * y1 - x4 * y3) / 2.0; }
}
|