%This function calculates the lateral error from the track of your car.  It
%accepts the position of the car in meters and returns the error also in
%meters.  Valid inputs for x are between 0 and 8 (when your car reaches 
%this point you may stop).  y can be any value.  The error is positive if
%you are to the right of the track and negative if you are to the left.  If
%your car is more than .5 meters from the track, this error function will not
%work well (that is much farther than your sensors can read).  The
%direction the car should travel is along the positive x-axis.

function e = distance(x,y)

%x = 2.9144;
%y = -1.8288;

if x < 0 %Invalid Input
    e = 100;
    
elseif (x >= 0 && x <= 2)
    e = -y;
    
elseif (x > 2 && x < 3.8288 && y > -.9144)
    e = -(sqrt((x-2)^2+(y+.9144)^2)-.9144);
    
elseif (x > 2 && x < 3.8288 && y <= -.9144)
    e = sqrt((x-3.8288)^2+(y+.9144)^2)-.9144;
    
elseif (x >= 3.8288)
    e = -(y + 1.8288);
    
elseif (x >= 5.8288)
    e = -(y + 1.6764);
    
end


























