function varargout = OrbitFeatures(varargin)
% ORBITFEATURES M-file for OrbitFeatures.fig
%      ORBITFEATURES, by itself, creates a new ORBITFEATURES or raises the existing
%      singleton*.
%
%      H = ORBITFEATURES returns the handle to a new ORBITFEATURES or the handle to
%      the existing singleton*.
%
%      ORBITFEATURES('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ORBITFEATURES.M with the given input arguments.
%
%      ORBITFEATURES('Property','Value',...) creates a new ORBITFEATURES or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before OrbitFeatures_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to OrbitFeatures_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 OrbitFeatures

% Last Modified by GUIDE v2.5 30-Oct-2012 10:49:29

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @OrbitFeatures_OpeningFcn, ...
                   'gui_OutputFcn',  @OrbitFeatures_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 OrbitFeatures is made visible.
function OrbitFeatures_OpeningFcn(hObject, eventdata, handles, varargin)

% Choose default command line output for OrbitFeatures
handles.output = hObject;

%% Initialize the three sliders...
P = 5; A = 8; phi = 30;
handles.P = P;
set(handles.P_slider,'Min',1,'Max',A,'Value',handles.P)
set(handles.P_edit,'String',sprintf('%5.1f',handles.P))

handles.A = A;
set(handles.A_slider,'Min',P,'Max',10,'Value',handles.A)
set(handles.A_edit,'String',sprintf('%5.1f',handles.A))

handles.phi = phi;
set(handles.phi_Slider,'Min',0,'Max',360,'Value',handles.phi)
set(handles.phi_edit,'String',sprintf('%5.1f',handles.phi))

%% Initialize the four check boxes...
set(handles.AxesCheck,'Value',1)
set(handles.PerihelionCheck,'Value',1)
set(handles.AphelionCheck,'Value',1)
set(handles.PhantomCheck,'Value',1)

% Display..
PlotOrbit(handles)

% Update handles structure
guidata(hObject, handles);

% --- Outputs from this function are returned to the command line.
function varargout = OrbitFeatures_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 slider movement.
function P_slider_Callback(hObject, eventdata, handles)
% Update the displayed P value and adjust the min value for A_slider so
% that A can never be set to a value less than P.
handles.P = get(handles.P_slider,'Value');
set(handles.P_edit,'String',sprintf('%5.1f',handles.P))
set(handles.A_slider,'Min',handles.P)
PlotOrbit(handles)
% Update handles structure
guidata(hObject, handles);

% --- Executes on slider movement.
function A_slider_Callback(hObject, eventdata, handles)
% Update the displayed A value and adjust the max value for P_slider so
% that P can never be set to a value greater than A.
handles.A = get(handles.A_slider,'Value');
set(handles.A_edit,'String',sprintf('%5.1f',handles.A))
set(handles.P_slider,'Max',handles.A)
PlotOrbit(handles)
% Update handles structure
guidata(hObject, handles);

% --- Executes on slider movement.
function phi_Slider_Callback(hObject, eventdata, handles)
% Update the displayed value of phi
handles.phi = get(hObject,'Value');
set(handles.phi_edit,'String',sprintf('%5.0f',handles.phi))
PlotOrbit(handles)
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in AxesCheck.
function AxesCheck_Callback(hObject, eventdata, handles)
% Re-display to reflect the change
PlotOrbit(handles)
guidata(hObject, handles);

% --- Executes on button press in PerihelionCheck.
function PerihelionCheck_Callback(hObject, eventdata, handles)
% Redisplay to reflect the change
PlotOrbit(handles)
guidata(hObject, handles);

% --- Executes on button press in AphelionCheck.
function AphelionCheck_Callback(hObject, eventdata, handles)
% Re-display to reflect the change
PlotOrbit(handles)
guidata(hObject, handles);

% --- Executes on button press in PhantomCheck.
function PhantomCheck_Callback(hObject, eventdata, handles)
% Redisplay to reflect the change
PlotOrbit(handles)
guidata(hObject, handles);

function P_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to P_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function A_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to A_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function phi_Slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to phi_Slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function P_edit_Callback(hObject, eventdata, handles)
function P_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to P_edit (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
function A_edit_Callback(hObject, eventdata, handles)
function A_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to A_edit (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
function phi_edit_Callback(hObject, eventdata, handles)
function phi_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to phi_edit (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


function PlotOrbit(handles)
% Display the orbit and selected features.
% Get P, A, and phi...
P = handles.P;
A = handles.A;
phi = handles.phi;
% Set up the black background...
axis([-10 10 -10 10])
fill([10 10 -10 -10],[-10 10 10 -10],'k')
axis equal off
% Display the orbit itself...
theta = linspace(0,2*pi);
x1 = (P-A)/2 + ((P+A)/2)*cos(theta);
y1 = sqrt(P*A)*sin(theta);
c = cos(phi*pi/180);
s = sin(phi*pi/180);
x = c*x1 - s*y1;
y = s*x1 + c*y1;
hold on
plot(x,y,'c')
% Display the Sun...
plot(0,0,'.y','Markersize',30)

% Display perihelion if the perihelion box is checked...
if get(handles.PerihelionCheck,'Value')
    plot(c*P,s*P,'.m','Markersize',30)
end
% Display aphelion if the aphelion box is checked...
if get(handles.AphelionCheck,'Value')
    plot(-c*A,-s*A,'.m','Markersize',30)
end
% Display the phantom focus if the phantom focus box is checked...
if get(handles.PhantomCheck,'Value')
    plot(c*(P-A),s*(P-A),'oy','Markersize',10)
end
% Display the axes if the axes box is checked...
if  get(handles.AxesCheck,'Value')
    xMajor = linspace(-A,P);
    plot(c*xMajor,s*xMajor,':m')
    b = sqrt(P*A);
    yMinor = linspace(-b,b);
    xMinor = ((-A+P)/2)*ones(1,100);
    plot(c*xMinor-s*yMinor,c*yMinor +s*xMinor,':m')
end
hold off
