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

% Last Modified by GUIDE v2.5 20-Aug-2013 16:25:34

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

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

% Update handles structure
guidata(hObject, handles);

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


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



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


% --- 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)
start_data=[100,100;198.480775301221,117.364817766693;134.202014332567,193.969262078591;100,100];
N=length(start_data);
iter=str2num(get(handles.edit1,'String'));
NNN=0;

for j=1:iter
    for i=1:N-1
        A=start_data(i,:);
        B=start_data(i+1,:);
        l=sqrt((A(1)-B(1))^2 + (A(2)-B(2))^2);
        NNN=NNN+1;
        end_data(NNN,:)=A;
        NNN=NNN+1;
        A1=[A(1)+((B(1)-A(1))/3),A(2)+((B(2)-A(2))/3)];
        end_data(NNN,:)=A1;
        A2=[A(1)+2*((B(1)-A(1))/3),A(2)+2*((B(2)-A(2))/3)];
        C=[(A(1)+B(1))/2,(A(2)+B(2))/2];
        l_AB=(B(2)-A(2))/(B(1)-A(1));
        l_CA3=-1/l_AB;
        b_AB=A(2)-l_AB*A(1);
        b_CA3=C(2)-l_CA3*C(1);
        if B(1)>A(1)
            A3(1)=((sqrt(4)*l/6)*sqrt(l_AB^2 + 1)+b_CA3-b_AB)/(l_AB-l_CA3);
        elseif B(1)<A(1)
            A3(1)=((-sqrt(4)*l/6)*sqrt(l_AB^2 + 1)+b_CA3-b_AB)/(l_AB-l_CA3);
        end
        A3(2)=l_CA3*A3(1)+b_CA3;
        NNN=NNN+1;
        end_data(NNN,:)=A3;
        NNN=NNN+1;
        end_data(NNN,:)=A2;
        NNN=NNN+1;
        end_data(NNN,:)=B;
    end
    start_data=end_data;
    N=length(start_data);
    NNN=0;
end
cla;
plot(start_data(:,1),start_data(:,2),'-r')
axis equal
axis off
zoom on
