Advertisement
bueddl

Continious Bezier Curves in Matlab (iterative)

Oct 28th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.19 KB | None | 0 0
  1. %% ------------------------------------------------------------------------
  2. % Cleanup
  3. %%
  4. clc;
  5. clear variables;
  6. figure, hold on;
  7. %axis([0 10 -1 9]);
  8.  
  9. %% ------------------------------------------------------------------------
  10. % Define
  11. %%
  12. n = 4;
  13. pmts = zeros([n 5]);
  14.  
  15. pnts(1, 1:2) = [1 0];   % Point (x|y)
  16. pnts(1, 3)   = 7.8*pi/8; % Angle
  17. pnts(1, 4:5) = [0 3];   % Vector lengths
  18.  
  19. pnts(2, 1:2) = [9 1];  % Point (x|y)
  20. pnts(2, 3)   = -7*pi/8; % Angle
  21. pnts(2, 4:5) = [3 1.5];  % Vector lengths
  22.  
  23. pnts(3, 1:2) = [12 5];  % Point (x|y)
  24. pnts(3, 3)   = -pi; % Angle
  25. pnts(3, 4:5) = [3 1.5];  % Vector lengths
  26.  
  27. pnts(4, 1:2) = [17 3];  % Point (x|y)
  28. pnts(4, 3)   = 5*pi/8; % Angle
  29. pnts(4, 4:5) = [1.5 0];  % Vector lengths
  30.    
  31. %% ------------------------------------------------------------------------
  32. % Draw
  33. %%
  34. plot(pnts(:, 1), pnts(:, 2), 's:', 'Color', [1 0 0]);
  35. for i=1:n
  36.     text(pnts(i, 1)+0.2, pnts(i, 2)+0.2, sprintf('P%d', i));
  37.     for j=4:5
  38.         if pnts(i, j) > 0
  39.             angle = pnts(i, 3);
  40.             if j==5
  41.                 angle = angle + pi;
  42.             end
  43.             length = pnts(i, j);
  44.             ctrl_x = pnts(i, 1)+cos(angle)*length;
  45.             ctrl_y = pnts(i, 2)+sin(angle)*length;
  46.             plot(ctrl_x, ctrl_y, 'o', 'Color', [0.6 0.8 0.3]);
  47.             plot([pnts(i, 1) ctrl_x], [pnts(i, 2) ctrl_y], '-', 'Color', [0.6 0.8 0.3]);
  48.             text(ctrl_x+0.2, ctrl_y+0.2, sprintf('P%d_%d', i, j-3));
  49.         end
  50.     end
  51. end
  52. %% ------------------------------------------------------------------------
  53. % !)(&/§%!"&/$§/&"%$
  54. %%
  55.  
  56. % number of curves
  57. N=n-1;
  58. % for each ancient two points (draw a bezier curve)
  59. for q=1:N
  60.     %result vector
  61.     B_x=[];
  62.     B_y=[];
  63.     % ttl points (pnts + ctrlpnts)
  64.     z = 2;
  65.     k = (z-2)*2 + 2 + z;
  66.     % point buffer
  67.     D = zeros(k, k, 2); % step * point * (x,y)
  68.     % initialize from points, Pa_0, Pa, Pa_1, Pb_0, Pb, Pb_1 etc...
  69.  
  70.     % pnt q
  71.     D(1, 1, :) = pnts(q, 1:2);
  72.     % ctrlpng after pnt q
  73.     angle = pnts(q, 3) + pi; % 180° ahead of ctrl pnt before pnt
  74.     length = pnts(q, 5);
  75.     ctrl_x = pnts(q, 1)+cos(angle)*length;
  76.     ctrl_y = pnts(q, 2)+sin(angle)*length;
  77.     D(1, 2, :) = [ctrl_x ctrl_y];
  78.  
  79.     % ctrlpnt before pnt q+1
  80.     angle = pnts(q+1, 3);
  81.     length = pnts(q+1, 4);
  82.     ctrl_x = pnts(q+1, 1)+cos(angle)*length;
  83.     ctrl_y = pnts(q+1, 2)+sin(angle)*length;
  84.     D(1, 3, :) = [ctrl_x ctrl_y];
  85.    
  86.     % pnt q+1
  87.     D(1, 4, :) = pnts(q+1, 1:2);
  88.  
  89.     % t from 0 to 1
  90.     for t=0:0.01:1
  91.         % One calculation step for each additional point (k-1 steps,
  92.         % k=pnts+ctrlpnts); 1->2, 2->3, 3->4
  93.         for j=2:k
  94.             % calculate each line in that iteration (as many lines to calculate as iterations left)
  95.             for i=1:k-j+1
  96.                 a = D(j-1,i,:);   % from pnt (previous iteration)
  97.                 b = D(j-1,i+1,:); % to pnt (previous iteration)
  98.                 % calc point on line
  99.                 v = a * (1-t) + b * t;
  100.                 D(j,i,:)=v;       % save for next iteration
  101.             end
  102.         end
  103.         % D(k-1,1) is point on curve
  104.         B_x=[B_x D(k,1,1)]; % X
  105.         B_y=[B_y D(k,1,2)]; % Y
  106.     end
  107.  
  108.     plot(B_x, B_y, 'b');
  109. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement