Partager via


Mock Grid Implementation

  
class Grid : IGrid {
    int left;
    int right;
    int bottom;
    int top;

    public int TotalCalls = 0;

    public Grid(int left, int right, int bottom, int top) {
        this.left = left;
        this.right = right;
        this.bottom = bottom;
        this.top = top;
    }

    public int CountBlocksSet(int x1, int x2, int y1, int y2) {
        ++TotalCalls;
        int intersectRight = Math.Min(x2, this.right);
        int intersectLeft = Math.Max(x1, this.left);
        int intersectTop = Math.Min(y2, this.top);
        int intersectBottom = Math.Max(y1, this.bottom);
        
        if (intersectRight < intersectLeft)
            return 0;
        if (intersectTop < intersectBottom)
            return 0;

        return (intersectRight - intersectLeft + 1) * (intersectTop - intersectBottom + 1);
    }
}

Comments