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

% Last Modified by GUIDE v2.5 22-Aug-2012 14:01:59

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

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

set(handles.slider1,'Min',1)
set(handles.slider1,'Max',360)
set(handles.slider1,'Value',65)
set(handles.edit1,'String','65')

set(handles.slider2,'Min',100)
set(handles.slider2,'Max',1000)
set(handles.slider2,'Value',400)
set(handles.edit2,'String','400')

MakeSpiral(65,400,'red','blue')
% Update handles structure
guidata(hObject, handles);

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

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

turnAngle = get(hObject,'Value');
set(handles.edit1,'String',sprintf('%3.0f',turnAngle))
numEdges = get(handles.slider2,'Value');
MakeSpiral(turnAngle,numEdges,'yellow','red')

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

% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider2 (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
numEdges = get(hObject,'Value');
set(handles.edit2,'String',sprintf('%3.0f',numEdges))
turnAngle = get(handles.slider1,'Value');
MakeSpiral(turnAngle,numEdges,'yellow','red')

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

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

% --- 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 edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double

% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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 MakeSpiral(turnAngle,numEdges,spiralColor1,spiralColor2)

plot(0,0)
axis equal off

% Color is specified by a 3-element vector, each component between 0 and 1.
% The numbers specify the red, green, and blue content respectively.

set(gcf,'color','black')
hold on

% Spiral starts at (0,0)
x_c= 0;
y_c= 0;

for k= 1:numEdges

% Direction of the k-th edge
theta= (k*turnAngle)*(pi/180);

% k-th edge starts at (x_c,y_c) and ends at (x_next,y_next)
x_next= x_c + k*cos(theta);
y_next= y_c + k*sin(theta);

% plot the k-th edge
if rem(k,2)==1
plot([x_c x_next],[y_c y_next],'color',spiralColor1)
else
plot([x_c x_next],[y_c y_next],'color',spiralColor2)
end

% The "start" of the next leg is the "end" of the current leg
x_c= x_next;
y_c= y_next;

end

hold off

% --- Executes when selected object is changed in uipanel1.

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

turnAngle = get(handles.slider1,'Value');
numEdges = get(handles.slider2,'Value');
MakeSpiral(turnAngle,numEdges,'red','blue')

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
turnAngle = get(handles.slider1,'Value');
numEdges = get(handles.slider2,'Value');
MakeSpiral(turnAngle,numEdges,'cyan','magenta')

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

turnAngle = get(handles.slider1,'Value');
numEdges = get(handles.slider2,'Value');
MakeSpiral(turnAngle,numEdges,'green','yellow')