% casteljau06.m
% fd, 17 octobre 2006, 04 mars 2025
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   clear ; format long

printf('casteljau06 : courbe de Bezier de Castejau de degre 4\n') ;


n=1 ; nn=5 ; 
pt = zeros(5 , 2) ; 
hold off ;
plot(pt(1,1),pt(1,2),'*;;');  % pause ; 
hold on ;

m=30 %% 150 ,
dth = 1 / m ; 
%%% x1=1; y1=0; x2=0 ; y2=1;  x3=.5; y3=2; x4=1; y4=1.5;
x1=1; y1=1; x2=.5; y2=2;  x3=0 ; y3=1;  x4=1; y4=0;
pt(1:1,1:1)=0; pt(2:2,1:1)=x1; pt(3:3,1)=x2; pt(4,1)=x3;
pt(1:1,2:2)=0; pt(2:2,2:2)=y1; pt(3:3,2)=y2; pt(4,2)=y3;
pt(5,1)=x4;  pt(5,2)=y4;

plot(pt(1:nn,1),pt(1:nn,2),'*k;noeuds;')
plot(pt(1:nn,1),pt(1:nn,2),'k;polygone de controle;')
axis ( [ -.1, 1.1 , -.1 , 2.1 ]) ;

%%%%%%      
printf('premiere generation de sommets : 4 segments\n') ;
pp1s1 = zeros(2,m+1) ; pp1s2 = zeros(2,m+1) ; pp1s3 = zeros(2,m+1) ; pp1s4 = zeros(2,m+1) ;

for k=1:m+1;
theta = (k-1)*dth ; 
pp1s1(:,k) =  (1-theta)*pt(1,:) + theta*pt(2,:) ; 
pp1s2(:,k) =  (1-theta)*pt(2,:) + theta*pt(3,:) ;  
pp1s3(:,k) =  (1-theta)*pt(3,:) + theta*pt(4,:) ;
pp1s4(:,k) =  (1-theta)*pt(4,:) + theta*pt(5,:) ;
end
plot(pp1s1(1,:) , pp1s1(2,:) , '*r;segment 1;'); pause (.1) 
plot(pp1s2(1,:) , pp1s2(2,:) , '*b;segment 2;');  pause (.1) 
plot(pp1s3(1,:) , pp1s3(2,:) , '*m;segment 3;');  pause (.1) 
plot(pp1s4(1,:) , pp1s4(2,:) , '*g;segment 4;');  pause (1) 

title ('courbe de Bezier de Casteljau de degre 4 : premiere generation de sommets') ;
print('06a-courbe-degre-4', '-djpg','-S891,630'); 

hold off 
%%%%%%      
printf('seconde generation de sommets : 3 courbes P2\n') ;
pp2s1 = zeros(2,m+1) ; pp2s2 = zeros(2,m+1) ; pp2s3 = zeros(2,m+1) ; 

plot(pt(1:nn,1),pt(1:nn,2),'k;polygone de controle;')
axis ( [ -.1, 1.1 , -.1 , 2.1 ]) ; hold on 

for k=1:m+1;
theta = (k-1)*dth ; 
pp2s1(:,k) =  (1-theta)*pp1s1(:,k) + theta*pp1s2(:,k) ;
pp2s2(:,k) =  (1-theta)*pp1s2(:,k) + theta*pp1s3(:,k) ;
pp2s3(:,k) =  (1-theta)*pp1s3(:,k) + theta*pp1s4(:,k) ;
end

plot(pp1s1(1,:) , pp1s1(2,:) , '+k;segment 1;');  pause (.1) 
plot(pp1s2(1,:) , pp1s2(2,:) , '+k;segment 2;');  pause (.1) 
plot(pp1s3(1,:) , pp1s3(2,:) , '+k;segment 3;');  pause (.1) 
plot(pp1s4(1,:) , pp1s4(2,:) , '+k;segment 4;');  pause (.1)  
plot(pp2s1(1,:) , pp2s1(2,:) , '*r;polynome degre 2 numero 1;'); pause (.1) 
plot(pp2s2(1,:) , pp2s2(2,:) , '*b;polynome degre 2 numero 2;'); pause (.1) 
plot(pp2s3(1,:) , pp2s3(2,:) , '*m;polynome degre 2 numero 3;'); pause (2) 

title ('courbe de Bezier de Casteljau de degre 4 : seconde generation de sommets') ;
print('06b-courbe-degre-4', '-djpg','-S891,630'); 

hold off 
%%%%%%      
printf('troisieme generation de sommets : 2 courbes polynomiales de degre 3\n') ;
pp3s1 = zeros(2,m+1) ; pp3s2 = zeros(2,m+1) ; 

plot(pt(1:nn,1),pt(1:nn,2),'k;polygone de controle;')
axis ( [ -.1, 1.1 , -.1 , 2.1 ]) ; hold on 

for k=1:m+1;
theta = (k-1)*dth ; 
pp3s1(:,k) =  (1-theta)*pp2s1(:,k) + theta*pp2s2(:,k) ;
pp3s2(:,k) =  (1-theta)*pp2s2(:,k) + theta*pp2s3(:,k) ;
end

plot(pp2s1(1,:) , pp2s1(2,:) , '+k;degre 2 numero 1;');  pause (.1) 
plot(pp2s2(1,:) , pp2s2(2,:) , '+k;degre 2 numero 2;');  pause (.1) 
plot(pp2s3(1,:) , pp2s3(2,:) , '+k;degre 2 numero 3;');  pause (.1) 
plot(pp3s1(1,:) , pp3s1(2,:) , '*r;polynome degre 3 numero 1;'); pause (.1) 
plot(pp3s2(1,:) , pp3s2(2,:) , '*b;polynome degre 3 numero 2;'); pause (2) 

title ('courbe de Bezier de Casteljau de degre 4 : troisieme generation de sommets') ;
print('06c-courbe-degre-4', '-djpg','-S891,630'); 

hold off 
%%%%%%      
printf('quatrieme generation de sommets : 1 courbes polynomiale de degre 4\n') ;
pp4s1 = zeros(2,m+1) ;  

for k=1:m+1;
theta = (k-1)*dth ; 
pp4s1(:,k) =  (1-theta)*pp3s1(:,k) + theta*pp3s2(:,k) ;
end

plot(pt(1:nn,1),pt(1:nn,2),'k;polygone de controle;')
axis ( [ -.1, 1.1 , -.1 , 2.1 ]) ; hold on


plot(pp3s1(1,:) , pp3s1(2,:) , '+k;degre 3 numero 1;');  pause (.1) 
plot(pp3s2(1,:) , pp3s2(2,:) , '+k;degre 3 numero 2;');  pause (.1) 
plot(pp4s1(1,:) , pp4s1(2,:) , '*r;polynome de degre 4;');  

title ('courbe de Bezier de Casteljau de degre 4 : quatrieme generation de sommets') ;
print('06d-courbe-degre-4', '-djpg','-S891,630'); 

hold off 

