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

% Last Modified by GUIDE v2.5 15-Oct-2012 16:02:39

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @MinEllipse_OpeningFcn, ...
'gui_OutputFcn', @MinEllipse_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 MinEllipse is made visible.
function MinEllipse_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 MinEllipse (see VARARGIN)

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

%%% THE GUI HAS THREE SLIDERS. DURING LAYOUT I GAVE THEM ILLUMINATING
%%% TAGS BY USING THE PROPERTY EDITOR. I DID THIS BEFORE SAVING THE EDITS.
%%% OTHERWISE WE WOULD HAVE, FOR EXAMPLE "slider1" INSTEAD OF "a_slider"

%%% PAIRED WITH THESE THREE SLIDERS ARE THREE EDIT TEXT COMPONENTS THAT ARE
%%% USED TO DISPLAY SLIDER VALUES.

%%% HERE IN THE OPENING FUNCTION WE INITIALIZE THEIR VALUES.
%%% TYPICALLY WE SET THE RANGE OF A SLIDER IN THE OPENING FUNCTION.

%%% INITIALIZE a_slider AND a_text..
a = 5;
set(handles.a_slider,'Min',0,'Max',5,'Value',a)
set(handles.a_text,'String',sprintf('%4.2f',a))

%%% INITIALIZE b_slider AND b_text...
b = 5;
set(handles.b_slider,'Min',0,'Max',5,'Value',b)
set(handles.b_text,'String',sprintf('%4.2f',b))

%%% INITIALIZE tilt_slider AND tilt_text..
tilt = 0;
set(handles.tilt_slider,'Min',0,'Max',360,'Value',tilt)
x = 1/360; % Want slider to step in 1-degree units
y = 0.1; % Want the slide to be one-tenth the width of the slider slot.
set(handles.tilt_slider,'SliderStep',[x y])
set(handles.tilt_text,'String',sprintf('%3d',tilt))

%%% AT ANY INSTANT, THE SLIDER VALUES COLLECTIVELY SPECIFY THE ELLIPSE.
%%% THAT IS, THE VALUE OF a_slider IS a, THE VALUE OF b_slider IS b, and
%%% THE VALUE OF tilt_slider is tilt.

%%% AN ADDITIONAL EDIT TEXT COMPONENT IS USED TO DISPLAY THE AREA OF THE
%%% CURRENT ELLIPSE USING THE FORMULA AREA = pi*a*b.
set(handles.area_text,'String',sprintf('%6.2f',pi*a*b))

%%% ALSO DISPLAYED ARE FOUR RANDOM POINTS. THEIR COORDINATES ARE HOUSED
%%% IN A PAIR OF VECTORS THAT WE SETUP AS "GUIDATA" VIA THE handles
%%% STRUCTURE. WITH THIS DEVICE WE CAN ACCESS THIS INFORMATION FROM ANY
%%% COMPONENT
handles.u = 3*[rand(1) -rand(1) -rand(1) rand(1)];
handles.v = 3*[rand(1) rand(1) -rand(1) -rand(1)];

%%% WE WROTE A FUNCTION CALLED "Display" THAT CAN BE USED TO DISPLAY THE
%%% CURRENT ELLIPSE IN FIGURE WINDOQ TOGETHER WITH THE FOUR RANDOM POINTS.
%% WE ARE STILL IN THE OPENIONG FUNCTION SO THE RESULT IS TO DISPLAY THE
%%% INITIAL ELLIPSE WHICH IS A RADIUS 5 CIRCLE

Display(a,b,tilt,handles.u,handles.v)

%%% IT IS IMPORTANT THAT OUR ADDITIONS TO THE OPENING FUNCTION CONCLUDE
%%% WITH THE FOLLOWING COMMAND...

% Update handles structure
guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = MinEllipse_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;

%%% NOW FOLLOWS THE CALLBACK FOR a_slider. WE PROVIDE THE ENTIRE BODY
%%% OF THIS FUNCTION. GUIDE PROVIDED THE HEADER AND ATTENDING COMMENTS

% --- Executes on slider movement.
function a_slider_Callback(hObject, eventdata, handles)
% hObject handle to a_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of
% slider

%%% GET HOLD OF THE THREE ELLIPSE PARAMETERS, DISPLAY THE UPDATED ELLIPSE,
%%% REPORT THE NEW VALUE of a_slider IN a_text, AND THE VALUE OF THE NEW
%%% AREA in area_text..

a = get(handles.a_slider,'Value');
b = get(handles.b_slider,'Value');
tilt = get(handles.tilt_slider,'Value');
Display(a,b,tilt,handles.u,handles.v)
set(handles.a_text,'String',sprintf('%4.2f',a))
set(handles.area_text,'String',sprintf('%6.2f',pi*a*b))

%%% IGNORE THIS...
% --- Executes during object creation, after setting all properties.
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

%%% NOW FOLLOWS THE CALLBACK FOR b_slider. WE PROVIDE THE ENTIRE BODY
%%% OF THIS FUNCTION. GUIDE PROVIDED THE HEADER AND ATTENDING COMMENTS

% --- Executes on slider movement.
function b_slider_Callback(hObject, eventdata, handles)
% hObject handle to b_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider

%%% GET HOLD OF THE THREE ELLIPSE PARAMETERS, DISPLAY THE UPDATED ELLIPSE,
%%% REPORT THE NEW VALUE of b_slider IN b_text, AND THE VALUE OF THE NEW
%%% AREA in area_text..

a = get(handles.a_slider,'Value');
b = get(handles.b_slider,'Value');
tilt = get(handles.tilt_slider,'Value');
Display(a,b,tilt,handles.u,handles.v)
set(handles.a_text,'String',sprintf('%4.2f',a))
set(handles.b_text,'String',sprintf('%4.2f',b))
set(handles.area_text,'String',sprintf('%6.2f',pi*a*b))

%%% IGNORE THIS...
% --- Executes during object creation, after setting all properties.
function b_slider_CreateFcn(hObject, eventdata, handles)
% hObject handle to b_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

%%% NOW FOLLOWS THE CALLBACK FOR tilt_slider. WE PROVIDE THE ENTIRE BODY
%%% OF THIS FUNCTION. GUIDE PROVIDED THE HEADER AND ATTENDING COMMENTS

% --- Executes on slider movement.
function tilt_slider_Callback(hObject, eventdata, handles)
% hObject handle to tilt_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider

%%% GET HOLD OF THE THREE ELLIPSE PARAMETERS, DISPLAY THE UPDATED ELLIPSE,
%%% REPORT THE NEW VALUE of tilt_slider IN tilt_text, AND THE VALUE OF THE NEW
%%% AREA in area_text..

a = get(handles.a_slider,'Value');
b = get(handles.b_slider,'Value');
tilt = get(handles.tilt_slider,'Value');
Display(a,b,tilt,handles.u,handles.v)
set(handles.tilt_text,'String',sprintf('%3.0f',tilt))
set(handles.area_text,'String',sprintf('%6.2f',pi*a*b))

%%% IGNORE THIS...
% --- Executes during object creation, after setting all properties.
function tilt_slider_CreateFcn(hObject, eventdata, handles)
% hObject handle to tilt_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

%%% THE GUI HAS A PUSH BUTTON THAT CAN BE USED TO GENERATE A NEW
%%% "ENCLOSE-THE-DATA" PROBLEM. HERE IS THE CALLBACK..

% --- Executes on button press in new_button.
function new_button_Callback(hObject, eventdata, handles)
% hObject handle to new_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

%%% GENERATE THE RANDOM POINTS. NOTE THAT AS GUIDATA, THE u AND v VECTORS
%%% ARE REFERENCED THROOOUGH THE handles STRUCTURE..
handles.u = 3*[rand(1) -rand(1) -rand(1) rand(1)];
handles.v = 3*[rand(1) rand(1) -rand(1) -rand(1)];

%%% SET UP THE INITIAL ELLIPSE WHICH WE TAKE TO BE THE CIRCLE WITH RADIUS
%%% 5. THIS INVOLVES SETTING THE THREE SLIDER VALUES AND REPORTING THE
%%% RESULTS IN THE CORRESPONDING EDIT-TEXT COMPONENTS...

set(handles.a_slider,'Value',5);
set(handles.a_text,'String',sprintf('%4.2f',5))
set(handles.b_slider,'Value',5);
set(handles.b_text,'String',sprintf('%4.2f',5))
set(handles.tilt_slider,'Value',0);
set(handles.tilt_text,'String',sprintf('%1d',0))

%%% SINCE WE ALTERED THE GUIDATA, IT MUST BE SAVED FOR FUTURE USE..
guidata(hObject, handles);

%%% DISPLAY THE ELLIPSE AND THE RANDOM POINTS...
Display(5,5,0,handles.u,handles.v)

%%% IGNORE ALL THESE FUNCTIONS ASSOCIATED WITH THE TEXT EDIT COMPONENTS...
function a_text_Callback(hObject, eventdata, handles)
% hObject handle to a_text (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of a_text as text
% str2double(get(hObject,'String')) returns contents of a_text as a double

% --- Executes during object creation, after setting all properties.
function a_text_CreateFcn(hObject, eventdata, handles)
% hObject handle to a_text (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 b_text_Callback(hObject, eventdata, handles)
% hObject handle to b_text (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of b_text as text
% str2double(get(hObject,'String')) returns contents of b_text as a double

% --- Executes during object creation, after setting all properties.
function b_text_CreateFcn(hObject, eventdata, handles)
% hObject handle to b_text (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 tilt_text_Callback(hObject, eventdata, handles)
% hObject handle to tilt_text (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of tilt_text as text
% str2double(get(hObject,'String')) returns contents of tilt_text as a double

% --- Executes during object creation, after setting all properties.
function tilt_text_CreateFcn(hObject, eventdata, handles)
% hObject handle to tilt_text (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 area_text_Callback(hObject, eventdata, handles)
% hObject handle to area_text (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of area_text as text
% str2double(get(hObject,'String')) returns contents of area_text as a double

% --- Executes during object creation, after setting all properties.
function area_text_CreateFcn(hObject, eventdata, handles)
% hObject handle to area_text (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

%%% ABSOLUTELY FINE TO INCLUDE SUBFUNCTIONS THAT ARE USEFUL TO THE
%%% IMPLEMENTATION OF THE GUI...

function Display(a,b,tilt,u,v)
% Displays a tilted version of (x/a)^2 + (y/b)^2 = 1 against
% a black background. For best results, assume a<=5 and b<=5.
% The random points defined by the length-4 vectors u and v are
% also displayed.

fill([-6 6 6 -6],[-6 -6 6 6],'k')
axis([-6 6 -6 6])
theta = linspace(0,2*pi);
x0 = a*cos(theta);
y0 = b*sin(theta);
c = cos(tilt*pi/180);
s = sin(tilt*pi/180);
x = c*x0 + s*y0;
y = -s*x0 + c*y0;
hold on
plot(u,v,'.','Markersize',20,'color','c')
plot(x,y,'linewidth',2,'color','y')
plot([-6 6],[0 0],'r',[0 0],[-6 6],'r')
hold off