function distance( path) % distance( centers, xave, yave, path ) % % centers : an image of the tile centers, oriented vertically % xave, yave : the average distance between adjacent centers % path : the file path to save connection data to % % finds connections between tiles, assuming that the crystal is oriented % vertically. Then allows user to adjust found connections if that option % is chosen % method: look in range of anticipated connection global OPTIONS; load('-mat', path, 'x','y','N','xave','yave', 'rotim'); figure(2) imagesc(rotim) colormap gray hold on plot(x,y,'g.') yshift = floor(yave); xshift = floor(xave); yrange = floor(2*yave/5); xrange = floor(2*xave/5); % stores connections % stores index of points and its 6 connections in a row % 1 2 6 should be larger than the row number % 3 4 5 should be smaller than the row number. % all connections stored twice, for convenience conn = zeros(N, 6); for n = 1:N % find points horizontal to the right --------------------------------- % lookhere is the center of where to look for tile centers lookhere(1) = x(n) + xshift; lookhere(2) = y(n); t = find( x < (lookhere(1) + xrange) & x > (lookhere(1) - xrange) ); t2 = find( y(t(:)) < (lookhere(2) + yrange) & y(t(:)) > (lookhere(2) - yrange), 1, 'last'); if ( ~isempty(t2) ) %horizontal and to the right connections hr = t(t2); conn(n, 1) = hr; conn(hr, 4) = n; plot( [x(n),x(hr)],[y(n), y(hr)],'y-' ) end t = []; t2 = []; % find points to the lower right -------------------------------------- lookhere(1) = x(n) + xshift/2; lookhere(2) = y(n) + yshift; t = find( x < (lookhere(1) + xrange) & x > (lookhere(1) - xrange) ); t2 = find( y(t(:)) < (lookhere(2) + yrange) & y(t(:)) > (lookhere(2) - yrange), 1, 'last'); if ( ~isempty(t2) ) % down and to the right connections lr = t(t2); conn(n, 2) = lr; conn(lr, 5) = n; plot( [x(n),x(lr)],[y(n), y(lr)],'m-' ) end t = []; t2 = []; % find points to the lower left --------------------------------------- lookhere(1) = x(n) - xshift/2; lookhere(2) = y(n) + yshift; t = find( x < (lookhere(1) + xrange) & x > (lookhere(1) - xrange) ); t2 = find( y(t(:)) < (lookhere(2) + yrange) & y(t(:)) > (lookhere(2) - yrange), 1, 'last'); if ( ~isempty(t2) ) %horizontal and to the right connections ll = t(t2); conn(n, 3) = ll; conn(ll, 6) = n; plot( [x(n),x(ll)],[y(n), y(ll)],'c-' ) end t = []; t2 = []; end % fill in isolated missing connections conn = fillinconn(conn,N,x,y, true); hold off % saves variable 'conn' to desired location save( path, 'conn', '-mat', '-append'); if ( ~isempty(find(OPTIONS == 8)) ) modify_conn(path); end