xxxxxxxxxx
108
let x = 0;
let y = 0;
let p;
let restart = true
let fernSelector
let fernJSON
let ferns = []
function preload() {
fernJSON = loadJSON("ferns.json")
fernSelector = createSelect()
fernSelector.parent("ferns")
fernSelector.changed(update)
}
function update(){
let fern = ferns.find(x=>x.name == fernSelector.value())
console.log(fern)
setValue("f1","a", fern.f1.a)
setValue("f1","b", fern.f1.b)
setValue("f1","c", fern.f1.c)
setValue("f1","d", fern.f1.d)
setValue("f1","e", fern.f1.e)
setValue("f1","f", fern.f1.f)
setValue("f1","p", fern.f1.p)
setValue("f2","a", fern.f2.a)
setValue("f2","b", fern.f2.b)
setValue("f2","c", fern.f2.c)
setValue("f2","d", fern.f2.d)
setValue("f2","e", fern.f2.e)
setValue("f2","f", fern.f2.f)
setValue("f2","p", fern.f2.p)
setValue("f3","a", fern.f3.a)
setValue("f3","b", fern.f3.b)
setValue("f3","c", fern.f3.c)
setValue("f3","d", fern.f3.d)
setValue("f3","e", fern.f3.e)
setValue("f3","f", fern.f3.f)
setValue("f3","p", fern.f3.p)
setValue("f4","a", fern.f4.a)
setValue("f4","b", fern.f4.b)
setValue("f4","c", fern.f4.c)
setValue("f4","d", fern.f4.d)
setValue("f4","e", fern.f4.e)
setValue("f4","f", fern.f4.f)
setValue("f4","p", fern.f4.p)
restart = true
}
function setup() {
canvas = createCanvas(windowWidth, windowHeight - 150)
ferns = Object.values(fernJSON)
for(let fern of ferns){
fernSelector.option(fern.name)
}
}
function getValue(row, col) {
return parseFloat(document.getElementById(row + col).value)
}
function setValue(row, col, value) {
document.getElementById(row + col).value = value
}
function draw() {
if (restart) {
background(255)
x = 0
y = 0
p = [getValue("f1", "p"), getValue("f2", "p"), getValue("f3", "p"), getValue("f4", "p")]
restart = false
}
for (i = 0; i < 1000; i++) {
let px = map(x, -2.1820, 2.6558, 0, width)
let py = map(y, 0, 9.9983, height, 0)
stroke("#86FF61")
point(px, py)
let w
let r = random()
if (r < p[0]) {
w = "f1"
} else if (r < p[0] + p[1]) {
w = "f2"
} else if (r < p[0] + p[1] + p[2]) {
w = "f3"
} else {
w = "f4"
}
let a = getValue(w, "a")
let b = getValue(w, "b")
let c = getValue(w, "c")
let d = getValue(w, "d")
let e = getValue(w, "e")
let f = getValue(w, "f")
let newX = a * x + b * y + e
let newY = c * x + d * y + f
x = newX
y = newY
}
}
function start() {
restart = true
}