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

Spiral Center

by Richard Zapor

Status: Passed
Results: 108428 (cyc: 7, node: 390)
CPU Time: 1.158
Score: 108439.0
Submitted at: 2012-11-01 08:06:28 UTC
Scored at: 2012-11-01 17:45:48 UTC

Current Rank: 1685th (Highest: 27th )
Basis for: Spiral and Center st (diff)

Comments
Please login or create a profile.
Code
function xyout = solver(a, xyin, wts)
% Spiral with centroid adjust, expect small effect wrt knots
nr=size(xyin,1);
awork=a; % when placed zero out columns
xyout=xyin;
unmoved=ones(nr,1);
dxy=[1 1; -1 -1; -1 1;1 -1;1 0;0 1;-1 0;0 -1];

sc=0; % spiral count
xy_center=[0 0];

while any(awork(:))
 sa=[(1:nr)' sum(awork,2)];
 sa=sortrows(sa,-2);
 node=sa(1);
 numadj=sa(1,2);
 d=2*ceil(numadj/8); % circle radius
 
 adj=find(awork(node,:)); % identify unplaced adjacent nodes
 xy_offset=[round(d*cosd(360/numadj*(0:numadj-1))); round(d*sind(360/numadj*(0:numadj-1)))]';
 
 xyout(node,:)=xy_center-[0 1]; % offset center to avoid knottiness
 awork(node,:)=0;
 awork(:,node)=0;
 awork(adj,:)=0;
 awork(:,adj)=0;
 unmoved([node adj])=0;
 
 for j=1:length(adj)
  xyout(adj(j),:)=xy_center+xy_offset(j,:);
 end
 
 sc=sc+1;
 xy_center=[round(5*sc*cosd(17*sc)) round(5*sc*sind(17*sc))]; %
  
end % while nodes to place

% Unmoved may have been over-mapped. Need to fix
% Also moves singles next to adjacency nodes to reduce knots
unmov_vec=find(unmoved);
for j=1:length(unmov_vec)
 ptr=find(a(unmov_vec(j),:)==1,1,'first'); % adjacency of unmoved
 for idxy=1:8
  if ~ismember(xyout(ptr,:)+dxy(idxy,:),xyout,'rows') % found an adjacent point
   xyout(unmov_vec(j),:)=xyout(ptr,:)+dxy(idxy,:);
   break
  end
 end % idxy
end % moving singles and unmoved next to adjacents

% Center Spiral to reduce wt effect, if any vs Knots
xy_offset=floor(mean(xyin));
xyout_offset=floor(mean(xyout));

xyout=xyout+repmat(xy_offset-xyout_offset,nr,1);

if size(unique(xyout,'rows'),1)<nr,xyout=xyin;end % safety net

end