im_larg = 100;

length = 0.001;

temp2 = mat1;
i = 1;
rot_mat = mat1;
rot_mat(ptStat6,2) = bob1(i)*mat1(ptStat6,2);
rot_mat(sommetStat6(ptStat6),2) = bob2(i)*mat1(ptStat6,2);
rot_mat(sommetStat6(sommetStat6(ptStat6)),2) = bob3(i)*mat1(ptStat6,2);
rot_mat(sommetStat6(sommetStat6(sommetStat6(ptStat6))),2) = -bob1(i)*mat1(ptStat6,2);
rot_mat(sommetStat6(sommetStat6(sommetStat6(sommetStat6(ptStat6)))),2) = -bob2(i)*mat1(ptStat6,2);
rot_mat(sommetStat6(sommetStat6(sommetStat6(sommetStat6(sommetStat6(ptStat6))))),2) = -bob3(i)*mat1(ptStat6,2);
mat1 = rot_mat;

subplot(1,2,1);
mu = mat1(:,1);
ju = mat1(:,2);
nu = sqrt(mat1(:,3).^2+mat1(:,4).^2);

m_map = mex_map_dis_cell(sommet/moteur_rayon+1, triangle, mu, im_larg, 2, 2);
j_map = mex_map_dis_cell(sommet/moteur_rayon+1, triangle, ju, im_larg, 2, 2);
b_map = mex_map_dis_cell(sommet/moteur_rayon+1, triangle, nu, im_larg, 2, 2);

jp_map = j_map.*(j_map>=0);
jn_map = -j_map.*(j_map<0);

im_m = zeros(2*im_larg, 2*im_larg, 3);

im_m(:,:,1) = m_map+jn_map+b_map;    
im_m(:,:,2) = m_map+jp_map+jn_map;    
im_m(:,:,3) = m_map+b_map+jp_map;    
im_m(:,:,1) = 1- im_m(:,:,1)./max(max(im_m,[],3),1);
im_m(:,:,2) = 1- im_m(:,:,2)./max(max(im_m,[],3),1);
im_m(:,:,3) = 1- im_m(:,:,3)./max(max(im_m,[],3),1);

coordX = moteur_rayon*(1/im_larg/2-1:1/im_larg:1-1/im_larg/2);
coordY = moteur_rayon*(1-1/im_larg/2:-1/im_larg:1/im_larg/2-1);

image(coordX,coordY,im_m);
title(strcat('Iteration ', num2str(iter,4), ' | Cmoy ', num2str(mean(tau(ang_obj)),5), ' | Cdev ', num2str(Wcouple*mean(tau(ang_obj))+Objectif),5));

hold on

for i=1:4:nbTriangle
    j = triangle(i,5);
    if(matmax(j,3))
        plot([sommet(j,1)-length*mat1(j,3) sommet(j,1)+length*mat1(j,3)], -[sommet(j,2)-length*mat1(j,4) sommet(j,2)+length*mat1(j,4)], 'k-');
        plot(sommet(j,1)+length*mat1(j,3), -sommet(j,2)-length*mat1(j,4), 'kx');  
    end
end
axis([-0.05 0.05 -0.05 0.05]);
axis equal

subplot(1,2,2);
mu = mat1(:,1);
ju = mat1(:,2);
nu = sqrt(mat1(:,3).^2+mat1(:,4).^2);

m_map = mex_map_dis_cell(sommet/moteur_rayon+1, triangle, mu, im_larg, 2, 2);
j_map = mex_map_dis_cell(sommet/moteur_rayon+1, triangle, ju, im_larg, 2, 2);
b_map = mex_map_dis_cell(sommet/moteur_rayon+1, triangle, nu, im_larg, 2, 2);

jp_map = j_map.*(j_map>=0);
jn_map = -j_map.*(j_map<0);


im_m = zeros(2*im_larg, 2*im_larg, 3);

im_m(:,:,1) = m_map+jn_map+b_map;    
im_m(:,:,2) = m_map+jp_map+jn_map;    
im_m(:,:,3) = m_map+b_map+jp_map;    
im_m(:,:,1) = 1- im_m(:,:,1)./max(max(im_m,[],3),1);
im_m(:,:,2) = 1- im_m(:,:,2)./max(max(im_m,[],3),1);
im_m(:,:,3) = 1- im_m(:,:,3)./max(max(im_m,[],3),1);


coordX = moteur_rayon*(1/im_larg/2-1:1/im_larg:1-1/im_larg/2);
coordY = moteur_rayon*(1-1/im_larg/2:-1/im_larg:1/im_larg/2-1);

image(coordX,coordY,im_m);
axis equal;
title(strcat('Iteration ', num2str(iter,4), ' | obj ', num2str(Objectif,4),  ' | a ', num2str(a_value,3), ' | b ', num2str(b(1,1),3), ' | c ', num2str(c_value,3)));

% hold on;
% uu = zeros(nbSommet,1);
% uu(ptInt) = u2;
% umap = mex_map_dis_quad(sommet/moteur_rayon+1, triangle, uu, im_larg, 2, 2);
% contour(coordX,coordY,umap,20);


winsize = get(fig1,'Position');
winsize(1:2) = [0 0];
set(gcf,'Color',[1,1,1])
I=getframe(fig1,winsize);
mat1 = temp2;
% imwrite(I.cdata, strcat('plotmap.png'));
