function [T, g, delaunayparams] = build_discretized_MDP_for_brick(mode, xmin, xmax, d, us, dt, discount);

delaunayparams=[];
dx = (xmax-xmin)/d;
us = [-1:.2:1];
S = prod( (xmax-xmin)./dx + 1);

%% discretize the dynamics and cost function of brick
if(strcmp(mode,'delaunay'))
    states = [];
    for x1 = xmin(1):dx(1):xmax(1)
        for x2 = xmin(2):dx(2):xmax(2)
            x = [x1; x2];
            states = [states x];
        end
    end
    tri = delaunayn(states');
    %to find triangle which contains x and the convex weights:
    %[tri_row,prob] = tsearchn(states',tri,x);
    for u_idx = 1:length(us)
        u = us(u_idx);

        T{u_idx} = sparse(S,S);
        all_xnext = zeros(length(dx),S);
        s=1;
        for x1 = xmin(1):dx(1):xmax(1)
            for x2 = xmin(2):dx(2):xmax(2)
                x = [x1; x2];
                xnext = sim_brick(x, u, dt, xmin, xmax);
                all_xnext(:,s) = xnext;
                cost(s, u_idx) = brick_cost(x, u);
                s=s+1;
            end
        end
        [tri_row, prob] = tsearchn(states', tri, all_xnext');
        for s=1:S
            this_tri_row = tri(tri_row(s),:);
            for k=1:size(this_tri_row,2)
                T{u_idx}(s, this_tri_row(k)) = prob(s,k);
            end            
        end
    end
    delaunayparams.tri = tri;
    delaunayparams.states = states;
else
    for u_idx = 1:length(us)
        u = us(u_idx);
        T{u_idx} = sparse(S,S);
        states_next = [];
        for x1 = xmin(1):dx(1):xmax(1)
            for x2 = xmin(2):dx(2):xmax(2)
                x = [x1; x2];

                curr_idx = continuous_to_discrete(x, xmin, xmax, dx);

                cost(curr_idx, u_idx) = brick_cost(x, u);

                xnext = sim_brick(x, u, dt, xmin, xmax);

                if(strcmp(mode, 'nn'))  % nearest neighbor:
                    T{u_idx}(curr_idx, continuous_to_discrete(xnext, xmin, xmax,dx))=1;
                end
            end
        end
    end
end

g = cost;