function [stohkraj_x, vyska,stsirka, stdelka] = Matice_desky_fcn(stoh_x,stoh_y,poc,typ)
poloha_x = stoh_x;
poloha_y = stoh_y;
pocetstohu = numel(poloha_x);
%typ...typ desky
natoceniDesek(1:pocetstohu,1) = 0;

vyska = zeros(pocetstohu,1); %referencni bod vysky stohu  

for j = 1: pocetstohu
    if poc(j) == 0
        stsirka(j,1) = 0;
        stdelka(j,1) = 0;
    else
        if typ(j) == 1
            for k = 1 : poc(j) %počet desek ve stohu
                sirka = 2; % m
                delka = 2.75; % m
                tloustka = 0.02; % m
                barva = [0 0.4470 0.7410]; %modrá

            % základní poloha bodů
                [mxmy, pxmy, pxpy, mxpy] = zakladnibody(poloha_x(j), poloha_y(j), delka, sirka,vyska(j));

            % matice natočení
                [T_rot] = natoceni(natoceniDesek(j), tloustka,k);

            % po natočení a translaci
                [bod11, bod21, bod22, bod12] = ponatoceni(T_rot, mxmy,pxmy,pxpy,mxpy);
         
            % zobrazení
                zobrazeni(bod11,bod21,bod22,bod12, barva)

                k = k+1;
            end
           vyska(j) = vyska(j) + (k-1)*tloustka;


        elseif typ(j) == 2
            for k = 1:poc(j) %počet desek ve stohu
                sirka = 2; % m
                delka = 2.75; % m
                tloustka = 0.02; % m
                barva = [0.4500 0.3250 0.0980]; %oranžová
                       
                [mxmy, pxmy, pxpy, mxpy] = zakladnibody(poloha_x(j), poloha_y(j), delka, sirka,vyska(j));
                [T_rot] = natoceni(natoceniDesek(j), tloustka,k);
                [bod11, bod21, bod22, bod12] = ponatoceni(T_rot, mxmy,pxmy,pxpy,mxpy);
                zobrazeni(bod11,bod21,bod22,bod12, barva)

                k = k+1;
            end
               vyska(j) = vyska(j) + (k-1)*tloustka;
                
        elseif typ(j) == 3
            for k = 1:poc(j) %počet desek ve stohu
                    sirka = 2; % m
                    delka = 2.75; % m
                    tloustka = 0.02; % m
                    barva = [0.9290 0.6940 0.1250]; %žlutá
                       
                [mxmy, pxmy, pxpy, mxpy] = zakladnibody(poloha_x(j), poloha_y(j), delka, sirka,vyska(j));
                [T_rot] = natoceni(natoceniDesek(j), tloustka,k);
                [bod11, bod21, bod22, bod12] = ponatoceni(T_rot, mxmy,pxmy,pxpy,mxpy);
                zobrazeni(bod11,bod21,bod22,bod12, barva)

                k = k+1;
            end
           vyska(j) = vyska(j) + (k-1)*tloustka;

        elseif typ(j) == 4
            for k = 1:poc(j) %počet desek ve stohu
                    sirka = 2; % m
                    delka = 2.75; % m
                    tloustka = 0.02; % m
                    barva = [0.4940 0.1840 0.5560]; %fialová
                       
                [mxmy, pxmy, pxpy, mxpy] = zakladnibody(poloha_x(j), poloha_y(j), delka, sirka,vyska(j));
                [T_rot] = natoceni(natoceniDesek(j), tloustka,k);
                [bod11, bod21, bod22, bod12] = ponatoceni(T_rot, mxmy,pxmy,pxpy,mxpy);
                zobrazeni(bod11,bod21,bod22,bod12, barva)

                k = k+1;
            end
           vyska(j) = vyska(j) + (k-1)*tloustka;

        elseif typ(j) == 5
            for k = 1:poc(j) %počet desek ve stohu
                    sirka = 2; % m
                    delka = 2.75; % m
                    tloustka = 0.02; % m
                    barva = [0.4660 0.6740 0.1880]; %zelená
                       
                [mxmy, pxmy, pxpy, mxpy] = zakladnibody(poloha_x(j), poloha_y(j), delka, sirka,vyska(j));
                [T_rot] = natoceni(natoceniDesek(j), tloustka,k);
                [bod11, bod21, bod22, bod12] = ponatoceni(T_rot, mxmy,pxmy,pxpy,mxpy);
                zobrazeni(bod11,bod21,bod22,bod12, barva)

                k = k+1;
            end
           vyska(j) = vyska(j) + (k-1)*tloustka;

        elseif typ(j) == 6
            for k = 1:poc(j) %počet desek ve stohu
                    sirka = 2; % m
                    delka = 2.75; % m
                    tloustka = 0.02; % m
                    barva = [0.3010 0.7450 0.9330]; %světle modrá
                       
                [mxmy, pxmy, pxpy, mxpy] = zakladnibody(poloha_x(j), poloha_y(j), delka, sirka,vyska(j));
                [T_rot] = natoceni(natoceniDesek(j), tloustka,k);
                [bod11, bod21, bod22, bod12] = ponatoceni(T_rot, mxmy,pxmy,pxpy,mxpy);
                zobrazeni(bod11,bod21,bod22,bod12, barva)

                k = k+1;
            end
           vyska(j) = vyska(j) + (k-1)*tloustka;


        elseif typ(j) == 7
            for k = 1:poc(j) %počet desek ve stohu
                    sirka = 2; % m
                    delka = 2.75; % m
                    tloustka = 0.02; % m
                    barva = [0.6350 0.0780 0.1840]; %červená
                       
                [mxmy, pxmy, pxpy, mxpy] = zakladnibody(poloha_x(j), poloha_y(j), delka, sirka,vyska(j));
                [T_rot] = natoceni(natoceniDesek(j), tloustka,k);
                [bod11, bod21, bod22, bod12] = ponatoceni(T_rot, mxmy,pxmy,pxpy,mxpy);
                zobrazeni(bod11,bod21,bod22,bod12, barva)

                k = k+1;
            end
         vyska(j) = vyska(j) + (k-1)*tloustka;

        end
        stsirka(j,1) = sirka;
        stdelka(j,1) = delka;

        stohkraj_x(j,1) = poloha_x(j) - delka/2;
        stohkraj_x(j,2) = poloha_x(j) + delka/2;
        stohkraj_x(j,3) = poloha_x(j) - delka*1;
        stohkraj_x(j,4) = poloha_x(j) + delka*1;

    end
end


view(3)
axis equal


function [mxmy, pxmy, pxpy, mxpy] = zakladnibody(poloha_x, poloha_y, delka, sirka,vyska)

    mxmy = [poloha_x - delka/2; 
           poloha_y - sirka/2; vyska;1];
    pxmy = [poloha_x + delka/2; 
           poloha_y - sirka/2; vyska;1];
    pxpy = [poloha_x + delka/2;
           poloha_y + sirka/2; vyska;1];
    mxpy = [poloha_x - delka/2; 
           poloha_y + sirka/2; vyska;1];
end


function [T_rot] = natoceni(natoceniDesek, tloustka,k)

    T_rot = [cosd(natoceniDesek), - sind(natoceniDesek) 0 0;
             sind(natoceniDesek), cosd(natoceniDesek) 0 0;
                0 0 1 k*tloustka;
                0 0 0 1];
end


function [bod11, bod21, bod22, bod12] = ponatoceni(T_rot, mxmy,pxmy,pxpy,mxpy)
        bod11 = T_rot*mxmy;
        bod21 = T_rot*pxmy;
        bod22 = T_rot*pxpy;
        bod12 = T_rot*mxpy;
end


function [] = zobrazeni(bod11,bod21,bod22,bod12, barva)
        mxmy_pxmy = line([bod11(1), bod21(1)],[bod11(2), bod21(2)],[bod11(3), bod21(3)]);
        set(mxmy_pxmy,'Color',barva);

        pxmy_pxpy = line([bod21(1), bod22(1)],[bod21(2), bod22(2)],[bod21(3), bod22(3)]);
        set(pxmy_pxpy,'Color',barva);
        
        pxpy_mxpy = line([bod22(1), bod12(1)],[bod22(2), bod12(2)],[bod22(3), bod12(3)]);
        set(pxpy_mxpy,'Color',barva);
        
        mxpy_mxmy = line([bod12(1), bod11(1)],[bod12(2), bod11(2)],[bod12(3), bod11(3)]);
        set(mxpy_mxmy,'Color',barva);
end
end