function varargout = gui_hiercluster(varargin)
%GUI_HIERCLUSTER M-file for gui_hiercluster.fig
%      GUI_HIERCLUSTER, by itself, creates a new GUI_HIERCLUSTER or raises the existing
%      singleton*.
%
%      H = GUI_HIERCLUSTER returns the handle to a new GUI_HIERCLUSTER or the handle to
%      the existing singleton*.
%
%      GUI_HIERCLUSTER('Property','Value',...) creates a new GUI_HIERCLUSTER using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to gui_hiercluster_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      GUI_HIERCLUSTER('CALLBACK') and GUI_HIERCLUSTER('CALLBACK',hObject,...) call the
%      local function named CALLBACK in GUI_HIERCLUSTER.M with the given input
%      arguments.
%
%      *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 gui_hiercluster

% Last Modified by GUIDE v2.5 15-May-2008 00:05:43

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_hiercluster_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_hiercluster_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 gui_hiercluster is made visible.
function gui_hiercluster_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)


X = varargin{1};
C = varargin{2};

[m n] = size(X);
if n ~= 2, error('Demo only works in 2D.'); end

% for display purposes
x = X(:,1);
y = X(:,2);
xmin = min(x);
xmax = max(x);
ymin = min(y);
ymax = max(y);
xrange = xmax-xmin;
yrange = ymax-ymin;
ax = [xmin-xrange/8 xmax+xrange/8 ymin-yrange/8 ymax+yrange/8];

% draw points
plot(x,y,'.','markersize',10);
hold on;

X(2*m-1,:)=0;
h = zeros(1,m-1);
for k=m+1:2*m-1
  X(k,:) = mean(X(C.points{k},:),1);
  i = C.points{k};
  j = [];
  for l=1:length(i)
    j = [j reshape([i(l)*ones(1,length(i)-l);i(l+1:end)],1,2*(length(i)-l))];
  end
  h(k-m) = plot(X(j,1),X(j,2),'b-');
  set(h(k-m),'visible','off');
end
axis(ax);
hold off

handles.m = m;
handles.h = h;
handles.drawnow = 0;


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

% Update handles structure
guidata(hObject, handles);

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


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


handles.slider = hObject;
guidata(hObject,handles);

% 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 mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(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)

curpos = get(handles.slider,'Value');
maxpos = get(handles.slider,'Max');
minpos = get(handles.slider,'Min');

k = round(sqrt((curpos-minpos)/(maxpos-minpos))*(handles.m-1));
set(handles.h(1:k),'visible','on');
set(handles.h(k+1:handles.m-1),'visible','off');
drawnow


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

curpos = get(handles.slider,'Value');
maxpos = get(handles.slider,'Max');
minpos = get(handles.slider,'Min');

k = round(sqrt((curpos-minpos)/(maxpos-minpos))*(handles.m-1));
set(handles.h(1:k),'visible','on');
set(handles.h(k+1:handles.m-1),'visible','off');
drawnow


