xxxxxxxxxx
let width = 150;
let height = 150;
let phase = 0;
let table = []
let result = []
let L0 = 5 // 風速
let b = 0.4 // 高さの調整
let q = 3 // 飛ぶ砂の量
let d = 0.2 // 転がる砂の量 高さのd*100%転がる
let generation = 0
function setup() {
createCanvas(width, height);
setTable()
}
function setTable() {
table = []
for (let y = 0; y < height; y++) {
let array = []
for (let x = 0; x < width; x++) {
array[x] = random(0, 100)
}
table[y] = array
}
}
function zeroTable() {
let array1 = []
for (let y = 0; y < height; y++) {
let array2 = []
for (let x = 0; x < width; x++) {
array2[x] = 0
}
array1[y] = array2
}
return array1
}
function draw() {
// 世代管理
if (generation > 200) return
generation++
// if(generation % 20 == 0) {
// console.log(generation)
// }
background(220);
warnerModel()
plot()
}
function warnerModel(){
result = zeroTable()
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
if(random(10) < 3) continue
hop(x, y)
if(random(10) < 3) continue
roll(x, y)
}
}
addTable()
}
function hop(x, y) {
const h = table[y][x]
let L = L0 + b * h
result[y][x] -= q
let x2 = x + floor(L)
if (x2 > width - 1) {
x2 = x2 - width
}
result[y][x2] += q
}
function roll(x, y) {
const h = table[y][x]
// 転がる砂の量の単位
const D = h * d * 1 / 8
// 転がる向きはx>0のとこ
addResult(x, y, -8 * D)
addResult(x + 1, y, 2 * D)
// addResult(x - 1, y, 2 * D)
addResult(x, y + 1, 2 * D)
addResult(x, y - 1, 2 * D)
addResult(x + 1, y + 1, D)
// addResult(x - 1, y + 1, D)
// addResult(x - 1, y - 1, D)
addResult(x - 1, y - 1, D)
}
function addResult(x, y, val) {
if (x > width - 1) {
x -= width
}
if (x < 0) {
x += width
}
if (y > height - 1) {
y -= height
}
if (y < 0) {
y += height
}
result[y][x] += val
}
function addTable() {
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
table[y][x] += result[y][x]
}
}
}
function plot() {
for (let y = 0; y < height; y += 1) {
for (let x = 0; x < width; x += 1) {
let h = table[x][y]
let r = map(h, 0, 100, 230, 90)
let g = map(h, 0, 100, 200, 50)
let b = map(h, 0, 100, 150, 0)
stroke(color(r, g, b))
point(x, y);
}
}
}