var x = new Array();
var y = new Array();
var a = new Array();
var b = new Array();
var dir = new Array();
var number_dir = new Array();
function sample(){
tx=0;
for(s=1;s<11;s++){
for(sx=1;sx<11;sx++){
tx=tx+1;
x[tx]=s;
y[tx]=sx;
}}
for(s=1;s<11;s++){
a[s]=1;
b[s]=100;
}
for(t=1;t<101;t++){
dir[t] = new Array();
}
for(t=1;t<101;t++){
tx = 0;
for(s1=-1;s1<2;s1++){
for(s2=-1;s2<2;s2++){
x1 = x[t] + s1;
y1 = y[t] + s2;
sx = search(x1, y1);
if(sx==t)sx=0;
if (sx > 0) tx = tx + 1;
if (sx > 0) dir[t][tx] = sx;
}}
number_dir[t] = tx;
}
var timer;
var delay = 1000;
var loop = function () {
move();
delta();
paint();
clearTimeout(timer);
timer = setTimeout(loop, delay);
}
loop();
}
function delta(){
s1 = 1;
mind = 999;
for(s=1;s<11;s++){
sx = a[s];
d1=(x[sx]-10)*(x[sx]-10)+(y[sx]-10)*(y[sx]-10);
if (d1 < mind) s1 = s;
if (d1 < mind) mind = d1;
}
s2 = 1;
mind = 999;
for(s=1;s<11;s++){
sx = b[s];
d1=(x[sx]-x[s1])*(x[sx]-x[s1])+(y[sx]-y[s1])*(y[sx]-y[s1]);
if (d1 < mind) s2 = s;
if (d1 < mind) mind = d1;
}
mind = 999;
s3 = b[s2];
s4 = s3;
for(s=1;s<number_dir[s3]+1;s++){
n1 = dir[s3][s];
n2 = a[s1];
d1=(x[n2]-x[n1])*(x[n2]-x[n1])+(y[n2]-y[n1])*(y[n2]-y[n1]);
if (d1 < mind) s4=n1;
if (d1 < mind) mind = d1;
}
b[s2] = s4;
}
function move(){
for(s=1;s<11;s++){
sx = a[s];
var n1 = number_dir[sx] * Math.random();
var n2 = Math.floor(n1) + 1;
a[s] = dir[sx][n2];
}
}
function paint(){
var sub=new svgpaint();
str="<svg width=\"500\" height=\"500\">";
for(s=1;s<11;s++){
n1 = a[s];
str=str+sub.rect(50*x[n1]-50,50*y[n1]-50,50,50,"red");
}
for(s=1;s<11;s++){
n1 = b[s];
str=str+sub.rect(50*x[n1]-50,50*y[n1]-50,50,50,"blue");
}
str=str+"</svg>";
$("#memo").html(str);
}
function search(x1,y1){
var s,sx,h;
sx=0;
for (s = 1; s < 101; s++) {
h = 0;
if (x[s] == x1) h = h + 1;
if (y[s] == y1) h = h + 1;
if (h == 2) sx = s;
}
return sx;
}
最終更新:2015年06月19日 14:03