for i=1:length(a1x),
        if i==1,
                v1=[a1x(i)-a1x(length(a1x)-1),a1y(i)-a1y(length(a1x)-1)];
        else
                v1=[a1x(i)-a1x(i-1),a1y(i)-a1y(i-1)];
        end
        if i==length(a1x),
                v2=[a1x(2)-a1x(i),a1y(2)-a1y(i)];
        else
                v2=[a1x(i+1)-a1x(i),a1y(i+1)-a1y(i)];
        end
        d1=sqrt(v1(1)^2+v1(2)^2); d2=sqrt(v2(1)^2+v2(2)^2);v=v1*d2+v2*d1;
        v=v/sqrt(v(1)^2+v(2)^2); n1(i,:)=[v(2),-v(1)];
end
for i=1:length(a2x),
        if i==1,
                v1=[a2x(i)-a2x(length(a2x)),a2y(i)-a2y(length(a2x))];
        else
                v1=[a2x(i)-a2x(i-1),a2y(i)-a2y(i-1)];
        end
        if i==length(a2x),
                v2=[a2x(3)-a2x(i),a2y(3)-a2y(i)];
        else
                v2=[a2x(i+1)-a2x(i),a2y(i+1)-a2y(i)];
        end
        d1=sqrt(v1(1)^2+v1(2)^2); d2=sqrt(v2(1)^2+v2(2)^2);v=v1*d2+v2*d1;
        v=v/sqrt(v(1)^2+v(2)^2); n2(i,:)=[v(2),-v(1)];
end
for i=1:length(a3x),
        if i==1,
                v1=[a3x(i)-a3x(length(a3x)-1),a3y(i)-a3y(length(a3x)-1)];
        else
                v1=[a3x(i)-a3x(i-1),a3y(i)-a3y(i-1)];
        end
        if i==length(a3x),
                v2=[a3x(4)-a3x(i),a3y(4)-a3y(i)];
        else
                v2=[a3x(i+1)-a3x(i),a3y(i+1)-a3y(i)];
        end
        d1=sqrt(v1(1)^2+v1(2)^2); d2=sqrt(v2(1)^2+v2(2)^2);v=v1*d2+v2*d1;
        v=v/sqrt(v(1)^2+v(2)^2); n3(i,:)=[v(2),-v(1)];
end
for i=1:length(a4x),
        if i==1,
                v1=[a4x(i)-a4x(length(a4x)-1),a4y(i)-a4y(length(a4x)-1)];
        else
                v1=[a4x(i)-a4x(i-1),a4y(i)-a4y(i-1)];
        end
        if i==length(a4x),
                v2=[a4x(4)-a4x(i),a4y(4)-a4y(i)];
        else
                v2=[a4x(i+1)-a4x(i),a4y(i+1)-a4y(i)];
        end
        d1=sqrt(v1(1)^2+v1(2)^2); d2=sqrt(v2(1)^2+v2(2)^2);v=v1*d2+v2*d1;
        v=v/sqrt(v(1)^2+v(2)^2); n4(i,:)=[v(2),-v(1)];
end
n2(1,:)=n2(2,:); n3(1,:)=n3(2,:);n4(1,:)=n4(2,:);
bimgx=zeros(N,N); bimgy=zeros(N,N);[xxm,yym]=meshgrid(1:128,1:128);
for i=1:length(a1x),bimgx(floor(a1x(i)),floor(a1y(i)))=n1(i,1);
        bimgy(floor(a1x(i)),floor(a1y(i)))=n1(i,2);end
for i=1:length(a2x),bimgx(floor(a2x(i)),floor(a2y(i)))=n2(i,1);
        bimgy(floor(a2x(i)),floor(a2y(i)))=n2(i,2);end
for i=1:length(a3x),bimgx(floor(a3x(i)),floor(a3y(i)))=n3(i,1);
        bimgy(floor(a3x(i)),floor(a3y(i)))=n3(i,2);end
for i=1:length(a4x),bimgx(floor(a4x(i)),floor(a4y(i)))=n4(i,1);
        bimgy(floor(a4x(i)),floor(a4y(i)))=n4(i,2);end
quiver(xxm,yym,bimgy,bimgx,3); zoom on; axis([1 128 1 128]); axis ij;

