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

% Last Modified by GUIDE v2.5 16-Sep-2013 13:01:40

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

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

%%%%%%%%%%%%%5
% Initialize the "n" slider which is slider1...
% The range...
set(handles.slider1,'Min',6);
set(handles.slider1,'Max',100);
% The step...
x = 1/47; % Want slider step to be (100-6)/2 sothat only even values assumed.
y = 0.1; % Want the slide to be one-tenth the with of the slider slot.
set(handles.slider1,'SliderStep',[x y]);
% The initial value...
n = 24;
set(handles.slider1,'Value',n);
% Report the slider value in editbox edit1...
set(handles.edit1,'String',sprintf('%3d',n));

% Initialize the popupmenu that manages the disk color and establish
% a handles variable that houses its value...
set(handles.popupmenu1,'Value',1);
handles.diskColor = 'r';

% Initialize the slider that manages r....
% ?????

% Initialize the popup menu that manages the back petal color and establish
% a handles variable that houses its value...
% ?????

% Initialize the popup menu that manages the front petal color and establish
% a handles variable that houses its value...

% Draw the initial flower.
DrawFlowerGUI(.5,n,'m','y',handles.diskColor)

% Update handles structure
guidata(hObject, handles);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

% --- Outputs from this function are returned to the command line.
function varargout = FlowerGUI_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 slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (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

% Obtain the selected n-value...
x = get(hObject,'Value');
% In case the value is not an integer, round to nearest even...
n = 2*floor(x/2);
set(hObject,'Value',n);
% Display the value...
set(handles.edit1,'String',sprintf('%3d',n));
% Acquire the value for r and the two petal colors...
% ????
% Draw the flower...
DrawFlowerGUI(.5,n,'r','y',handles.diskColor)

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

% --- 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

function DrawFlowerGUI(r,n,backPetalColor,frontPetalColor, diskColor)
% In the current figure window this function draws a unit radius flower
% with n petals fiwhere n is an even integer >= 4.
% The inner radius is specied by r and 0<r<1.
% The back petal color, front petal color, and inner disk color are
% specified by backPetalColor, frontPetalColor, and diskColor. Valid
% colors are 'k', 'y', 'c', 'm', 'r', 'b', 'w', 'g'.

plot(0,0)
axis equal off
hold on
theta = linspace(2*pi/n,2*pi,n);
xP = r*cos(theta);
yP = r*sin(theta);
xQ = cos(theta);
yQ = sin(theta);
% Draw the back petals...
for k=1:2:n
% Determine the xy coordinates of the kth petal...
if k==1
xPetal = [xQ(1) xP(n) xP(2)];
yPetal = [yQ(1) yP(n) yP(2)];
else
xPetal = [xQ(k) xP(k-1) xP(k+1)];
yPetal = [yQ(k) yP(k-1) yP(k+1)];
end
fill(xPetal,yPetal,backPetalColor)
end
% Draw the front petals...
for k=2:2:n
% Determine the xy coordinates of the kth petal...
if k==n
xPetal = [xQ(n) xP(n-1) xP(1)];
yPetal = [yQ(n) yP(n-1) yP(1)];
else
xPetal = [xQ(k) xP(k-1) xP(k+1)];
yPetal = [yQ(k) yP(k-1) yP(k+1)];
end
fill(xPetal,yPetal,frontPetalColor)
end
DrawDisk(0,0,r,diskColor)
hold off

function DrawDisk(xc,yc,r,c)
% Adds a circular disk to the current window.
% Assumes hold is on.
% The disk has radius r, center(xc,yc) and
% color c where c is one of 'r', 'g', 'y', etc.

theta = linspace(0,2*pi);
cosVals = cos(theta);
sinVals = sin(theta);
fill(xc+r*cosVals,yc+r*sinVals,c);

% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1

% Obtain the selected color and save..
diskColorMenu = cellstr(get(hObject,'String'));
handles.diskColor = diskColorMenu{get(hObject,'Value')};
% Save the handles update...
guidata(hObject, handles);
% Obtain the value of n...
n = get(handles.slider1,'Value');
% Acquire the value for r and the two petal colors...
% ????
% Draw the flower...
DrawFlowerGUI(.5,n,'r','y',handles.diskColor)

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

% Hint: popupmenu 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