%function [x_3dgrid,y_3dgrid,count]=mapping2(rg)
T=60;
inc_iwrap=30;
rg=10;
Vp=120;
time_rotation=1;
delta_az=4;
delta_t= (delta_az*time_rotation)/360;
delta_d_plane=delta_t*Vp;
N=T/delta_t;
az_N=0:delta_az:356;
az_a=90-az_N;
u_neg=find(az_a<0);
az_a(u_neg)=az_a(u_neg)+360;
az=(az_a)'*ones(1,(T/time_rotation));
az=reshape(az,length(az)*T/time_rotation,1);
incid_angle=deg2rad(30);
Hp=2000;
Ha=500:15:1800;
rge=15:30:2000;

HR=rge*sin(deg2rad(inc_iwrap));

p_position_x=zeros(N,1);
p_position_y=((1:N)'.*delta_d_plane)-delta_d_plane;  
delta_x=150;
delta_y=150;
delta_z=30;
x_grid=0:delta_x:2100;  
x_grid=[-2100:delta_x:-150 x_grid];

y_grid=x_grid;%-2000:delta_y:2000;                                       
RO=delta_x/2;
%[x_3dgrid,y_3dgrid,z_3dgrid]=meshgrid(x_grid,y_grid,z_grid);
[x_3dgrid,y_3dgrid]=meshgrid(x_grid,y_grid);
[x_size,y_size,z_size]=size(x_3dgrid);
count(x_size,y_size)=0;

temp=HR(rg);
[th,r]=meshgrid(deg2rad(az),temp);
[x,y]=pol2cart(th,r);
 
x=x+p_position_x';
y=y+p_position_y';% it should be displacement if the fist plane position is not at 0


 x1=x;%reshape(x,length(az)*length(rge),1);
 y1=y;%reshape(y,length(az)*length(rge),1);
 cross_angle=999*ones(length(y_grid),length(x_grid),100);
for i=1:length(y_grid)
    for j=1:length(x_grid)
        
u=find(x>x_3dgrid(i,j)-RO & x<x_3dgrid(i,j)+RO);
v=find(y(u)>y_3dgrid(i,j)-RO & y(u)<y_3dgrid(i,j)+RO);
d=sqrt((x_3dgrid(i,j)-x1(u(v))).^2 + (y_3dgrid(i,j)-y1(u(v))).^2);
w=find(d<RO);
count(i,j)=length(w);
if length(w)>1
 cross_angle(i,j,1:length(w))=az(u(v(w)));
end
end
end


