
%% clear and close all child forms
delete(allchild(0));close all;
clear;
clc;

%% plot coordinates system in 3D area and labeling
plot3([-5;5],[-5;5]*0,[-5;5]*0,'k');hold on;
plot3([-5;5]*0,[-5;5],[-5;5]*0,'k');hold on
plot3([-5;5]*0,[-5;5]*0,[-5;5],'k');hold on
text([-6;6],[-6;6]*0,[-6;6]*0,{'-X','X'});hold on;
text([-6;6]*0,[-6;6],[-6;6]*0,{'-Y','Y'});hold on
text([-6;6]*0,[-6;6]*0,[-6;6],{'-Z','Z'});hold on
xlabel('X');
ylabel('Y');
zlabel('Z');


%% convert implicit plan formula as parametric form
syms x y z scalerVal;
plan='2*x+3*y+0*z+1';%% plan as inplicit form
pluIdex=strfind(plan,'+');%% find plus index in plan formula
scalerIndex=strsplit(plan,'+');
for s=1:length(scalerIndex)
    scalerVal=str2num(scalerIndex{s});
    if ~isempty(scalerVal)
        break;
    else
        scalerVal=0;
    end
end

%% extract z coff

zIndex=strfind(plan,'z');
if ~isempty(zIndex)
    pluIZ=pluIdex(pluIdex<zIndex);
    if ( pluIZ)
        pluIZ=pluIZ(end);
        cofZIndex=pluIZ+1:zIndex-2;
        cofZ=str2double(plan(cofZIndex));
    else
        if (zIndex>1)
            pluIZ=1;
            cofZIndex=pluIZ:zIndex-2;
            cofZ=str2double(plan(cofZIndex));
        else
            cofZ=1;
        end
    end
else
    cofZ=0;

end
%% extract y coff

yIndex=strfind(plan,'y');
if ~isempty(yIndex)
    pluIY=pluIdex(pluIdex<yIndex);
    if ( pluIY)
        pluIY=pluIY(end);
        cofYIndex=pluIY+1:yIndex-2;
        cofY=str2double(plan(cofYIndex));
    else
        if (yIndex>1)
            pluIY=1;
            cofYIndex=pluIY:yIndex-2;
            cofY=str2double(plan(cofYIndex));
        else
            cofY=1;
        end
    end
else
    cofY=0;

end
%% extract x coff
xIndex=strfind(plan,'x');
if ~isempty(xIndex)
    pluIX=pluIdex(pluIdex<xIndex);
    if ( pluIX)
        pluIX=pluIX(end);
        cofXIndex=pluIX+1:xIndex-2;
        cofX=str2double(plan(cofXIndex));
    else
        if (xIndex>1)
            pluIX=1;
            cofXIndex=pluIX:xIndex-2;
            cofX=str2double(plan(cofXIndex));
        else
            cofX=1;
        end
    end
else
    cofX=0;

end

coff=[cofX,cofY,cofZ];


funcIndex=find(coff);funcIndex=funcIndex(end);
switch funcIndex
    case 1
        y=[-2:2];
        z=[-2:2];
        [y,z]=meshgrid(y,z);
        x=-(coff(2)/coff(1))*y -(coff(3)/coff(1))*z-scalerVal/coff(1);

        
        
    case 2
        x=[-2:2];
        z=[-2:2];
        [x,z]=meshgrid(x,z);
        y=(coff(1)/coff(2))*x +(coff(3)/coff(2))*z+scalerVal/coff(2);

    case 3
        x=[-2:2];
        y=[-2:2];
        [x,y]=meshgrid(x,y);
        z=(coff(1)/coff(3))*x +(coff(2)/coff(3))*y+scalerVal/coff(3);
end


 
surf(x,y,z)
