Rectangle Area(Easy)

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Java代码

public int computeAreaJoin(int A, int B, int C, int D, int E, int F, int G, int H) {
    int hTop = Math.min(D, H);
    int hBot = Math.max(B, F);
    int wTop = Math.min(C, G);
    int wBot = Math.max(A, E);
    if (hTop < hBot || wTop < wBot) {
        return 0;
    } else {
        return (hTop - hBot) * (wTop - wBot);
    }
}
// A U B = A + B - A * B
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    return (C-A)*(D-B) + (G-E)*(H-F) - computeAreaJoin(A,B,C,D,E,F,G,H);
}

results matching ""

    No results matching ""