function varargout = Julia_Sets(varargin)
% JULIA_SETS M-file for Julia_Sets.fig
%      JULIA_SETS, by itself, creates a new JULIA_SETS or raises the existing
%      singleton*.
%
%      H = JULIA_SETS returns the handle to a new JULIA_SETS or the handle to
%      the existing singleton*.
%
% How to use the GUI:
%    - The sliders at the bottom allow you to change the real and
%      imaginary parts of the parameter <i>c</i>.
%    - The "Iterations" slider allows you to set the number of
%      successive iterations used in deciding whether points belong
%      to the Julia set.
%    - The "Resolution" panel controls the number of points actually
%      plotted in the picture. You have to keep in mind that a
%      higher resolution will lead to a nicer picture, but the code
%      will be slower.
%    - You can zoom in on any part of the picture by clicking and
%      dragging the mouse to define a rectangular region to be
%      magnified. The code keeps track of successive magnifications.
%    - You can then zoom out by repeatedly clicking on the "Back"
%      button. The "Reset" button takes you back to the default
%      view.
%    
% Developed by Joceline Lega, Department of Mathematics, University
% of Arizona, Spring 2008.
% 

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

% Last Modified by GUIDE v2.5 16-Sep-2007 12:45:25

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @Julia_Sets_OpeningFcn, ...
    'gui_OutputFcn',  @Julia_Sets_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 Julia_Sets is made visible.
function Julia_Sets_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 Julia_Sets (see VARARGIN)
global xmin xmax ymin ymax npts
global boxbdry

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Julia_Sets wait for user response (see UIRESUME)
% uiwait(handles.figure1);
set(handles.figure1,'Name',' Julia Sets');
set(handles.panel_c,'Title',...
    [' c = ' num2str(get(handles.slider_c,'Value')) ' + ' num2str(get(handles.slider_ci,'Value')) ' i ']);
set(handles.panel_jmax,'Title',...
    [' Iterations: ' num2str(get(handles.slider_jmax,'Value'))]);
xmin=-1.5;
xmax=1.5;
ymin=-1.5;
ymax=1.5;
boxbdry=[xmin xmax ymin ymax];

if get(handles.radio_high,'Value') == 1
    npts = 200;
elseif get(handles.radio_medium,'Value') == 1
    npts = 150;
else
    npts = 100;
end
plot_fractal(hObject, eventdata, handles);

% --- Outputs from this function are returned to the command line.
function varargout = Julia_Sets_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 slider_c_Callback(hObject, eventdata, handles)
% hObject    handle to slider_c (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
set(handles.panel_c,'Title',...
    [' c = ' num2str(get(handles.slider_c,'Value')) ' + ' num2str(get(handles.slider_ci,'Value')) ' i ']);
plot_fractal(hObject, eventdata, handles);

% --- Executes during object creation, after setting all properties.
function slider_c_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_c (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 slider_ci_Callback(hObject, eventdata, handles)
% hObject    handle to slider_ci (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
set(handles.panel_c,'Title',...
    [' c = ' num2str(get(handles.slider_c,'Value')) ' + ' num2str(get(handles.slider_ci,'Value')) ' i ']);
plot_fractal(hObject, eventdata, handles);


% --- Executes during object creation, after setting all properties.
function slider_ci_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_ci (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 plot_fractal(hObject, eventdata, handles)
global xmin xmax ymin ymax npts

axes(handles.axes1)
x=xmin:(xmax-xmin)/npts:xmax;
y=ymin:(ymax-ymin)/npts:ymax;;
[X,Y]=meshgrid(x,y);
jmax = get(handles.slider_jmax,'Value');
c = get(handles.slider_c,'Value') + get(handles.slider_ci,'Value')*i;
Z = X + i*Y;
TF=max(sign(5-abs(Z)),zeros(size(Z)));   % TF=1 -> good; TF = 0 -> diverge
TF1=max(sign(abs(Z)-5),zeros(size(Z)));
for j=1:jmax
    Z = Z.^2 + c;
    TF1=TF1+max(sign(abs(Z)-5),zeros(size(Z)))*(jmax-j);
    TF=max(sign(5-abs(Z)),zeros(size(Z)));
    Z=Z.*TF;
end
pcolor(X,Y,TF1),shading interp
axis square
axis off

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

% Hint: get(hObject,'Value') returns toggle state of radio_high
set(handles.radio_high,'Value',1);
set(handles.radio_medium,'Value',0);
set(handles.radio_low,'Value',0);
if npts ~= 200
    npts = 200;
    plot_fractal(hObject, eventdata, handles);
end

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

% Hint: get(hObject,'Value') returns toggle state of radio_medium
set(handles.radio_high,'Value',0);
set(handles.radio_medium,'Value',1);
set(handles.radio_low,'Value',0);
if npts ~= 150
    npts = 150;
    plot_fractal(hObject, eventdata, handles);
end

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

% Hint: get(hObject,'Value') returns toggle state of radio_low
set(handles.radio_high,'Value',0);
set(handles.radio_medium,'Value',0);
set(handles.radio_low,'Value',1);
if npts ~= 100
    npts = 100;
    plot_fractal(hObject, eventdata, handles);
end




% --- Executes on slider movement.
function slider_jmax_Callback(hObject, eventdata, handles)
% hObject    handle to slider_jmax (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
set(handles.slider_jmax,'Value',...
    round(get(handles.slider_jmax,'Value')));
set(handles.panel_jmax,'Title',...
    [' Iterations: ' num2str(get(handles.slider_jmax,'Value'))]);
plot_fractal(hObject, eventdata, handles);

% --- Executes during object creation, after setting all properties.
function slider_jmax_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_jmax (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 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 xmin xmax ymin ymax npts
global boxbdry

xmin=-1.5;
xmax=1.5;
ymin=-1.5;
ymax=1.5;
boxbdry=[boxbdry; xmin xmax ymin ymax];
plot_fractal(hObject, eventdata, handles);


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

global xmin xmax ymin ymax npts
global boxbdry

n=size(boxbdry,1);
if n > 1
    boxbdry(n,:)=[];
    n=n-1;
end
xmin=boxbdry(n,1);
xmax=boxbdry(n,2);
ymin=boxbdry(n,3);
ymax=boxbdry(n,4);
plot_fractal(hObject, eventdata, handles);


function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global xmin xmax ymin ymax npts
global boxbdry

if get(get(hObject,'CurrentObject'),'Parent') == handles.axes1
    
    point1 = get(gca,'CurrentPoint');    % button down detected
    finalRect = rbbox;                   % return figure units
    point2 = get(gca,'CurrentPoint');
    if finalRect(3) ~= 0
        xmin=min(point1(1,1),point2(1,1));
        xmax=max(point1(1,1),point2(1,1));
        ymin=min(point1(1,2),point2(1,2));
        ymax=max(point1(1,2),point2(1,2));
        lgth=max(xmax-xmin,ymax-ymin);
        xmax=xmin+lgth;
        ymax=ymin+lgth;
        if boxbdry(size(boxbdry,1),1) ~= xmin
            boxbdry=[boxbdry; [xmin xmax ymin ymax]];
        end
        plot_fractal(hObject, eventdata, handles)
    end
end
