Code covered by the BSD License  

Highlights from
SAE Paper Demo: Model-Based Design of Hybrid Electric Vehicles

image thumbnail

SAE Paper Demo: Model-Based Design of Hybrid Electric Vehicles

by

 

07 Nov 2010 (Updated )

Demo files to accompany the paper by the same name

generateArrow(XData,YData,width,height,color)
function [h] = generateArrow(XData,YData,width,height,color)

L = length(XData);
j=1;

%Generating the Squares
for(i=1:1:L-1)
    if((XData(i)~=XData(i+1)))
        if(XData(i)<XData(i+1))
            X = XData(i);Y = YData(i);
            if((i<L-1) && (YData(i+1)~=YData(i+2)))
                dx = width+height/2;
            else
                dx = width;
            end
            while(X+dx <= XData(i+1))
                h.squareHs(j) =  rectangle('Position', [X+1 Y+1-height/2 width height],'EdgeColor','none','FaceColor',color,'Visible','off','erasemode','xor');
                X = X+2*width;
                j=j+1;
            end
        else
            X = XData(i);Y = YData(i);
            if((i<L-1) && (YData(i+1)~=YData(i+2)))
                dx = width+height/2;
            else
                dx = 0;
            end
            while(X-dx >= XData(i+1))
                h.squareHs(j) =  rectangle('Position', [X+1-width Y+1-height/2 width height],'EdgeColor','none','FaceColor',color,'Visible','off','erasemode','xor');
                X = X-2*width;
                j=j+1;
            end
        end
    end
    
    if((YData(i)~=YData(i+1)))
        if(YData(i)<YData(i+1))
            X = XData(i);Y = YData(i);
            if((i<L-1) && (XData(i+1) ~= XData(i+2)))
                dy = width+height/2;
            else
                dy = 0;
            end
            while(Y+dy <= YData(i+1))
                h.squareHs(j) =  rectangle('Position', [X+1-height/2 Y+1 height width],'EdgeColor','none','FaceColor',color,'Visible','off','erasemode','xor');
                Y = Y+2*width;
                j=j+1;
            end
        else
            X = XData(i);Y = YData(i);
            if((i<L-1) && (XData(i+1) ~= XData(i+2)))
                dy = width+height/2;
            else
                dy = 0;
            end
            while(Y-dy >= YData(i+1))
                h.squareHs(j) =  rectangle('Position', [X+1-height/2 Y+1-width height width],'EdgeColor','none','FaceColor',color,'Visible','off','erasemode','xor');
                Y = Y-2*width;
                j=j+1;
            end
        end
    end
    
    if((XData(i)~=XData(i+1)) && (YData(i)~=YData(i+1)))
        disp('Only 90 degree bends are allowed in arrows');
    end
end

%Generating the Arrows vertices
 width = width + 4;
 height = height + 4;
j=1;k=1;
for(i=1:1:L)
    if(i==1)
        %Going Right
        if((XData(i+1)>XData(i)) && (YData(i+1)==YData(i)))
            A(j,1:2) = [XData(i)-width YData(i)];
            A(j+1,1:2) = [XData(i) YData(i)-height];
            A(j+2,1:2) = [XData(i) YData(i)-height/2];
            B(k,1:2) = [XData(i) YData(i)+height];
            B(k+1,1:2) = [XData(i) YData(i)+height/2];
            j=j+3;k=k+2;
            CONN_START_APPEND = [XData(i)-width YData(i)-height/2;XData(i)-width YData(i)+height/2];
        end

        %Going Left
        if((XData(i+1)<XData(i)) && (YData(i+1)==YData(i)))
            A(j,1:2) = [XData(i)+width YData(i)];
            A(j+1,1:2) = [XData(i) YData(i)+height];
            A(j+2,1:2) = [XData(i) YData(i)+height/2];
            B(k,1:2) = [XData(i) YData(i)-height];
            B(k+1,1:2) = [XData(i) YData(i)-height/2];
            j=j+3;k=k+2;
            CONN_START_APPEND = [XData(i)+width YData(i)+height/2;XData(i)+width YData(i)-height/2];
        end

        %Going Up
        if((XData(i+1)==XData(i)) && (YData(i+1)<=YData(i)))
            A(j,1:2) = [XData(i) YData(i)+width];
            A(j+1,1:2) = [XData(i)-height YData(i)];
            A(j+2,1:2) = [XData(i)-height/2 YData(i)];
            B(k,1:2) = [XData(i)+height YData(i)];
            B(k+1,1:2) = [XData(i)+height/2 YData(i)];
            j=j+3;k=k+2;
            CONN_START_APPEND = [XData(i)-height/2 YData(i)+width;XData(i)+height/2 YData(i)+width];
        end

        %Going Down
        if((XData(i+1)==XData(i)) && (YData(i+1)>=YData(i)))
            A(j,1:2) = [XData(i) YData(i)-width];
            A(j+1,1:2) = [XData(i)+height YData(i)];
            A(j+2,1:2) = [XData(i)+height/2 YData(i)];
            B(k,1:2) = [XData(i)-height YData(i)];
            B(k+1,1:2) = [XData(i)-height/2 YData(i)];
            j=j+3;k=k+2;
            CONN_START_APPEND = [XData(i)+height/2 YData(i)-width;XData(i)-height/2 YData(i)-width];
        end
    else
        if(i==L)
            %Going Right
            if((XData(i)>XData(i-1)) && (YData(i)==YData(i-1)))
                A(j,1:2) = [XData(i) YData(i)-height/2];
                A(j+1,1:2) = [XData(i) YData(i)-height];
                A(j+2,1:2) = [XData(i)+width YData(i)];
                B(k,1:2) = [XData(i) YData(i)+height/2];
                B(k+1,1:2) = [XData(i) YData(i)+height];
                j=j+3;k=k+2;
                CONN_END_APPEND = [XData(i)+width YData(i)-height/2;XData(i)+width YData(i)+height/2];
            end
            
            %Going Left
            if((XData(i)<XData(i-1)) && (YData(i)==YData(i-1)))
                A(j,1:2) = [XData(i) YData(i)+height/2];
                A(j+1,1:2) = [XData(i) YData(i)+height];
                A(j+2,1:2) = [XData(i)-width YData(i)];
                B(k,1:2) = [XData(i) YData(i)-height/2];
                B(k+1,1:2) = [XData(i) YData(i)-height];
                j=j+3;k=k+2;
                CONN_END_APPEND = [XData(i)-width YData(i)+height/2;XData(i)-width YData(i)-height/2];
            end
            
            %Going Up
            if((XData(i)==XData(i-1)) && (YData(i)<=YData(i-1)))
                A(j,1:2) = [XData(i)-height/2 YData(i)];
                A(j+1,1:2) = [XData(i)-height YData(i)];
                A(j+2,1:2) = [XData(i) YData(i)-width];
                B(k,1:2) = [XData(i)+height/2 YData(i)];
                B(k+1,1:2) = [XData(i)+height YData(i)];
                j=j+3;k=k+2;
                CONN_END_APPEND = [XData(i)-height/2 YData(i)-width;XData(i)+height/2 YData(i)-width];
            end
            
            %Going Down
            if((XData(i)==XData(i-1)) && (YData(i)>=YData(i-1)))
                A(j,1:2) = [XData(i)+height/2 YData(i)];
                A(j+1,1:2) = [XData(i)+height YData(i)];
                A(j+2,1:2) = [XData(i) YData(i)+width];
                B(k,1:2) = [XData(i)-height/2 YData(i)];
                B(k+1,1:2) = [XData(i)-height YData(i)];
                j=j+3;k=k+2;
                CONN_END_APPEND = [XData(i)+height/2 YData(i)+width;XData(i)-height/2 YData(i)+width];
            end
            
        else
            %Going right
            if(XData(i)>XData(i-1))
                %Turning Down
                if((YData(i)==YData(i-1)) && (YData(i)<YData(i+1)))
                    A(j,1:2) = [XData(i)+height/2 YData(i)-height/2];
                    B(k,1:2) = [XData(i)-height/2 YData(i)+height/2];
                    j=j+1;k=k+1;
                end
                %Turning Down
                if((YData(i)==YData(i-1)) && (YData(i)>YData(i+1)))
                    A(j,1:2) = [XData(i)-height/2 YData(i)-height/2];
                    B(k,1:2) = [XData(i)+height/2 YData(i)+height/2];
                    j=j+1;k=k+1;
                end
            end
            
            %Going Left
            if(XData(i)<XData(i-1))
                %Turning Down
                if((YData(i)==YData(i-1)) && (YData(i)<YData(i+1)))
                    B(k,1:2) = [XData(i)-height/2 YData(i)-height/2];
                    A(j,1:2) = [XData(i)+height/2 YData(i)+height/2];
                    j=j+1;k=k+1;
                end
                %Turning Up
                if((YData(i)==YData(i-1)) && (YData(i)>YData(i+1)))
                    B(k,1:2) = [XData(i)+height/2 YData(i)-height/2];
                    A(j,1:2) = [XData(i)-height/2 YData(i)+height/2];
                    j=j+1;k=k+1;
                end
            end
            
            %Going Up
            if(YData(i)<YData(i-1))
                %Turning Right
                if((XData(i)==XData(i-1)) && (XData(i)<XData(i+1)))
                    B(k,1:2) = [XData(i)-height/2 YData(i)-height/2];
                    A(j,1:2) = [XData(i)+height/2 YData(i)+height/2];
                    j=j+1;k=k+1;
                end
                %Turning Left
                if((XData(i)==XData(i-1)) && (XData(i)>XData(i+1)))
                    B(k,1:2) = [XData(i)-height/2 YData(i)+height/2];
                    A(j,1:2) = [XData(i)+height/2 YData(i)-height/2];
                    j=j+1;k=k+1;
                end
            end
            
            %Going Down
            if(YData(i)>YData(i-1))
                %Turning Right
                if((XData(i)==XData(i-1)) && (XData(i)<XData(i+1)))
                    B(k,1:2) = [XData(i)-height/2 YData(i)+height/2];
                    A(j,1:2) = [XData(i)+height/2 YData(i)-height/2];
                    j=j+1;k=k+1;
                end
                %Turning Left
                if((XData(i)==XData(i-1)) && (XData(i)>XData(i+1)))
                    B(k,1:2) = [XData(i)+height/2 YData(i)+height/2];
                    A(j,1:2) = [XData(i)-height/2 YData(i)-height/2];
                    j=j+1;k=k+1;
                end
            end
        end
    end

end

B = flipud(B);
h.forArrow = patch([CONN_START_APPEND(1,1);A(3:end,1);B(1:end-1,1);CONN_START_APPEND(2,1)],...
                   [CONN_START_APPEND(1,2);A(3:end,2);B(1:end-1,2);CONN_START_APPEND(2,2)],color,'FaceAlpha',0.25,'EdgeColor','none','Visible','off','erasemode','normal');
               
h.revArrow = patch([A(1:end-2,1);CONN_END_APPEND(:,1);B(2:end,1);],...
                   [A(1:end-2,2);CONN_END_APPEND(:,2);B(2:end,2)],color,'FaceAlpha',0.25,'EdgeColor','none','Visible','off','erasemode','normal');

h.connect = patch([CONN_START_APPEND(1,1);A(3:end-2,1);CONN_END_APPEND(:,1);B(2:end-1,1);CONN_START_APPEND(2,1)],...
                  [CONN_START_APPEND(1,2);A(3:end-2,2);CONN_END_APPEND(:,2);B(2:end-1,2);CONN_START_APPEND(2,2)],[0.3 0.3 0.3],...
                  'FaceAlpha',0.25,'EdgeColor','none','Visible','on','erasemode','normal');

Contact us