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(){
for (s = 1; s < 11; s++) {
s1 = a[s];
s2 = b[s];
mind = 999;
s3 = s2;
for (sx = 1; sx < number_dir[s2] + 1; sx++) {
n2 = dir[s2][sx];
d1 = (x[n2] - x[s1]) * (x[n2] - x[s1]) + (y[n2] - y[s1]) * (y[n2] - y[s1]);
if (d1 < mind) s3 = n2;
if (d1 < mind) mind = d1;
}
b[s] = s3;
}
}
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:08