function varargout = Catapult(varargin)
% CATAPULT MATLAB code for Catapult.fig
%      CATAPULT, by itself, creates a new CATAPULT or raises the existing
%      singleton*.
%
%      H = CATAPULT returns the handle to a new CATAPULT or the handle to
%      the existing singleton*.
%
%      CATAPULT('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CATAPULT.M with the given input arguments.
%
%      CATAPULT('Property','Value',...) creates a new CATAPULT or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Catapult_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Catapult_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Catapult

% Last Modified by GUIDE v2.5 29-Jan-2014 13:48:29

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Catapult_OpeningFcn, ...
                   'gui_OutputFcn',  @Catapult_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Catapult is made visible.
function Catapult_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Catapult (see VARARGIN)
global b
b=30;
plot(0,linspace(0,15,100),'.k',500,linspace(0,15,100),'.k');
axis([-100,500,0,165]); 
title('Trajectory of Heated Rock')
set(gca,'XTickLabel','')
set(gca,'YTickLabel','')
% Choose default command line output for Catapult
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Catapult wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Catapult_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global b
if b>=0 && b<=90
r=@(b,t) [-100+80*cosd(b).*t; 80*sind(b).*t-4.9*t.^2];
t0=80*sind(b)/4.9;
r=@(b,t) [-100+80*cosd(b).*t; 80*sind(b).*t-4.9*t.^2];
if abs(r(b,5/4/cosd(b)))<15
    tend=5/4/cosd(b);
else
    tend=t0;
end
t=linspace(0,tend,1000);
traj=r(b,t);
plot(traj(1,:),traj(2,:),0,linspace(0,15,100),'.k',500,linspace(0,15,100),'.k');
axis([-100,500,0,165]); 
title('Trajectory of Heated Rock')
set(gca,'XTickLabel','')
set(gca,'YTickLabel','')
axis([-100,500,0,165]); %max(traj(2,:))+10
t0=(5/4).*secd(b);
t500=(15/2).*secd(b);
r0=r(b,t0);
r500=r(b,t500);
    if r0(2,1)>15 && r500(2,1)<=15
        set(handles.text5,'String','Hit!')
    else 
        set(handles.text5,'String','Miss!')
    end
    if b<4.40403 || b>85.59596
        set(handles.text6,'String','The rock hit the ground in front of the wall.')
    elseif b >= 4.40403 && r0(2,1)<=15
        set(handles.text6,'String','The rock hit the wall.')
    elseif r0(2,1)>15 && r500(2,1)<0
        set(handles.text6,'String','The rock hit the ground within the city.')
    elseif r500(2,1)>=0 && r500(2,1)<=15
        set(handles.text6,'String','The rock hit the back wall.')
    elseif r500(2,1)>15
        set(handles.text6,'String','The rock flew over the city.')
    end

else
   h=msgbox('Please enter an angle between 0 and 90 degrees.', 'Warning');
end



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global b
b=str2num(get(handles.edit1,'String'));


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
