%MONOPOLE
%   Creates a single monopole or a monopole array 
%   on a finite ground plane using mouse input
%
%   Copyright 2002 AEMM. Revision 2002/03/12 
%   Chapter 4

clear all
warning off

%   The following parameters need to be specified:

L=2.0;      %Plate length (along the x-axis)
W=2.0;      %Plate width (along the y-axis)
Nx=11;      %Discretization parameter (length)
Ny=11;      %Discretization parameter (width)
h=1.0;      %Monopole height
Number=7;   %Number of monopole rectangles

%Set vertexes of the plate
epsilon=1e-6; 
M=1;
for i=1:Nx+1
    for j=1:Ny+1
        X(M)=-L/2+(i-1)/Nx*L;
        Y(M)=-W/2+(j-1)/Ny*W-epsilon*X(M);
        M=M+1;
    end
end

%Identify probe feed edge(s)
x=[-0.02 0.02];
y=[0 0];
X=[X x];
Y=[Y y];
C=mean(x);
x1=[C C];
y1=mean(y)+2*[max(x)-C min(x)-C];
X=[X x1];
Y=[Y y1];

%Use Delaunay triangulation
TRI = delaunay(X,Y,0); 
t=TRI'; t(4,:)=0; %plate has the index zero!!!
p=[X; Y; zeros(1,length(X))];
save plate p t 


%Identify junction edge(s)using mouse input
%Use RETURN key to stop the process
clear figure
hold off
viewer plate; view(0,90);
hold on

FeedingTriangle=[];
TRI=t(1:3,:)';
while ~isempty(t)
    [xi,yi]=ginput(1);
    TriangleNumber = tsearch(p(1,:),p(2,:),TRI,xi,yi);
    n=t(1:3,TriangleNumber);
    FeedingTriangle= [FeedingTriangle TriangleNumber];
    x= p(1,n);
    y= p(2,n);
    if isempty(xi|yi) break; end
    fill(x,y,'w')
    clear xi yi
end
clear figure


%Create the monopole(s)
for n=1:length(FeedingTriangle)/2
    %find bottom (feeding) edge
    FT=[FeedingTriangle(2*n-1) FeedingTriangle(2*n)];
    N=t(1:3,FT(1));
    M=t(1:3,FT(2));
    a=1-all([N-M(1) N-M(2) N-M(3)]);
    Edge_B=M(find(a)); 
    %find top edge
    p=[p p(:,Edge_B(1))+[0;0;h] p(:,Edge_B(2))+[0;0;h]];
    Edge_T  =[length(p)-1; length(p)];
    %fill the strip
    Edge_MM=Edge_B;
    for k=1:Number-1
        p(:,length(p)+1)=k/Number*(p(:,Edge_T(1))-p(:,Edge_B(1)))+p(:,Edge_B(1));
        p(:,length(p)+1)=k/Number*(p(:,Edge_T(2))-p(:,Edge_B(2)))+p(:,Edge_B(2));
        Edge_M=[length(p)-1,length(p)];
        tFeed1(:,k)  =[Edge_MM(1);Edge_MM(2);Edge_M(2);1];
        tFeed2(:,k)  =[Edge_MM(1);Edge_M(1);Edge_M(2);1];
        Edge_MM=Edge_M;
    end
    %update array t
    tFeed3  =[Edge_M(1);Edge_M(2);Edge_T(2);1];
    tFeed4  =[Edge_M(1);Edge_T(1);Edge_T(2);1];
    t=[t tFeed1 tFeed2 tFeed3 tFeed4];
end

%Save result
save monopole p t 
hold off
clear figure
viewer monopole
    
    
    




