clc;
clear all;

M = csvread('Farias2.csv');
N_Q=csvread('Sizes.csv');
SizeT=0;
tot=size(N_Q);
for i=1:1:tot(1)
    SizeT=SizeT+N_Q(i);
end
posx=1;
posy=2;
posX=3;
posVel_k=6;
posVel_=7;
posT=8;
posT_C=9;
for i=1:SizeT
    x(i)=M((posx-1)*SizeT+i);
end
for i=1:SizeT
    y(i)=M((posy-1)*SizeT+i);
end
for i=1:SizeT
    Tt(i)=M((posT-1)*SizeT+i);
end
for i=1:SizeT
    T_Ct(i)=M((posT_C-1)*SizeT+i);
end
for i=1:SizeT
   Vel_t(i)=M((posVel_-1)*SizeT+i);
end
for i=1:SizeT
   Vel_kt(i)=M((posVel_k-1)*SizeT+i);
end
Tt=Tt*10^-3;
y=y*10^-3;
x=x*10^-3;
Vel_kt=Vel_kt*10^-3;
ver_teste=M((posT_C-1)*SizeT+1)
cont_teste=0;
p_chute=1;
n_teste=1;
n_tempos=1;
for i=2:SizeT
    cont_teste=cont_teste+1;
    if ver_teste~=M((posT_C-1)*SizeT+i) || p_chute==tot(1)
        N_T(n_tempos)=n_teste;
        n_tempos=n_tempos+1;
        ver_teste=M((posT_C-1)*SizeT+i);
        p_chute=p_chute+1;
        cont_teste=0;
        n_teste=1;
    end
    if p_chute<tot(1) && cont_teste==N_Q(p_chute)
        n_teste=n_teste+1;
        p_chute=p_chute+1;
        cont_teste=0;
    end
end
N_Em=size(N_T);
N_E=N_Em(2);
N_T_R(1)=0;
N(1)=0;
N_Videos=0;
for i=1:N_E
    N_Videos=N_Videos+N_T(i);
    N_T_R(i+1)=N_Videos;
end
for i=1:N_Videos
    N(i+1)=N(i)+N_Q(i);
end
Xt(1)=0;
for j=1:N_Videos
    Xt(N(j)+1)=0;
    for i=N(j)+1:N(j+1)
        Xt(i)=sqrt((x(i)-x(N(j)+1))^2+(y(i)-y(N(j)+1))^2);
    end
end
for i=1:N_E
    T_C(i)=T_Ct(N(N_T_R(i)+1)+1);
end

cont_delay=1;
for j=1:N_Videos
    for i=N(j)+1:N(j+1)
        if(abs(Xt(i)-Xt(N(j)+1))<0.002)
            delay(j)=i-N(j);
            cont_delay=1;
        end
        cont_delay=cont_delay+1;
    end   
end
for i=1:N_Videos
    N_Q(i)=N_Q(i)-delay(i);
end
cont_adptdelay=1;
for j=1:N_Videos
    for i=N(j)+1+delay(j):N(j+1)
        X(cont_adptdelay)=Xt(i);
        T(cont_adptdelay)=Tt(i);
        Vel_(cont_adptdelay)=Vel_t(i);
        Vel_k(cont_adptdelay)=Vel_kt(i);
        cont_adptdelay=1+cont_adptdelay;
    end
end
N_Em=size(N_T);
N_E=N_Em(2)
N_T_R(1)=0;
N(1)=0;
N_Videos=0;
for i=1:N_E
    N_Videos=N_Videos+N_T(i);
    N_T_R(i+1)=N_Videos;
end
for i=1:N_Videos
    N(i+1)=N(i)+N_Q(i);
end
ThetaI=atan((y(20)-y(5))/(x(20)-x(5)));
ThetaF=atan((y(SizeT)-y(1))/(x(SizeT)-x(1)));
ThetaFGraus=ThetaF*180/pi;
ThetaIGraus=ThetaI*180/pi;
if 1
figure(1)
for b=1:6
    subplot(3,2,b);
    title('Gráfico de x(t) para um tempo de capacitor')
    for z=1:N_T(b) 
        for j=1:(N_Q(N_T_R(b)+z)-1) 
        plot([T(N(N_T_R(b)+z)+j) T(N(N_T_R(b)+z)+j+1)],[X(N(N_T_R(b)+z)+j) X(N(N_T_R(b)+z)+j+1)],'-k')
        hold on 
        %M=getframe()
        end
    end
    
    hold off
    b
   
end
end
if 1
    figure(2)
    title('Dados x(t) recebidos do csv')
        for j=1:SizeT-1 
         plot([Tt(j) Tt(j+1)],[Xt(j) Xt(j+1)],'-k')
        hold on
        end
    
end