xxxxxxxxxx
function setup() {
width = 2000
height = 2000
N = 15000
l = 1.5
createCanvas(width, height);
background(230)
fill(200, 0, 0)
circle(width / 2, 3 * height / 4, 5)
//
// 素数列の準備
//
num = []
prime = []
for (var i = 2; i < N; i++) {
num.push(i)
}
n = 2
while (n < Math.sqrt(N)) {
prime.push(n)
num = num.filter(function(value) {
return !(value % n === 0)
})
n = num[0]
}
prime = prime.concat(num)
//
// 素数ランダムウォーク
//
direction = ["N", "E", "S", "W"]
pre_direction = "N"
x = width / 2
y = 3 * height / 4
for (var i = 0; i < N; i++) {
next_direction = direction.filter(function(value) {
return !(value === pre_direction)
})
if (prime.indexOf(i) > 0) {
rand = Math.floor(Math.random() * 3)
switch (next_direction[rand]) {
//
// もと来た道を帰れないので、N-S, E-Wで逆の方向に向かう
//
case "N":
line(x, y, x, y+l)
y = y+l
pre_direction = "S"
break;
case "E":
line(x, y, x-l, y)
x = x-l
pre_direction = "W"
break;
case "S":
line(x, y, x, y-l)
y = y-l
pre_direction = "N"
break;
case "W":
line(x, y, x+l, y)
x = x+l
pre_direction = "E"
}
} else {
switch (pre_direction) {
case "N":
line(x, y, x, y-l)
y = y-l
break;
case "E":
line(x, y, x+l, y)
x = x+l
break;
case "S":
line(x, y, x, y+l)
y = y+l
break;
case "W":
line(x, y, x-l, y)
x = x-l
}
}
}
}