%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% tabla çizim fonksiyonu, kendisine gönderilen 6 nokta için, yükseklik 
%değerine göre  tabla çizimi yapar
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [p_handle] = tabla_cizimi_genel( Points ,  yukseklik ,color,  p_handles )
%nargin=2 
if nargin < 4 ,     p_handles=0;   end
if nargin < 3 , color = [0.8,0.3,0.3]; end

%check for points that contains NaN and dont draw this .. 
%NaN must be on all element of a row
column1 = find(~isnan(Points(:,1)));
column2 = find(~isnan(Points(:,2)));
column3 = find(~isnan(Points(:,3)));

if length(column1)~=length(column2)
    error('NaN problem on platform data, please check that NaN must be all element of a row');
elseif length(column1)~=length(column3)
    error('NaN problem on platform data, please check that NaN must be all element of a row');
elseif length(column2)~=length(column3)
    error('NaN problem on platform data, please check that NaN must be all element of a row');
end

% all indices of Point that not contain NaN = column1
% only draw for this indices = column1

point_number = length(column1);

vertex = [ Points(column1,:); 
           Points(column1,:)-repmat([0,0,yukseklik],point_number,1)
           ];


if point_number>3
    faces = [ 1:point_number ; ] ;
    for i=1:1:point_number-1    
        faces = [ faces; i  i+1 i+point_number+1  i+point_number  ones(1,point_number-4)*NaN ];
    end    
    faces = [ faces ; point_number+1:2*point_number; 1 point_number 2*point_number point_number+1 ones(1,point_number-4)*NaN ; ];
    
elseif point_number==3
    faces = [ 1:point_number ones(1,4-point_number)*NaN ; ] ;
    for i=1:1:point_number-1    
        faces = [ faces; i  i+1 i+point_number+1  i+point_number  ones(1,point_number-4)*NaN ];
    end   
    faces = [ faces ; point_number+1:2*point_number NaN; 1 point_number 2*point_number point_number+1 ; ]           ;
else
    error('myApp:argChk', 'platform en az üç nokta içermelidir')
end
 
if nargin == 3 
        
    p_handle = patch('vertices',vertex,...
                 'faces',faces,...
                 'facecolor',color);
else 
    
    set(p_handles,'vertices',vertex);
    p_handle = p_handles;

end             