Finish 2012-11-07 16:00:00 UTC

bow middle slurp always plus

by Seth Kosowsky

Status: Passed
Results: 299862 (cyc: 9, node: 1256)
CPU Time: 0.703
Score: 299908.0
Submitted at: 2012-11-01 15:54:00 UTC
Scored at: 2012-11-01 20:59:35 UTC

Current Rank: 1722nd (Highest: 60th )

Comments
Please login or create a profile.
Code
function xyOut = solver(a, xyIn, wts)
    
    %ends and bow arrow
    
    xyIn1 = xyIn - repmat(xyIn(1,:), size(xyIn, 1), 1);
    m = xyIn1(:,1) ./ xyIn1(:,2);
    m(isnan(m)) = 1;
    if all(m == 1),
        %linear
        mag = round((max(xyIn(:,1))  - min(xyIn(:,1)) ) ./ 2);
        cen = round((max(xyIn(:,1))  + min(xyIn(:,1)) ) ./ 2);
        dy = round(0.3*abs((xyIn(:,1) - cen) .^ .8));
        %         dy = round(0.3*(mag - abs((xyIn(:,1) - cen))));
        
        xyOut = xyIn;
        xyOut(:,1) = xyOut(:,1) + dy;
    else
        xyOut = xyIn;
    end
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %prep
    xmin = min(xyOut(:,1));
    ymin = min(xyOut(:,2));
    xmax = max(xyOut(:,1));
    ymax = max(xyOut(:,2));
    
    occu = zeros(1+ymax - ymin, 1+ xmax - xmin);
    occ = xyOut(:,1) - xmin + 1;
    ocr = xyOut(:,2) - ymin + 1;
    ocsub = sub2ind(size(occu), ocr, occ);
    occu(ocsub) = 1;
    
    %expand as needed
    win = ones(3,3) ./ 9;
    occuden = filter2(win, occu, 'same');
    
    deni = find(occuden >= .95);
    fractden = length(deni) ./ size(xyIn, 1);
    if fractden > 0,
        %expand
        xyOut = floor(1.5 .* xyOut);
        %    recalc occupation
        xmin = min(xyOut(:,1));
        ymin = min(xyOut(:,2));
        xmax = max(xyOut(:,1));
        ymax = max(xyOut(:,2));
        
        occu = zeros(1+ymax - ymin, 1+ xmax - xmin);
        occ = xyOut(:,1) - xmin + 1;
        ocr = xyOut(:,2) - ymin + 1;
        ocsub = sub2ind(size(occu), ocr, occ);
        occu(ocsub) = 1;
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %middle
    
    %find conn=2  nodes
    smnodes = find(sum(a, 2) == 2);
    %     aa = a(snodes0, :);
    %     [rr,cc] = find(aa == 1);
    %     snodes = snodes0(rr);
    
    for ii = 1:length(smnodes),
        asrow = a(smnodes(ii), :);
        [cc] = find(asrow == 1);  %should be exactly 2
        
        
        %now snodes(ii) goes to cc
        
        
        %
        %     s = [1 1];
        %     f = [10 -2];
        
        s = xyOut(smnodes(ii), :);
        fa = xyOut(cc(1), :);
        fb = xyOut(cc(2), :);
        
        
        if abs(fa(1) - fb(1)) >= abs(fa(2) - fb(2)),
            %     use dx
            xf = [fa(1):sign(fb(1)-fa(1)):fb(1)];
            m = (fa(2) - fb(2)) ./ (fa(1) - fb(1));
            yfa1 = floor(fa(2) + m .* (xf - fa(1)));
            yfa2 = ceil(fa(2) + m .* (xf - fa(1)));
            
            ptarr = [xf(:) yfa1(:); xf(:) yfa2(:)];
            
        else
            
            xf = [fa(2):sign(fb(2)-fa(2)):fb(2)];
            m = (fa(1) - fb(1)) ./ (fa(2) - fb(2));
            yfa1 = floor(fa(1) + m .* (xf - fa(2)));
            yfa2 = ceil(fa(1) + m .* (xf - fa(2)));
            
            ptarr = [ yfa1(:) xf(:); yfa2(:) xf(:)];
        end
        
        d2s = sum(([ptarr - repmat(s, size(ptarr, 1), 1)]) .^ 2, 2);
        [d2s, sorti] = sort(d2s);
        ptarr = ptarr(sorti, :);
        
        pti = sub2ind(size(occu), ptarr(:,2) - ymin + 1, ptarr(:,1) - xmin + 1);
        ptoccu = occu(pti);  %occupancy in dist order
        endpti = find(ptoccu == 0, 1, 'first');  %1st avail
        
        if ~isempty(endpti),
            occu(sub2ind(size(occu), xyOut(smnodes(ii), 2) - ymin + 1, xyOut(smnodes(ii), 1) - xmin + 1)) = 0;
            xyOut(smnodes(ii),:) = ptarr(endpti, :);
            occu(pti(endpti)) = 1;
        end
        
        
        
    end %for
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    % ends1
    
    
    xmin = min(xyOut(:,1));
    ymin = min(xyOut(:,2));
    xmax = max(xyOut(:,1));
    ymax = max(xyOut(:,2));
    
    occu = zeros(1+ymax - ymin, 1+ xmax - xmin);
    occ = xyOut(:,1) - xmin + 1;
    ocr = xyOut(:,2) - ymin + 1;
    ocsub = sub2ind(size(occu), ocr, occ);
    occu(ocsub) = 1;
    
    %find single ended nodes
    snodes0 = find(sum(a, 2) == 1);
    aa = a(snodes0, :);
    [rr,cc] = find(aa == 1);
    snodes = snodes0(rr);
    
    for ii = 1:length(snodes),
        %now snodes(ii) goes to cc(ii)
        
        
        %
        %     s = [1 1];
        %     f = [10 -2];
        
        s = xyOut(snodes(ii), :);
        f = xyOut(cc(ii), :);
        
        if abs(s(1) - f(1)) >= abs(s(2) - f(2)),
            %     use dx
            xf = [s(1):sign(f(1)-s(1)):f(1)];
            m = (s(2) - f(2)) ./ (s(1) - f(1));
            yf1 = floor(s(2) + m .* (xf - s(1)));
            yf2 = ceil(s(2) + m .* (xf - s(1)));
            
            ptarr = [xf(:) yf1(:); xf(:) yf2(:)];
            
        else
            
            xf = [s(2):sign(f(2)-s(2)):f(2)];
            m = (s(1) - f(1)) ./ (s(2) - f(2));
            yf1 = floor(s(1) + m .* (xf - s(2)));
            yf2 = ceil(s(1) + m .* (xf - s(2)));
            
            ptarr = [ yf1(:) xf(:); yf2(:) xf(:)];
        end
        
        d2s = sum(([ptarr - repmat(f, size(ptarr, 1), 1)]) .^ 2, 2);
        [d2s, sorti] = sort(d2s);
        ptarr = ptarr(sorti, :);
        
        pti = sub2ind(size(occu), ptarr(:,2) - ymin + 1, ptarr(:,1) - xmin + 1);
        ptoccu = occu(pti);  %occupancy in dist order
        endpti = find(ptoccu == 0, 1, 'first');  %1st avail
        
        if ~isempty(endpti),
            occu(sub2ind(size(occu), xyOut(snodes(ii), 2) - ymin + 1, xyOut(snodes(ii), 1) - xmin + 1)) = 0;
            xyOut(snodes(ii),:) = ptarr(endpti, :);
            occu(pti(endpti)) = 1;
        end
        
        
        
    end %for
    
end