function plotgrid_CR1tri(nodes,elemNodes,CRnodes,elemCRnodes,legend)

% plotgrid_CR1tri(nodes,elemNodes,CRnodes,elemCRnodes,legend)
%            displays a Crouzeix-Raviart triangulation.

ne = size(elemNodes,2);

figure
hold on
for i=1:ne
    ei = elemNodes(:,i);

    xi1 = nodes(:,ei(1));
    xi2 = nodes(:,ei(2));
    xi3 = nodes(:,ei(3));
           
    plot([xi1(1),xi2(1)],[xi1(2),xi2(2)]);
    plot([xi2(1),xi3(1)],[xi2(2),xi3(2)]);
    plot([xi3(1),xi1(1)],[xi3(2),xi1(2)]);

    eCRi = elemCRnodes(:,i);
    
    xCRi1 = 0.5*(nodes(:,CRnodes(1,eCRi(1)))+nodes(:,CRnodes(2,eCRi(1))));
    xCRi2 = 0.5*(nodes(:,CRnodes(1,eCRi(2)))+nodes(:,CRnodes(2,eCRi(2))));
    xCRi3 = 0.5*(nodes(:,CRnodes(1,eCRi(3)))+nodes(:,CRnodes(2,eCRi(3))));

    if (legend==1)
        plot(xCRi1(1),xCRi1(2),'o')
        text(xCRi1(1),xCRi1(2),int2str(eCRi(1)));
        plot(xCRi2(1),xCRi2(2),'o')
        text(xCRi2(1),xCRi2(2),int2str(eCRi(2)));
        plot(xCRi3(1),xCRi3(2),'o')
        text(xCRi3(1),xCRi3(2),int2str(eCRi(3)));
        
        t = text((1/3)*(xi1(1)+xi2(1)+xi3(1)),(1/3)*(xi1(2)+xi2(2)+xi3(2)),int2str(i));
        set(t,'Color',[1 0 0]);
    end
end
hold off
