Code covered by the BSD License

# Simple Fuzzy Control Demonstration

### Husam Aldahiyat (view profile)

Steering a car in the middle of a road based on fuzzy logic.

testfuzz
function testfuzz
% FUZZY CONTROL LOGIC CAR HOVER PROE
% numandina@gmail.com

global p PX
figure('color',[1,1,1],'units','normalized','position',[.1 .1 .8 .8],'numbertitle','off',...
uipanel('un','n','pos',[.05 .05 .6 .6],'backgroundc',[1,1,1])
axes('units','normalized','position',[.05 .05 .6 .6])
rotate3d on

kd = findobj('type','uicontrol');
uicontrol('style','text','un','n','pos',[.1/20 .8 .125 .025],'string','Right Wheel Sensor: ','fontw','b','horizontalal','r','backgroundc',[1,1,1])
uicontrol('style','text','un','n','pos',[.1/20 .825 .125 .025],'string','Left Wheel Sensor: ','fontw','b','horizontalal','r','backgroundc',[1,1,1])
uicontrol('style','text','un','n','pos',[.1/20 .775 .125 .025],'string','Error: ','fontw','b','horizontalal','r','backgroundc',[1,1,1])
uicontrol('style','text','un','n','pos',[.1/20 .75 .125 .025],'string','Change in Error: ','fontw','b','horizontalal','r','backgroundc',[1,1,1])
uicontrol('style','text','un','n','pos',[.1/20 .725 .125 .025],'string','Output: ','fontw','b','horizontalal','r','backgroundc',[1,1,1])
uicontrol('style','text','un','n','pos',[.1/20 .85 .125 .025],'string','Time: ','fontw','b','horizontalal','r','backgroundc',[1,1,1])

t1 = uicontrol('style','text','un','n','pos',[.1/20 .8 .1 .025]+[.125,0,0,0],'string',' ','fontw','b','horizontalal','l','backgroundc',[1,1,1]);
t2 = uicontrol('style','text','un','n','pos',[.1/20 .825 .1 .025]+[.125,0,0,0],'string',' ','fontw','b','horizontalal','l','backgroundc',[1,1,1]);
t3 = uicontrol('style','text','un','n','pos',[.1/20 .775 .1 .025]+[.125,0,0,0],'string',' ','fontw','b','horizontalal','l','backgroundc',[1,1,1]);
t4 = uicontrol('style','text','un','n','pos',[.1/20 .75 .1 .025]+[.125,0,0,0],'string',' ','fontw','b','horizontalal','l','backgroundc',[1,1,1]);
t5 = uicontrol('style','text','un','n','pos',[.1/20 .725 .1 .025]+[.125,0,0,0],'string',' ','fontw','b','horizontalal','l','backgroundc',[1,1,1]);
t6 = uicontrol('style','text','un','n','pos',[.1/20 .85 .1 .025]+[.125,0,0,0],'string',' ','fontw','b','horizontalal','l','backgroundc',[1,1,1]);

kd2 = findobj('type','uicontrol');

kdn = kd2(~ismember(kd2,kd));

for  hk = 1:length(kdn)

x = get(kdn(hk),'position');
set(kdn(hk),'position',x + [.1,0,0,0]);

end

uipanel('un','n','pos',[.65 .05 .3 .6],'backgroundc',[1,1,1])

uipanel('un','n','pos',[0.05 .7 .3 .2],'backgroundc',[1,1,1])

hg2 = uibuttongroup('un','n','pos',[.675 .45 .25 .15],'backgroundc',[1,1,1],'SelectionChangeFcn',@init);

hh1 = uicontrol('style','radiob','parent',hg2,'un','n','pos',[0,0,1,.5],'string','Super Future Hover Car','backgroundc',[1,1,1],...
'fonts',14,'fontw','b','value',0);
'fonts',14,'fontw','b','value',1);

function init(varargin)
cla
axis off
axis on
set(gca,'xtick',[])
set(gca,'ytick',[])
set(gca,'ztick',[])
set(gca,'color',[0,.5,0])
switch get(hg2,'SelectedObject')
case hh2
V = V2*.0075;
case hh1
V = V2*1;
end
% 		load proe3 V3 F3 C3

C = C2;
F = F2;

cla;
hold on
line([-2 8],[0,0],[0,0],'color',[1,1,1],'linewidth',2,'linestyle','--')
line([-2 18],[-8,-8],[0,0],'color',[1,1,1],'linewidth',2,'linestyle','--')
line([-2 8],[-3,-3],[0,0],'color',[1,1,0]*0,'linewidth',2,'linestyle','-')
line([-2 8],[3,3],[0,0],'color',[1,1,0]*0,'linewidth',2,'linestyle','-')

hold on
x = pi/2:.01:pi;
c1 = sin(x)*5+8;
c2 = cos(x)*5+5;
c1p = sin(x)*8+8;
c2p = cos(x)*8+5;
c3p = sin(x)*2+8;
c4p = cos(x)*2+5;
patch([c1p fliplr(c3p)],[c2p fliplr(c4p)],[0,0,0],'edgecolor','none')
plot3(c1,c2,zeros(size(x)),'linestyle','--','color',[1,1,1],'linewidth',2)

a = [-2 -5 0;...
8 -5 0;...
8 5 0;...
-2 5 0;...
-2 -5 1;...
8 -5 1;...
8 5 1;...
-2 5 1];

a(:,3) = a(:,3)*0;
a(:,2) = a(:,2)*0.5;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
patch('vertices',a,...
'faces',b,...
'facecolor',[.1 .1 .1],'edgecolor','none');

a = [-2 -5 0;...
8 -5 0;...
8 5 0;...
-2 5 0;...
-2 -5 1;...
8 -5 1;...
8 5 1;...
-2 5 1];

a(:,3) = a(:,3)*0;
a(:,1) = a(:,1)*2+2;
a(:,2) = a(:,2)*0.5-8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'facecolor',[.1 .1 .1],'edgecolor','none');
for k = 2:-2:-6
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) - k;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
end
for k = 2:-2:-16
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5-8;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) - k;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
end
for k = 2:-2:-16
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5-3;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) - k;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
end
for k = 1:-2:-17
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5-8;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) - k;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.7 .7 .1]+.2,...
'linewidth',2,...
'linestyle','-');
end
for k = 1:-2:-17
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5-3;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) - k;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.7 .7 .1]+.2,...
'linewidth',2,...
'linestyle','-');
end
for k = -3:-2:-10
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5-8;
a(:,2) = a(:,2)-3;
a(:,2) = a(:,2) - k;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.7 .7 .1]+.2,...
'linewidth',2,...
'linestyle','-');
rotate(mp,[0,0,1],90,[1,a(3,2),0])
xx = get(mp,'vertices');
xx(:,1) = xx(:,1)+19;
xx(:,2) = xx(:,2)+6.5;
set(mp,'vertices',xx)
end
for k = -3:-2:-10
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5-8;
a(:,2) = a(:,2)-3;
a(:,2) = a(:,2) - k;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.7 .7 .1]+.2,...
'linewidth',2,...
'linestyle','-');
rotate(mp,[0,0,1],90,[1,a(3,2),0])
xx = get(mp,'vertices');
xx(:,1) = xx(:,1)+24.5;
xx(:,2) = xx(:,2)+6.5;
set(mp,'vertices',xx)
end
for k = -2:-2:-10
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5-8;
a(:,2) = a(:,2)-3;
a(:,2) = a(:,2) - k;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
rotate(mp,[0,0,1],90,[1,a(3,2),0])
xx = get(mp,'vertices');
xx(:,1) = xx(:,1)+24.5;
xx(:,2) = xx(:,2)+6.5;
set(mp,'vertices',xx)
end
for k = -2:-2:-10
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5-8;
a(:,2) = a(:,2)-3;
a(:,2) = a(:,2) - k;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
rotate(mp,[0,0,1],90,[1,a(3,2),0])
xx = get(mp,'vertices');
xx(:,1) = xx(:,1)+19;
xx(:,2) = xx(:,2)+6.5;
set(mp,'vertices',xx)
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],0,[8 5 0]);			xx = get(mp,'vertices'); 			xx(:,2) = xx(:,2)-8;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.7 .7 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],30,[8 5 0]);xx = get(mp,'vertices'); 			xx(:,2) = xx(:,2)-8;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],60,[8 5 0]);xx = get(mp,'vertices'); 			xx(:,2) = xx(:,2)-8;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],0,[8 5 0]);			xx = get(mp,'vertices'); 			xx(:,2) = xx(:,2)-8;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
for k = 7.5:7.5:7.5*11
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
if ~mod(k,15)
ct = .1;
else
ct = .7;
end
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[ct ct .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],k,[8 5 0])
xx = get(mp,'vertices');
xx(:,2) = xx(:,2)-8;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end

for k = 2:-2:-6
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) - k;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
end
for k = 1:-2:-7
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) - k;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.7 .7 0]+.2,...
'linewidth',2,...
'linestyle','-');
end
for k = 1:-2:-7
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) - k;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.7 .7 0]+.2,...
'linewidth',2,...
'linestyle','-');
end

%%
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],0+90,[8 5 0]);			xx = get(mp,'vertices'); 			xx(:,2) = xx(:,2)-0;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.7 .7 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],30+90,[8 5 0]);xx = get(mp,'vertices'); 			xx(:,2) = xx(:,2)-0;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],60+90,[8 5 0]);xx = get(mp,'vertices'); 			xx(:,2) = xx(:,2)-0;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],0+90,[8 5 0]);			xx = get(mp,'vertices'); 			xx(:,2) = xx(:,2)-0;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
for k = 7.5:7.5:7.5*11
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
if ~mod(k,15)
ct = .1;
else
ct = .7;
end
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[ct ct .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],k+90,[8 5 0])
xx = get(mp,'vertices');
xx(:,2) = xx(:,2)-0;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
%%

%%
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],0+180,[8 5 0]);			xx = get(mp,'vertices'); 			xx(:,2) = xx(:,2)-0;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.7 .7 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],30+180,[8 5 0]);xx = get(mp,'vertices'); 			xx(:,2) = xx(:,2)-0;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],60+180,[8 5 0]);xx = get(mp,'vertices'); 			xx(:,2) = xx(:,2)-0;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],0+180,[8 5 0]);			xx = get(mp,'vertices'); 			xx(:,2) = xx(:,2)-0;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
for k = 7.5:7.5:7.5*11
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
if ~mod(k,15)
ct = .1;
else
ct = .7;
end
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[ct ct .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],k+180,[8 5 0])
xx = get(mp,'vertices');
xx(:,2) = xx(:,2)-0;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
%%

for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],0,[8 5 0])
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.7 .7 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],30,[8 5 0])
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],60,[8 5 0])
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],0,[8 5 0])
% 	pause(.1)
% 	end
end
for k = 7.5:7.5:7.5*11
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
if ~mod(k,15)
ct = .1;
else
ct = .7;
end
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[ct ct .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],k,[8 5 0])
% 	pause(.1)
% 	end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],0,[8 5 0])
% 	pause(.1)
% 	end
xx = get(mp,'vertices');
xx(:,2) = xx(:,2)-8;xx(:,1)=xx(:,1)+10;
set(mp,'vertices',xx)

end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.7 .7 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],30,[8 5 0])
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)+2.5;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],60,[8 5 0])
% 	pause(.1)
% 	end
end
for k = 1
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[.1 .1 .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],0,[8 5 0]);			xx = get(mp,'vertices'); 			xx(:,2) = xx(:,2)-8;xx(:,1)=xx(:,1)+10;			set(mp,'vertices',xx)
% 	pause(.1)
% 	end
end
for k = 7.5:7.5:7.5*11
a = [0 0 0;...
1 0 0;...
1 1 0;...
0 1 0;...
0 0 1;...
1 0 1;...
1 1 1;...
0 1 1];

a(:,3) = a(:,3)*.5;
a(:,2) = a(:,2)*.5;
a(:,2) = a(:,2)-3;
a(:,1) = a(:,1) + 8;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
if ~mod(k,15)
ct = .1;
else
ct = .7;
end
mp = patch('vertices',a,...
'faces',b,...
'edgecolor',[0,0,0],...
'facecolor',[ct ct .1]+.2,...
'linewidth',2,...
'linestyle','-');
% 	for k2=1:200
rotate(mp,[0,0,1],k,[8 5 0])
% 	pause(.1)
% 	end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

p = patch('faces', F, 'vertices' ,V);
%set(p, 'facec', 'b');              % Set the face color (force it)
set(p, 'facec', 'flat');            % Set the face color flat

set(p, 'FaceVertexCData', C);       % Set the color (from file)
%set(p, 'facealpha',.4)             % Use for transparency
set(p, 'EdgeColor','none');         % Set the edge color
%set(p, 'EdgeColor',[1 0 0 ]);      % Use to see triangles, if needed.
light                               % add a default light
% daspect([1 1 1])                    % Setting the aspect ratio
% 				p22 = patch('faces', F3, 'vertices' ,V3);
% 		%set(p, 'facec', 'b');              % Set the face color (force it)
% 		set(p22, 'facec', 'flat');            % Set the face color flat
%
% 		set(p22, 'FaceVertexCData', C3);       % Set the color (from file)
% 		%set(p, 'facealpha',.4)             % Use for transparency
% 		set(p22, 'EdgeColor','none');         % Set the edge color

% axis qu
% view(3)                             % Isometric view

V = V';
V = [V(1,:); V(2,:); V(3,:); ones(1,length(V))];

nv = rx(90)*V;
set(p,'Vertices',nv(1:3,:)')
nv = rz(180)*nv;
set(p,'Vertices',nv(1:3,:)')
% 		V3 = V3';
% 		V3 = [V3(1,:); V3(2,:); V3(3,:); ones(1,length(V3))];
%
% 		nv = rx(90)*V3;
% 		set(p22,'Vertices',nv(1:3,:)')
% 		nv = rz(180)*nv;
% 		set(p22,'Vertices',nv(1:3,:)')

if ~isnan(str2double(get(dumc1,'string')))
view(str2double(get(dumc1,'string')),str2double(get(dumc2,'string')))
else
view(-90,75)
end

axis([-2 30 -5 5 -1 10]*1)
axis equal
% nkjlnjlkn
% axis off
set(gcf,'color',[1,1,1])
% line([],[],[],'color',
set(p,'ydata',get(p,'ydata')-get(s3,'value'))
io = 0;
pg = 0;
% 		pause(1)
posz = get(p,'zdata');

h = findobj('parent',gca);

hold on
x = pi/2:.01:pi;
c1 = sin(x)*5+8+10;
c2 = cos(x)*5+5;
c1p = sin(x)*8+8+10;
c2p = cos(x)*8+5;
c3p = sin(x)*2+8+10;
c4p = cos(x)*2+5;
patch([c1p fliplr(c3p)],[c2p fliplr(c4p)],[0,0,0],'edgecolor','none')
plot3(c1,c2,zeros(size(x)),'linestyle','--','color',[1,1,1],'linewidth',2)

h2 = findobj('parent',gca);
hn = h2(~ismember(h2,h));
% 				delete(h2(ismember(h2,h)))
rotate(hn,[0,0,1],180,[18,5,0])

hold on
x = pi/2:.01:pi;
c1 = sin(x)*5+8+10;
c2 = cos(x)*5+5;
c1p = sin(x)*8+8+10;
c2p = cos(x)*8+5;
c3p = sin(x)*2+8+10;
c4p = cos(x)*2+5;
patch([c1p fliplr(c3p)],[c2p fliplr(c4p)],[0,0,0],'edgecolor','none')
plot3(c1,c2,zeros(size(x)),'linestyle','--','color',[1,1,1],'linewidth',2)

h3 = findobj('parent',gca);
hn = h3(~ismember(h3,h2));
% delete(h2(ismember(h2,h)))
rotate(hn,[0,0,1],90,[18,5,0])

hold on
x = pi/2:.01:pi;
c1 = sin(x)*5+8+10;
c2 = cos(x)*5-3;
c1p = sin(x)*8+8+10;
c2p = cos(x)*8-3;
c3p = sin(x)*2+8+10;
c4p = cos(x)*2-3;
patch([c1p fliplr(c3p)],[c2p fliplr(c4p)],[0,0,0],'edgecolor','none')
plot3(c1,c2,zeros(size(x)),'linestyle','--','color',[1,1,1],'linewidth',2)

h4 = findobj('parent',gca);
hn = h4(~ismember(h4,h3));
% delete(h2(ismember(h2,h)))
% 		rotate(hn,[0,0,1],0,[18,5,0])

line([23 23],[-3 5],[0,0],'color',[1,1,1],'linewidth',2,'linestyle','--')

a = [18 -8 0;...
24 -8 0;...
24 8 0;...
18 8 0;...
18 -8 1;...
24 -8 1;...
24 8 1;...
18 8 1];

a(:,3) = a(:,3)*0;
a(:,2) = a(:,2)*0.5;
a(:,1) = a(:,1)+2;
a(:,2) = a(:,2)+1;
b=[1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
patch('vertices',a,...
'faces',b,...
'facecolor',[.1 .1 .1],'edgecolor','none');
PX = get(p,'ydata');
end

function main(varargin)

g = findobj('style','slider');
set([hh1,hh2,g(3)],'enable','off')

cla reset
init

if ~isnan(str2double(get(dumc1,'string')))
view(str2double(get(dumc1,'string')),str2double(get(dumc2,'string')))
else
view(-90,75)
end

axis([-2 30 -5 5 -1 10]*1)
axis equal
% nkjlnjlkn
% axis off
set(gcf,'color',[1,1,1])
% line([],[],[],'color',
set(p,'ydata',get(p,'ydata')-get(s3,'value'))
io = 0;
pg = 0;
% 		pause(1)
posz = get(p,'zdata');
poszf = mean([max(max(posz)),min(min(posz))]);
% nmjknkjlnljk

% 		reald = l.reald;

c1 = sin(x)*5+8;
c2 = cos(x)*5+5;

x = -pi/2:.01:pi/2;
c12 = sin(x)*5+18;
c22 = cos(x)*5+5;

x = -3/2*pi:.01:-pi;
c13 = sin(x)*5+18;
c23 = cos(x)*5-3;

tic
while true

posy = get(p,'ydata');
posx = get(p,'xdata');

sp = get(s1,'value');

nesb = get(s2,'value');
if mean(mean(posx)) <= 8 && mean(mean(posy)) > -5
r = mean(mean(posy));
i1 = -r*nesb;
i2 = i1-io;
io = i1;
elseif mean(mean(posx)) > 8 &&  mean(mean(posx)) < 17 && mean(mean(posy)) <= 5 && mean(mean(posy)) > -5
xt = mean(mean(posx));
yt = mean(mean(posy));
xtm = sum(xt - c1,1);
xti = find(abs(xtm)==min(abs(xtm)));
yt2 = c2(xti);

i1 = yt2-yt;
i1 = i1*nesb;
i2 = i1-io;
io = i1;
elseif mean(mean(posy)) > 5
xt = mean(mean(posx));
yt = mean(mean(posy));
xtm = sum(xt - c12,1);
xti = find(abs(xtm)==min(abs(xtm)));
yt2 = c22(xti);

i1 = yt2-yt;
i1 = i1*nesb;
i2 = i1-io;
io = i1;

elseif mean(mean(posy)) < 5 && mean(mean(posx)) > 18 &&  mean(mean(posy)) > -3
r = mean(mean(posx));
i1 = (23-r)*nesb;
i2 = i1-io;
io = i1;

elseif mean(mean(posx)) > 18
xt = mean(mean(posx));
yt = mean(mean(posy));
xtm = sum(xt - c13,1);
xti = find(abs(xtm)==min(abs(xtm)));
yt2 = c23(xti);

i1 = yt2-yt;
i1 = -i1*nesb;
i2 = i1-io;
io = i1;

elseif mean(mean(posx)) > -1
r = mean(mean(posy));
i1 = (8+r)*nesb;
i2 = i1-io;
io = i1;
else
break
end
set(t1,'string',num2str(5-i1/2));
set(t2,'string',num2str(5+i1/2));
set(t3,'string',num2str(i1));
set(t4,'string',num2str(i2));
set(t5,'string',num2str(o1*180/pi));
set(t6,'string',num2str(toc));
rotate(p,[0,0,1],o1*180/pi*sp,[mean(mean(posx)),mean(mean(posy)),poszf])
pg = o1*180/pi*sp + pg;
set(p,'xdata',get(p,'xdata')+sp*cosd(pg),'ydata',get(p,'ydata')+sp*sind(pg))
pause(.001);

end
g = findobj('style','slider');
set([hh1,hh2,g(3)],'enable','on')
init
end

uicontrol('sty','push','un','n','pos',[.8 .2 .1 .05],'string','Change Camera','callback',@chc,'fontw','b','backgroundc',[1,1,1])
uicontrol('sty','text','un','n','pos',[.69 .24 .1 .1/2],'string','Car Speed','fontw','b','backgroundc',[1,1,1],'fonts',12,'horizontalal','r')
uicontrol('sty','text','un','n','pos',[.69 .34 .1 .1/2],'string','Initial X','fontw','b','backgroundc',[1,1,1],'fonts',12,'horizontalal','r')
uicontrol('sty','text','un','n','pos',[.66 .29 .13 .1/2],'string','Response Speed','fontw','b','backgroundc',[1,1,1],'fonts',12,'horizontalal','r')
s3 = uicontrol('sty','slide','un','n','pos',[.8 .35 .1 .05],'string','Change Camera','fontw','b','min',-5,'max',5,'value',.25,'callback',@fff);

function fff(varargin)

set(p,'ydata',PX-get(s3,'value'));

end

s1 = uicontrol('sty','slide','un','n','pos',[.8 .25 .1 .05],'string','Change Camera','fontw','b','min',.01,'max',.8,'value',.2);
s2 = uicontrol('sty','slide','un','n','pos',[.8 .3 .1 .05],'string','Change Camera','fontw','b','min',.01,'max',1,'value',.25);
uicontrol('sty','push','un','n','pos',[.8 .15 .1 .05],'string','Go','callback',@main,'fontw','b','backgroundc',[1,1,1])
dumc1 = uicontrol('style','text','visible','off');
dumc2 = uicontrol('style','text','visible','off');
axis off
chc

function chc(varargin)
x1 = rand*-90;
x2 = rand*90;
view(x1,x2)
set(dumc1,'string',num2str(x1))
set(dumc2,'string',num2str(x2))
end

function Rx = rx(THETA)
%
THETA = THETA*pi/180;  % Note: THETA in radians.
c = cos(THETA);
s = sin(THETA);
Rx = [1 0 0 0; 0 c -s 0; 0 s c 0; 0 0 0 1];
%
end

function Rz = rz(THETA)
%
THETA = THETA*pi/180;  %Note: THETA is in radians.
c = cos(THETA);
s = sin(THETA);
Rz = [c -s 0 0; s c 0 0; 0 0 1 0; 0 0 0 1];
%
end

init
PX = get(p,'ydata');
fff
end