% matlab script to illustrate the basic concepts of Bezier curves

clear all
close all

% cubic Bezier curves require 2 nodes and 2 control points

% prompt for nodes and control points
node1 = input('Please enter  first node: ');
node2 = input('Please enter second node: ');

% numbering of control points corresponds to numbering of nodes
control1 = input('Please enter  first control point: ');
control2 = input('Please enter second control point: ');

% evaluate for t between 0 and 1 at some suitable spacing
t = [0:0.001:1]';
% it is useful to compute this once and for all because it is re-used
oneMinusT = ones(length(t),1)-t; 

% compute Bernstein polynomials literally for clarity
% (in practice a recursive algorithm is used)
B30 =   oneMinusT.^3;
B31 = 3*(oneMinusT.^2).*(t);
B32 = 3*(oneMinusT).*(t.^2);
B33 =   t.^3;

% set up x and y points of the Bezier curve for plotting
x = node1(1)*B30 + control1(1)*B31 + control2(1)*B32 + node2(1)*B33;
y = node1(2)*B30 + control1(2)*B31 + control2(2)*B32 + node2(2)*B33;

% plot the Bezier curve along with the input data
figure(1)
hold on
plot([node1(1), control1(1)], [node1(2), control1(2)],'--o')
plot([node2(1), control2(1)], [node2(2), control2(2)],'--o')
plot(x,y)