%Problem 1 - Write a matlab function that uses adaptive quadrature to
%compute the arc length from t=0 t t = T for a given T <= 1.
clear;
f = @(x)((0.3+7.8*x-14.1*x^2)^2+(0.3+1.8*x-8.1*x^2)^2)^(1/2);
var = arclenquad(.5,f);
sprintf('Arc length from 0 to .5 is %4.5f\n',var)
clear;

%Problem 2 - Write a program that finds the time value at a specific
%percentage of the overall arc length.
%Bracketing Interval = [0,1]
f = @(x)((0.3+7.8*x-14.1*x^2)^2+(0.3+1.8*x-8.1*x^2)^2)^(1/2);
val = bisarclen(.5,.001,f);
sprintf('Time Position at 50 percent of the curve length with a tolerance of .001 is %4.4f',val)
clear

%Problem 3 - Equipartition the path of Figure 5.6 into n subpaths of equal
%length.
%n = 4
%get values for t
f = @(x)((0.3+7.8*x-14.1*x^2)^2+(0.3+1.8*x-8.1*x^2)^2)^(1/2);

%Initialize graph
s=0:.01:1;
X=0.5+0.3*s+3.9*s.^2-4.7*s.^3;
Y=1.5+0.3*s+0.9*s.^2-2.7*s.^3;
plot(X,Y)
hold all
t=zeros(5,1);
%Getvalues for t
for i=0:4
t(i+1)=bisarclen(i*.25,.001,f);
end
x=0.5+0.3*t+3.9*t.^2-4.7*t.^3;
y=1.5+0.3*t+0.9*t.^2-2.7*t.^3;
%Plot!
plot(x,y,'+')
clear

%n = 20
f = @(x)((0.3+7.8*x-14.1*x^2)^2+(0.3+1.8*x-8.1*x^2)^2)^(1/2);

%Initialize graph
s=0:.01:1;
X=0.5+0.3*s+3.9*s.^2-4.7*s.^3;
Y=1.5+0.3*s+0.9*s.^2-2.7*s.^3;
plot(X,Y)
hold all
t=zeros(20,1);
%Get values for T
for i=0:19
t(i+1)=bisarclen(i*.05,.001,f);
end
%Graph them
x=0.5+0.3*t+3.9*t.^2-4.7*t.^3;
y=1.5+0.3*t+0.9*t.^2-2.7*t.^3;
plot(x,y,'+')

clear

%Problem 4 - Replace the Bisection Method in Step 2 with Newton's Method
%and repeat steps 2 and 3.
%Step 2
val = newtarclen(.5,.001);
sprintf('Time Position at 50 percent of the curve length with a tolerance of .001 is %4.4f',val)
%Step 3
%n = 4
%Initialize graph
s=0:.01:1;
X=0.5+0.3*s+3.9*s.^2-4.7*s.^3;
Y=1.5+0.3*s+0.9*s.^2-2.7*s.^3;
plot(X,Y)
hold all
t=zeros(5,1);
%Getvalues for t
for i=0:4
t(i+1)=newtarclen(i*.25,.001);
end
x=0.5+0.3*t+3.9*t.^2-4.7*t.^3;
y=1.5+0.3*t+0.9*t.^2-2.7*t.^3;
%Plot!
plot(x,y,'+')
clear

%n = 20
f = @(x)((0.3+7.8*x-14.1*x^2)^2+(0.3+1.8*x-8.1*x^2)^2)^(1/2);

%Initialize graph
s=0:.01:1;
X=0.5+0.3*s+3.9*s.^2-4.7*s.^3;
Y=1.5+0.3*s+0.9*s.^2-2.7*s.^3;
plot(X,Y)
hold all
t=zeros(20,1);
%Get values for T
for i=0:19
t(i+1)=newtarclen(i*.05,.001);
end
%Graph them
x=0.5+0.3*t+3.9*t.^2-4.7*t.^3;
y=1.5+0.3*t+0.9*t.^2-2.7*t.^3;
plot(x,y,'+')
clear;
%Problem 5 - Use Matlab's animation commands to demonstrate traveling along
%the path. 
clf
%Setup path
s=0:.01:1;
X=0.5+0.3*s+3.9*s.^2-4.7*s.^3;
Y=1.5+0.3*s+0.9*s.^2-2.7*s.^3;
plot(X,Y)
hold all

%Setup drawing
set(gca,'XLim',[0 2],'YLim',[0 2], 'Drawmode', 'fast', 'visible', 'on');
axis square
ball=line('color','r','Marker','o','MarkerSize',10,'LineWidth',2,'erase','xor','xdata',[],'ydata',[]);
ball2=line('color','b','Marker','o','MarkerSize',10,'LineWidth',2,'erase','xor','xdata',[],'ydata',[]);

%Original Parameter
t=.00:.05:20;

t2 = zeros(20,1);
%Constant Speed
for i=0:20
t2(i+1)=newtarclen(i*.05,.001);
end


a=0.5+0.3*t+3.9*t.^2-4.7*t.^3;
b=1.5+0.3*t+0.9*t.^2-2.7*t.^3;

c=0.5+0.3*t2+3.9*t2.^2-4.7*t2.^3;
d=1.5+0.3*t2+0.9*t2.^2-2.7*t2.^3;


filename = 'Problem5.gif';
for i = 1:21
    set(ball,'xdata',a(i),'ydata',b(i));
    set(ball2,'xdata',c(i),'ydata',d(i));
    drawnow;
    pause(.1);
    frame = getframe(1);
    im = frame2im(frame);
    [imind,cm] = rgb2ind(im,256);
    if i == 1;
    imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
    else
    imwrite(imind,cm,filename,'gif','WriteMode','append');
    end
end
clf
%Problem 6 - Pick 4 points, create a Bezier spline and equipartition it into n segments.

%Problem 7 - With that spline, create three pictures 
% 1 - Constant Speed
% 2 - s^(1/3)
% 3 - s^2

